yii2 表单输入 ajax 验证不会执行 [英] yii2 form input ajax validation will not perfom
本文介绍了yii2 表单输入 ajax 验证不会执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图根据我添加的文档在我的表单中激活 ajax 验证('enableAjaxValidation' => true 和 validationUrl ) 及其操作谁能告诉我为什么验证不会在我的表单上执行?我在控制台中没有任何错误
谢谢
这是我的控制器
公共函数 actionCreate(){$model = new Developers();返回 $this->render('_form', ['模型' =>$model]);}公共函数 actionValidation(){$model = new Developers();if(Yii::$app->request->isAjax && $model->load(Yii::$app->request->post()) ){Yii::$app->response->format = 'json';返回 ActiveForm::validate($model);}}
和我的表格
<?php $form = ActiveForm::begin(['id' =>$model->formName(),'enableAjaxValidation' =>真的,'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']) ?><?php ActiveForm::end();?>
这里是我的模型规则函数
公共函数规则(){返回 [[['姓名','家庭','电话','电子邮件','地址','生日','年龄','能力','角色','观点','加入日期',], '必需的'],[['id'], '整数'],[['date_project_done','estimate_for_next_project','number_of_project','activity_rate','free_rate','project_done'],'safe'],[['地址'], '字符串', 'max' =>100],[['name'], 'string', 'min' =>3],];}
解决方案
在视图文件表单变量中使用以下代码:
<?php $form = ActiveForm::begin(['id' =>'login-form',//你的表单ID'enableAjaxValidation' =>真的,'启用客户端验证' =>错误的,'validateOnBlur' =>错误的,'validateOnType' =>错误的,'validateOnChange' =>错误的,]) ?>
在您的控制器操作中使用以下代码:
公共函数 actionCreate(){$model = new Developers();if(Yii::$app->request->isAjax && $model->load(Yii::$app->request->post()) ){if($model->validate()){//在这里做任何事情$model->save();}别的{Yii::$app->response->format = 'json';返回 ActiveForm::validate($model);}}别的{返回 $this->render('_form', ['模型' =>$model]);}}
im trying to active ajax validation in my form according the docs i added( 'enableAjaxValidation' => true and validationUrl )with its action can anyone tell me why validation will not perform on my form ? i dont have any error in console
thankyou
this is my controller
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);
}
}
and its my form
<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>
and here my model rules function
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],
];
}
解决方案
Use below code in view file form variable :
<?php $form = ActiveForm::begin([
'id' => 'login-form', //your form id
'enableAjaxValidation' => true,
'enableClientValidation' => false,
'validateOnBlur' => false,
'validateOnType' => false,
'validateOnChange' => false,
]) ?>
Use below code in your controllers action :
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
]);
}
}
这篇关于yii2 表单输入 ajax 验证不会执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文