yii2 表单输入 ajax 验证不会执行
yii2 form input ajax validation will not perfom
我正在尝试根据我添加的文档在我的表单中激活 ajax 验证(
'enableAjaxValidation' => true 和 validationUrl )及其操作
谁能告诉我为什么验证不会对我的表单执行?
我在控制台中没有任何错误
谢谢你
这是我的控制器
public function actionCreate()
{
$model = new Developers();
return $this->render('_form', [
'model' => $model
]);
}
public function actionValidation(){
$model = new Developers();
if(Yii::$app->request->isAjax && $model->load(Yii::$app->request->post()) )
{
Yii::$app->response->format = 'json';
return ActiveForm::validate($model);
}
}
还有我的表格
<div class="developer-form">
<?php $form = ActiveForm::begin([
'id' => $model->formName(),
'enableAjaxValidation' => true,
'validationUrl'=>\yii\helpers\Url::toRoute('site-admin/validation')
]); ?>
<?= $form->field($model, 'name')->textInput(['minlength'=>true]) ?>
<?= $form->field($model, 'family')->textInput() ?>
<?= $form->field($model, 'phone')->textInput() ?>
<?= $form->field($model, 'email')->textInput() ?>
<?= $form->field($model, 'address')->textInput() ?>
<?= $form->field($model, 'brithday')->textInput() ?>
<?= $form->field($model, 'age')->textInput() ?>
<?= $form->field($model, 'ability')->textInput() ?>
<?= $form->field($model, 'role')->textInput() ?>
<?= $form->field($model, 'point')->textInput() ?>
<?= $form->field($model, 'join_date')->textInput() ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
这里是我的模型规则函数
public function rules()
{
return [
[['name',
'family',
'phone',
'email',
'address',
'brithday',
'age',
'ability',
'role',
'point',
'join_date',
], 'required'],
[['id'], 'integer'],
[['date_project_done','estimate_for_next_project','number_of_project','activity_rate','free_rate','project_done'], 'safe'],
[['address'], 'string', 'max' => 100],
[['name'], 'string', 'min' => 3],
];
}
在视图文件表单变量中使用以下代码:
<?php $form = ActiveForm::begin([
'id' => 'login-form', //your form id
'enableAjaxValidation' => true,
'enableClientValidation' => false,
'validateOnBlur' => false,
'validateOnType' => false,
'validateOnChange' => false,
]) ?>
在您的控制器操作中使用以下代码:
public function actionCreate()
{
$model = new Developers();
if(Yii::$app->request->isAjax && $model->load(Yii::$app->request->post()) )
{
if($model->validate())
{
// do here anything
$model->save();
}
else
{
Yii::$app->response->format = 'json';
return ActiveForm::validate($model);
}
}
else
{
return $this->render('_form', [
'model' => $model
]);
}
}
我正在尝试根据我添加的文档在我的表单中激活 ajax 验证( 'enableAjaxValidation' => true 和 validationUrl )及其操作 谁能告诉我为什么验证不会对我的表单执行? 我在控制台中没有任何错误
谢谢你
这是我的控制器
public function actionCreate()
{
$model = new Developers();
return $this->render('_form', [
'model' => $model
]);
}
public function actionValidation(){
$model = new Developers();
if(Yii::$app->request->isAjax && $model->load(Yii::$app->request->post()) )
{
Yii::$app->response->format = 'json';
return ActiveForm::validate($model);
}
}
还有我的表格
<div class="developer-form">
<?php $form = ActiveForm::begin([
'id' => $model->formName(),
'enableAjaxValidation' => true,
'validationUrl'=>\yii\helpers\Url::toRoute('site-admin/validation')
]); ?>
<?= $form->field($model, 'name')->textInput(['minlength'=>true]) ?>
<?= $form->field($model, 'family')->textInput() ?>
<?= $form->field($model, 'phone')->textInput() ?>
<?= $form->field($model, 'email')->textInput() ?>
<?= $form->field($model, 'address')->textInput() ?>
<?= $form->field($model, 'brithday')->textInput() ?>
<?= $form->field($model, 'age')->textInput() ?>
<?= $form->field($model, 'ability')->textInput() ?>
<?= $form->field($model, 'role')->textInput() ?>
<?= $form->field($model, 'point')->textInput() ?>
<?= $form->field($model, 'join_date')->textInput() ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
这里是我的模型规则函数
public function rules()
{
return [
[['name',
'family',
'phone',
'email',
'address',
'brithday',
'age',
'ability',
'role',
'point',
'join_date',
], 'required'],
[['id'], 'integer'],
[['date_project_done','estimate_for_next_project','number_of_project','activity_rate','free_rate','project_done'], 'safe'],
[['address'], 'string', 'max' => 100],
[['name'], 'string', 'min' => 3],
];
}
在视图文件表单变量中使用以下代码:
<?php $form = ActiveForm::begin([
'id' => 'login-form', //your form id
'enableAjaxValidation' => true,
'enableClientValidation' => false,
'validateOnBlur' => false,
'validateOnType' => false,
'validateOnChange' => false,
]) ?>
在您的控制器操作中使用以下代码:
public function actionCreate()
{
$model = new Developers();
if(Yii::$app->request->isAjax && $model->load(Yii::$app->request->post()) )
{
if($model->validate())
{
// do here anything
$model->save();
}
else
{
Yii::$app->response->format = 'json';
return ActiveForm::validate($model);
}
}
else
{
return $this->render('_form', [
'model' => $model
]);
}
}