How to validate tabular data in yii?

前端 未结 2 1808
清歌不尽
清歌不尽 2021-01-23 12:07

I have created a form which allows user to save tabular data. I have followed this tutorial . I have managed to add multiple instance for a Model and I am getting the data in th

2条回答
  •  有刺的猬
    2021-01-23 12:30

    http://www.yiiframework.com/doc/api/1.1/CActiveForm

    The AJAX-based validation has a few limitations. First, it does not work with file upload fields. Second, it should not be used to perform validations that may cause server-side state changes. Third, it is not designed to work with tabular data input for the moment.

    So, you need to write custom error handler for tabular data

    //view
    $form = $this->beginWidget('CActiveForm', array(   
        'enableClientValidation' => true,
        'clientOptions' => array(
            'validateOnChange' => false,
            'validateOnType' => false,
            'validateOnSubmit' => true,
            'beforeValidate' => 'js:formBeforeValidate',
            'afterValidate' => 'js:formAfterValidate'
        ),
    ));
    //some js
    function formBeforeValidate (form) {    
        //clean errors
        return true;
    }
    
    function formAfterValidate(form, data, hasError) {    
        if (hasError === true) {
            //append errors to input
        }
        return !hasError;
    }
    

提交回复
热议问题