E
E
Eugene2019-10-11 16:04:21
Yii
Eugene, 2019-10-11 16:04:21

Is it possible to get form validation errors without yii2 widgets?

In general, I decided to make a form, but not based on activeForm, since it is more expensive to style ActiveForm, but with a simple layout. But I thought that by tearing out some blocks and classes from the generated ActiveForm form, I could resurrect the validation.
I created such a form, specifying the id and name needed for the inputs, I didn’t forget about my friend csrf

mold

<form  id="login-form" class="login100-form validate-form form-horizontal" action="/login" method="post">
                <input type="hidden" name="<?=Yii::$app->request->csrfParam; ?>" value="<?=Yii::$app->request->getCsrfToken(); ?>" />
          <span class="login100-form-title">
            Авторизация
          </span>

                <div class="form-group wrap-input100 validate-input field-loginform-username required">
                    <input id="loginform-username" class="form-control input100" type="text" name="LoginForm[username]" placeholder="Логин">
                    <span class="focus-input100"></span>
                    <span class="symbol-input100">
              <i class="fa fa-envelope" aria-hidden="true"></i>
            </span>
                    <p class="help-block help-block-error "></p>
                </div>

                <div class="form-group wrap-input100 validate-input field-loginform-password required has-error">
                    <input id="loginform-password" class="form-control input100" type="password" name="LoginForm[password]" placeholder="Пароль">
                    <span class="focus-input100"></span>
                    <span class="symbol-input100">
              <i class="fa fa-lock" aria-hidden="true"></i>
            </span>
                    <p class="help-block help-block-error "></p>
                </div>

                <div class="container-login100-form-btn">
                    <button name="login-button" type="submit" class="login100-form-btn">
                        Войти
                    </button>
                </div>

                <div class="text-center p-t-12">
                    <div class="form-group field-loginform-rememberme txt1">
                        <div class="col-lg-offset-1 col-lg-3"><input type="hidden" name="LoginForm[rememberMe]" value="0"><input type="checkbox" id="loginform-rememberme" name="LoginForm[rememberMe]" value="1" checked=""> <label for="loginform-rememberme">Запомнить меня</label></div>
                        <div class="col-lg-8"><p class="help-block help-block-error "></p></div>
                    </div>
                </div>

                <div class="text-center p-t-136">
                    <p class="help-block help-block-error "></p>
                </div>
                
            </form


But it was not there. Yii2 threw a pig in the garden and tells me "Fuck you, not validation errors"
Not that the field is not filled (I can process this on the client side, figs with him)
Not an unsuccessful validation error from submitting the form (Incorrect login or password) he does not issue.
Is it possible to catch validation errors somehow without widgets? At least incorrect login and password errors)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2019-10-12
@evgen9586

In general, you have three options.
The first way, I took the form that you gave in the question as a basis, I think that this one will suit you.

<?php
$model = new LoginForm;
$form = ActiveForm::begin([
   'id' => 'login-form',
   'options' => ['class' => 'login100-form validate-form form-horizontal'],
   'fieldConfig' => [
      'options' => ['class' => 'form-group wrap-input100 validate-input'],
      'template' => "{input}\n<span class=\"focus-input100\"></span>{label}\n{hint}\n{error}",
      'labelOptions' => ['class' => 'symbol-input100'],
      'inputOptions' => ['class' => 'form-control input100']
   ],
]);
?>
          <span class="login100-form-title">
            Авторизация
          </span>
          <?= $form->field($model, 'username')->textInput(['placeholder' => 'Логин'])->label('<i class="fa fa-envelope" aria-hidden="true"></i>') ?>
          <?= $form->field($model, 'password')->textInput(['placeholder' => 'Пароль'])->label('<i class="fa fa-lock" aria-hidden="true"></i>') ?>
          <div class="container-login100-form-btn">
            <?= Html::submitButton('Войти', ['class' => 'login100-form-btn', 'name' => 'login-button']) ?>
          </div>  
          <?= $form->field($model, 'rememberMe', ['template' => '<div class="text-center p-t-12"><div class="form-group field-loginform-rememberme txt1"><div class="col-lg-offset-1 col-lg-3">{input}</div></div></div>'])->checkbox() ?>
<?php
ActiveForm::end();
?>

The second way is unlikely to work, since yii\bootstrap\ActiveForm is used. In this option, the form is more flexibly configured.
Well, the third way is to use the widget from kartik, although I'm not very inclined towards it.
ps In AppAsset, you must have YiiAsset connected, your css and js, the rest can be omitted
public $depends = [
        'yii\web\YiiAsset',
    ];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question