yii2 form input ajax validation will not perfom

北城以北 提交于 2019-12-11 16:32:58

问题


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],

    ];
}

回答1:


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
     ]);
    }
}


来源:https://stackoverflow.com/questions/45772753/yii2-form-input-ajax-validation-will-not-perfom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!