Create \app\models\FormModel:
class FormModel extends Model
{
public $login;
public $password;
public $rememberMe = true;
public function rules()
{
return [
[['login', 'password'], 'required'],
['rememberMe', 'boolean'],
];
}
public function attributeLabels()
{
return [
'login' => 'Ваш логин',
'password' => 'Ваш пароль',
'rememberMe' => 'Запомнить?',
];
}
}
Create \app\controllers\SiteController::actionTest:
public function actionTest()
{
$form = new FormModel();
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
}
return $this->render('test', [
'form' => $form
]);
}
Create views/site/test.php
<?php
$activeForm = \yii\widgets\ActiveForm::begin([]);
?>
<?= $activeForm->errorSummary($form); ?>
<?= $activeForm->field($form, 'login'); ?>
<?= $activeForm->field($form, 'password')->passwordInput(); ?>
<?= $activeForm->field($form, 'rememberMe')->checkbox(); ?>
<div class="form__buttons">
<?= \yii\helpers\Html::submitButton('Войти', [
'class' => 'button button_main button_submit',
'name' => 'submit'
]); ?>
</div>
<?php \yii\widgets\ActiveForm::end(); ?>
http://<YOUR-APP-URL>/index.php?r=site%2Fcontact and fill form fields with any data.Form is submitted to server.
Form doesn't submitted. Fields are validated (green indication after submit click), but there is no request to server. We need to click on Submit one more time to send data.
Form can be submitted after first click only of validateOnSubmit = false:
$activeForm = \yii\widgets\ActiveForm::begin([
'validateOnSubmit' => false
]);
| Q | A |
| --- | --- |
| Yii version | 2.0.9 |
| PHP version | 5.6.24 |
| Operating system | FreeBSD |
Verified.
Verified that it was like that at least since 2.0.5 if not from the very first public version...
The issue is with the following:
<?= \yii\helpers\Html::submitButton('Войти', [
'class' => 'button button_main button_submit',
'name' => 'submit' // <------- this!
]); ?>
Name it differently and it works well.
Related to https://github.com/yiisoft/yii2/issues/6642
Have no idea why that happens :(
The issue is sorted using this:
'validateOnSubmit' => false
Thanks @samdark
i think that's problem is the following line :
<?= \yii\helpers\Html::submitButton('Войти', [
'class' => 'button button_main button_submit',
'name' => 'submit' // here,
]); ?>
@DmLapin you should avoid to use 'submit' as name. Futher more details here :(https://github.com/yiisoft/yii2/blob/master/docs/guide/input-forms.md)
Forms and their child elements should not use input names or ids that conflict with properties of a form, such as submit, length, or method. Name conflicts can cause confusing failures. For a complete list of rules and to check your markup for these problems, see DOMLint.
@pana1990 thank you
Hey guys, I know the reason.
The button you gonna click needs to be in form.
Instead of generating new one, he thinks he is related to the previous form.
You must let him know, that he relates other form.
You must create <form "anotherForm"></form> to somewhere else outside of your form.
And set this line
<?=Html::a('your link',
['/yoururlt'],
[
'data' =>[
'form'=>'anotherForm',
'confirm'=>Yii::t('app', 'Are you sure you want to delete this item ?')
]
]);?>
this solves the problem.
Your are welcome :)
<?php
$form = ActiveForm::begin([
'id' => "id--name",
'options' => ['class' => 'edit_form'],
'enableAjaxValidation' => false,
'enableClientValidation' => true,
]);
?>
My problem was enableAjaxValidation. set it false solved it.
Most helpful comment
Hey guys, I know the reason.
The button you gonna click needs to be in form.
Instead of generating new one, he thinks he is related to the previous form.
You must let him know, that he relates other form.
You must create
<form "anotherForm"></form>to somewhere else outside of your form.And set this line
<?=Html::a('your link', ['/yoururlt'], [ 'data' =>[ 'form'=>'anotherForm', 'confirm'=>Yii::t('app', 'Are you sure you want to delete this item ?') ] ]);?>this solves the problem.
Your are welcome :)