S
S
Svetlana Galenko2018-10-26 19:48:26
Yii
Svetlana Galenko, 2018-10-26 19:48:26

Need to reconfigure HTML form in ActiveForm yii2?

Hello, there is a form that is difficult for me, help me wind it up on ActiveForm
Here is the HTML form:

<form name="appointment_form" id="appointment_form" method="post" action="#" onSubmit="return false">

        <span class="input input--kohana">
          <input class="input__field input__field--kohana" type="text" id="input-29" name="input-29" />
          <label class="input__label input__label--kohana" for="input-29">
                        <i class="icon-user6 icon icon--kohana"></i>
            <span class="input__label-content input__label-content--kohana">Your Name</span>
          </label>
        </span>
                        <span class="input input--kohana">
          <input class="input__field input__field--kohana" type="text" id="input-30" name="input-30" />
          <label class="input__label input__label--kohana" for="input-30">
            <i class="icon-dollar icon icon--kohana"></i>
            <span class="input__label-content input__label-content--kohana">Email Address</span>
          </label>
        </span>
                        <span class="input input--kohana last">
          <input class="input__field input__field--kohana" type="text" id="input-31" name="input-31" />
          <label class="input__label input__label--kohana" for="input-31">
            <i class="icon-phone5 icon icon--kohana"></i>
            <span class="input__label-content input__label-content--kohana">Phone Number</span>
          </label>
        </span>

                        <span class="input input--kohana">
          <input class="input__field input__field--kohana" type="text" id="datepicker"  placeholder="Appointment Date" onClick="" name="datepicker" />
        </span>

                        <span class="input input--kohana message">
                	<input class="input__field input__field--kohana" type="text" id="textarea" name="textarea" >
          <label class="input__label input__label--kohana" for="textarea">
            <i class="icon-new-message icon icon--kohana"></i>
            <span class="input__label-content input__label-content--kohana">Message</span>
                    </label>
                </span>

                        <input name="submit" type="submit" value="send" onClick="validateAppointment();">
                    </form>

Here are my settings:
<?php $form = ActiveForm::begin(['method' => 'POST', 'id' => 'appointment_form']); ?>

                        <span class="input input--kohana"><?= $form->field($model, 'name1', ['inputOptions' => ['class' => 'input__field input__field--kohana', 'id' => 'input-29']])->label([
                                'class' => 'input__label input__label--kohana', 'for' => 'input-29', 'template' => '<i class="icon-user6 icon icon--kohana"></i><span class="input__label-content input__label-content--kohana">Your Name</span>{label}'
                            ]); ?></span>

                        <span class="input input--kohana"><?= $form->field($model, 'email1', ['inputOptions' => ['class' => 'input__field input__field--kohana', 'id' => 'input-30']])->label([
                                'class' => 'input__label input__label--kohana', 'for' => 'input-30', 'template' => '<i class="icon-dollar icon icon--kohana"></i><span class="input__label-content input__label-content--kohana">Email Address</span>{label}'
                            ]); ?></span>

                        <span class="input input--kohana last"><?= $form->field($model, 'phone', ['inputOptions' => ['class' => 'input__field input__field--kohana', 'id' => 'input-31']])->label([
                                'class' => 'input__label input__label--kohana', 'for' => 'input-31', 'template' => '<i class="icon-phone5 icon icon--kohana"></i><span class="input__label-content input__label-content--kohana">Phone Number</span>{label}'
                        ]); ?></span>

                        <span class="input input--kohana"><?= $form->field($model, 'datepicker', ['inputOptions' => ['class' => 'input__field input__field--kohana', 'id' => 'datepicker']]); ?></span>

                        <span class="input input--kohana message"><?= $form->field($model, 'body1', ['inputOptions' => ['class' => 'input__field input__field--kohana', 'id' => 'textarea']])->label([
                                'class' => 'input__label input__label--kohana', 'for' => 'textarea', 'template' => '<i class="icon-new-message icon icon--kohana"></i><span class="input__label-content input__label-content--kohana">Message</span>{label}'
                            ]) ?></span>

                        <div class="form-group">
                            <input name="submit" type="submit" value="submit">
                        </div>

                        <?php ActiveForm::end(); ?>

Here is the controller:
/**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        /* Создаем экземпляр класса */
        $model = new AppointmentForm();
        /* получаем данные из формы и запускаем функцию отправки contact, если все хорошо, выводим сообщение об удачной отправке сообщения на почту */
        if ($model->load(Yii::$app->request->post()) && $model->appointment(Yii::$app->params['adminEmail'])) {
            Yii::$app->session->setFlash('contactFormSubmitted');
            return $this->refresh();
            /* иначе выводим форму обратной связи */
        }  else {
        return $this->render('index');
        }
    }

Model:
/**
     * Sends an email to the specified email address using the information collected by this model.
     * @param string $email1 the target email address
     * @return bool whether the model passes validation
     */
    public function appointment($email1)
    {
        $content = "<p>Email: " . $this->email1 . "</p>";
        $content .= "<p>Name: " . $this->name1 . "</p>";
        $content .= "<p>Phone: " . $this->phone . "</p>";
        $content .= "<p>Datepicker: " . $this->datepicker . "</p>";
        $content .= "<p>Body: " . $this->body1 . "</p>";
        if ($this->validate()) {
            Yii::$app->mailer->compose("@app/mail/layouts/html", ["content" => $content])
                //->setTo($email)
                ->setTo('[email protected]')
                ->setFrom([\Yii::$app->params['supportEmail'] => $this->name1])
                //->setFrom([$this->email => $this->name])
                //->setFrom('[email protected]')
                //->setFrom([\Yii::$app->params['supportEmail'] => $this->name])
                ->setPhone($this->phone)
                ->setDatepicker($this->datepicker)
                ->setTextBody1($this->body1)
                ->send();

            return true;
        }
        return false;
    }

And here is the error I encountered:
1. in C:\Users\acer\OSPanel\domains\medicalyii2\vendor\yiisoft\yii2\helpers\BaseHtml.php at line                  * See  for explanation of attribute expression.
     *
     * @param Model $model the model object
     * @param string $attribute the attribute name or expression
     * @return string the generated input name
     * @throws InvalidArgumentException if the attribute name contains non-word characters.
     */
    public static function getInputName($model, $attribute)
    {
        $formName = $model->formName();
        if (!preg_match(static::$attributeRegex, $attribute, $matches)) {
            throw new InvalidArgumentException('Attribute name must contain word characters only.');
        }
        $prefix = $matches[1];
        $attribute = $matches[2];
        $suffix = $matches[3];
        if ($formName === '' && $prefix === '') {
            return $attribute . $suffix;
        } elseif ($formName !== '') {
                
2. yii\base\ErrorHandler::handleFatalError()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-10-27
@swallow_97

Slightly shortened your code for generating the form.

<?php
   $form = ActiveForm::begin([
       'id' => 'appointment_form',
       'fieldConfig' => [
           'options' => [
               'tag' => 'span',
               'class' => 'input input--kohana'
           ],
           'template' => '{input}{label}{error}',
           'inputOptions' => ['class' => 'input__field input__field--kohana'],
           'labelOptions' => [
               'class' => 'input__label input__label--kohana',
           ]
       ]
   ]);
?>
<?= $form->field($model, 'name')->textInput()->label("<i class=\"icon-phone5 icon icon--kohana\"></i><span class=\"input__label-content input__label-content--kohana\">" . $model->getAttributeLabel('name') . "</span>") ?>
<!-- ОСТАЛЬНЫЕ ПОЛЯ ФОРМЫ  -->
<?= $form->field($model, 'body')->textInput()->label() ?>
<?= Html::submitButton('Submit'); ?>
<?php
  ActiveForm::end();
?>

I think that it will be better. Add some attributes for the fields yourself, if necessary.
But I’m not entirely sure about the template for the label, it’s possible that it can not be duplicated, but I haven’t figured out how yet.
ps There is no need to form a variable before this line and then pass it to the letter template.
It is enough just to transfer the model.
// Удалить!!!
   /*   $content = "<p>Email: " . $this->email1 . "</p>";
        $content .= "<p>Name: " . $this->name1 . "</p>";
        $content .= "<p>Phone: " . $this->phone . "</p>";
        $content .= "<p>Datepicker: " . $this->datepicker . "</p>";
        $content .= "<p>Body: " . $this->body1 . "</p>";*/
        if ($this->validate()) {
            Yii::$app->mailer->compose("@app/mail/layouts/html", ["content" => $this])

And already in the template itself, process and form html.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question