Yii ClientSide Validation on Render Partial not Working

前端 未结 3 1315
無奈伤痛
無奈伤痛 2021-02-10 07:01

I have a Yii form which calls a render partial from another model (team has_many team_members). I want to call via ajax a partial view to add members in team/_form. All works (c

3条回答
  •  难免孤独
    2021-02-10 07:43

    If you will look at the CActiveForm::run:

    $cs->registerCoreScript('yiiactiveform');
    //...
    $cs->registerScript(__CLASS__.'#'.$id,"jQuery('#$id').yiiactiveform($options);");
    

    Then you will understand that you validation will not work, because you render partial and not the whole page. And these scripts show up at the bottom of the page. So you should solve this by execute these scripts.

    After you partial is rendered, try to get activeform script which should be stored at the scipts array:

    $this->renderPartial('_member',array('model'=> $model, 'index'=> $index));
    $script = Yii::app()->clientScript->scripts[CClientScript::POS_READY]['CActiveForm#team-form'];
    

    after, send it with rendered html to page:

    echo ""
    

    Also remember before you will append recieved html on the page you should include jquery.yiiactiveform.js, if you not already did it(by render another form, or registerCoreScript('yiiactiveform')), on the page from calling ajax request. Otherwise javascript error will raised.

    Hope this will help.

    Edit: Sorry I'm not understood that you are render part of form and not the whole. But you validation will not work exactly with the same issue. Because jQuery('#$id').yiiactiveform($options); script was not created for the field.

提交回复
热议问题