formbuilder: ajaxValidation doesn't work with errorSummary set to true #2254

Closed
AnniG opened this Issue Mar 23, 2013 · 1 comment

Comments

Projects
None yet
2 participants
@AnniG

AnniG commented Mar 23, 2013

Once I turn on errorSummary jquery and yiiactiveform don't get registered anymore.

controller:

public function actionCreate() {
    $model=new House;
    if(isset($_POST['ajax']) && $_POST['ajax']==='house-form') {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }    
    $form=new CForm('application.modules.house.views.form',$model);
    ...
    $this->render('create',array(
        'form'=>$form
    ));
}

config file for the form:

return array(
    'title'=>'New House',
    'showErrorSummary'=>true,
    'activeForm'=>array(
        'class'=>'CActiveForm',
        'enableAjaxValidation'=>true,
        'id'=>'house-form',
        'clientOptions'=>array(
            'validateOnSubmit'=>true,
        ),
    ),
    'elements'=>array(
        'email'=>array(
            'type'=>'text',
            'size'=>35,
            'maxlength'=>80,
        ),
        'name'=>array(
            'type'=>'text',
            'size'=>35,
            'maxlength'=>80,
        ),
        ...
    ),
    'buttons'=>array(
        ...
    ),
);
@resurtm

This comment has been minimized.

Show comment Hide comment
@resurtm

resurtm Mar 23, 2013

Contributor

Confirmed and reproduced.

This is because CActiveForm::error() method must be called for all the attributes that should be AJAX or client validated. (Read more about CActiveForm::$attributes protected property.)

Possible temporary workaround:

class FormInputElement extends CFormInputElement
{
    public function render()
    {
        if($this->type==='hidden')
            return $this->renderInput();
        $output=array(
            '{label}'=>$this->renderLabel(),
            '{input}'=>$this->renderInput(),
            '{hint}'=>$this->renderHint(),
            '{error}'=>$this->renderError(),
            // OLD VERSION OF THIS LINE:
            // '{error}'=>$this->getParent()->showErrorSummary ? '' : $this->renderError(),
        );
        return strtr($this->layout,$output);
    }
}

$form=new CForm(array(
    // ...
    'inputElementClass'=>'FormInputElement',
    // ...
),$model);
Contributor

resurtm commented Mar 23, 2013

Confirmed and reproduced.

This is because CActiveForm::error() method must be called for all the attributes that should be AJAX or client validated. (Read more about CActiveForm::$attributes protected property.)

Possible temporary workaround:

class FormInputElement extends CFormInputElement
{
    public function render()
    {
        if($this->type==='hidden')
            return $this->renderInput();
        $output=array(
            '{label}'=>$this->renderLabel(),
            '{input}'=>$this->renderInput(),
            '{hint}'=>$this->renderHint(),
            '{error}'=>$this->renderError(),
            // OLD VERSION OF THIS LINE:
            // '{error}'=>$this->getParent()->showErrorSummary ? '' : $this->renderError(),
        );
        return strtr($this->layout,$output);
    }
}

$form=new CForm(array(
    // ...
    'inputElementClass'=>'FormInputElement',
    // ...
),$model);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment