Yii的客户方验证的渲染部分不工作 [英] Yii ClientSide Validation on Render Partial not Working

查看:157
本文介绍了Yii的客户方验证的渲染部分不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Yii的形式,调用渲染来自其他模型(队的has_many team_members)部分。我想通过AJAX调用的局部视图,以团队/ _form添加成员。所有的作品(呼叫,显示,保存)除了AJAX验证(服务器和客户端)。如果我提交表单,成员的模型没有验证,即使在客户端,它不是验证所需的字段。

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 (call, show, save) except for ajax validations (server and client side). If i submit form, member's model isn't validating, even in client side, it's not validating the required fields.

任何线索?

// _形式

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'team-form',
        'enableAjaxValidation'=>true,
        'enableClientValidation'=>true,
        'clientOptions'=>array(
            'validateOnSubmit'=>true,
            'validateOnChange'=>true

        ),
        'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>

//控制器

//Controller

public function actionMember($index)
{
    $model = new TeamMember();
        $this->renderPartial('_member',array(
            'model'=> $model, 'index'=> $index
        )
                ,false,true
                ); 
}  

public function actionCreate()
{
        $model=new Team;
        $members = array();
        if(isset($_POST['Team']))
        {
                $model->attributes=$_POST['Team'];

                if(!empty($_POST['TeamMember'])){
                foreach($_POST['TeamMember'] as $team_member)
                            {
                                $mem = new TeamMember();
                                $mem->setAttribute($team_member);
                                if($mem->validate(array('name'))) $members[]=$mem;
                            }
                }
                        $this->redirect(array('team/create','id'=>$model->id,'#'=>'submit-message'));

        }

        $members[]=new TeamMember;
        $this->performAjaxMemberValidation($members);
        $this->render('create',array(
                'model'=>$model,'members'=>$members
        ));

}

// _成员

//_member

<div class="row-member<?php echo $index; ?>">
    <h3>Member <?php echo $index+1; ?></h3>
    <div class="row">
    <?php echo CHtml::activeLabel($model, "[$index]name",array('class'=>'member')); ?>
    <?php echo CHtml::activeTextField($model, "[$index]name",array('class'=>'member')); ?>
    <?php echo CHtml::error($model, "[$index]name");?>    
    </div>
</div>

ProcessOutput 设置为true。没有骰子。 开关的RenderPartial()渲染()。没有骰子。

ProcessOutput was set to true. No dice. Switch renderPartial() to render(). No dice.

推荐答案

如果你会看<一href="https://github.com/yiisoft/yii/blob/1b165448af1716c228acb5b89474890acf0a3ead/framework/web/widgets/CActiveForm.php#L369">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.

在局部呈现,尝试获得的ActiveFor​​m脚本应存放在 scipts 数组:

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'];

后,与呈现的HTML发送到页面:

after, send it with rendered html to page:

echo "<script type='text/javascript'>$script</script>"

还记得之前你会追加:收到的HTML页面,你应该包括 jquery.yiiactiveform.js ,如果你不是已经做到了(被给予另一种形式,或 registerCoreScript('yiiactiveform')),从调用Ajax请求的页面上。否则JavaScript错误会引发。

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.

希望这会有所帮助。

编辑: 对不起,我不明白,你是渲染的组成部分,而不是全部。但你确认不会同样的问题完全正常工作。因为的jQuery(#$身份证')yiiactiveform($选择); 脚本没有为字段创建

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.

这篇关于Yii的客户方验证的渲染部分不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆