Yii2: how to use custom validation function for activeform?

前端 未结 12 1273
独厮守ぢ
独厮守ぢ 2020-12-25 14:18

In my form\'s model, I have a custom validation function for a field defined in this way

class SignupForm extends Model
{
    public function rules()
    {
          


        
12条回答
  •  自闭症患者
    2020-12-25 14:33

    I stumbled on this when using the CRUD generator. The generated actionCreate() function doesn't include a model validation call so custom validators never get called. Also, the _form doesn't include and error summary.

    So add the error summary to the _form.

    errorSummary($model); ?>
    

    ...and add the validation call - $model->validate() - to the controller action

    public function actionCreate()
    {
        $model = new YourModel();
    
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {...
    

提交回复
热议问题