Php 在CActiveForm中初始化ajaxVar时

Php 在CActiveForm中初始化ajaxVar时,php,yii,Php,Yii,关于文档中的CActiveForm(clientOptions部分) ajaxVar:string,表示请求是AJAX请求的参数的名称。触发AJAX验证时,名为this属性的参数将与其他表单数据一起发送到服务器。参数值是表单ID。然后服务器端可以检测是谁触发了AJAX验证并做出相应的反应。默认为“ajax” 现在看看我的例子: 小结:我有一个包含两个字段的表单mail和newEmail,我通过ajaxSubmitButton提交表单(如果需要表单代码,请告诉我如何放置)。在下面的示例中,我得到了

关于文档中的
CActiveForm
(clientOptions部分)


ajaxVar:string,表示请求是AJAX请求的参数的名称。触发AJAX验证时,名为this属性的参数将与其他表单数据一起发送到服务器。参数值是表单ID。然后服务器端可以检测是谁触发了AJAX验证并做出相应的反应。默认为“ajax”

现在看看我的例子: 小结:我有一个包含两个字段的表单
mail
newEmail
,我通过
ajaxSubmitButton
提交表单(如果需要表单代码,请告诉我如何放置)。在下面的示例中,我得到了两种状态的
var\u dump($\u POST)
内容:

首先:以下变量转储($\u POST)用于字段(新电子邮件)为空时:

array
'User' =>
    array
    'email' => string 'user@gmail.com' (length=14)
    'newEmail' => string '' (length=0)
Second:以下变量转储($\u POST)用于填充所有字段时:

array
'User' =>
    array
    'email' => string 'user@gmail.com' (length=14)
    'newEmail' => string 'admin@gmail.net' (length=19)
'ajax' => string 'email-form' (length=10)
'yt0' => string 'update' (length=18)
正如您看到的,只有当所有字段都被填充时,
ajaxVar(ajax)
才存在于
$\u POST
中。当
ajaxVar(ajax)
在CActiveForm中初始化时


编辑 电子邮件形式:

<?php
<div class="form">
    <?php $form = $this->beginWidget('CActiveForm',array(
            'id'=>'email-form',
            'enableAjaxValidation'=>true,
    'clientOptions'=>array(
            'validateOnSubmit'=>true,
            'focus'=>'input[type="email"]:first',
            )
    )); ?>

    <div class="row">
            <?php echo $form->label($model,'email') ?>
            <?php echo $form->textField($model,'email') ?>
            <?php echo $form->error($model,'email') ?>
    </div>

    <div class="row">
            <?php echo $form->label($model,'newEmail') ?>
            <?php echo $form->textField($model,'newEmail') ?>
            <?php echo $form->error($model,'newEmail') ?>
    </div>
    <hr>
    <div class="row">
            <?php  echo CHtml::ajaxSubmitButton(
            'update',
            Yii::app()->createUrl('upanel/user/CEmail'),
            array(
                    'dataType'=>'json',
                    'type' => 'POST',
                    'data' => "js:$('#email-form').serialize()",
                    'success'=>'function(data){
                    if(data.status=="success")
                    {
                            //alert(data.status);
                            hideFormErrors(form="#email-form");
                            callback(status=data.status);
                    }else{
                            formErrors(data,form="#email-form");
                    }
                    }',

                    'beforeSend'=>'before',
            ),
            array(
                    'id' => 'update-button'.uniqid(),
                    'class'=>'submit-button'
            )
            );
            ?>
    </div>
    <?php $this->endWidget() ?>
</div> 
<?php echo $form->errorSummary($model,'please solve following errors:') ?>
public function  actionCEmail()
{
    /*ob_start();
    var_dump($_POST);
    $log=ob_get_contents();
    $fp = fopen('data.html', 'w');
    fwrite($fp, $log);
    fclose($fp);
    Yii::app()->end();*/ //This block active whenever i want see the $_POST content

    $model = $this->loadModel(Yii::app()->user->id);
    $model->scenario = 'CEmail';
    $this->performAjaxValidation($model,'email-form');

    if(Yii::app()->request->isAjaxRequest)
        $this->renderPartial('_cemail',array('model'=>$model),false,true);
    else
        $this->render('update',array('model'=>$model,'form'=>'_cemail'));
}

protected function performAjaxValidation($model,$form)
{
    if(isset($_POST['ajax']) && $_POST['ajax']===$form)
    {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }
}
如果
'validateOnSubmit'=>为true,则在提交时初始化,
。尝试使用
validateOnChange=>true
并在初始化时显示cactiveform代码

真的不要再编造“童话”了。 如果您不理解上一个问题中的代码,请尝试阅读。 我向您展示了我在工作项目中使用的ajax验证的工作代码。使用该方法,您的
$\u POST
将在所有JSON对象字段发生更改时更新,其中包含表单错误

停止淹没社区。问候。

在您的行动中:

    public function  actionCEmail()
    {
        $model = $this->loadModel(Yii::app()->user->id);
        $model->scenario = 'CEmail';
        if(Yii::app()->request->isAjaxRequest)
            {
                $error = CActiveForm::validate($model);
                if($error!='[]')
                                {
                                    echo $error;
                    Yii::app()->end();
                }
            }

        if(isset($_POST['CEmailForm']))
              {
               //put form name in POST above 
               //do whatever you need with model here
               //save / edit etc.
               //in the end
               echo CJSON::encode(array(
                                  'status'=>'success',
                                  )); 
           Yii::app()->end();
               }
if(Yii::app()->request->isAjaxRequest) //check renders 
            $this->renderPartial('_cemail',array('model'=>$model),false,true);
        else
            $this->render('update',array('model'=>$model,'form'=>'_cemail'));
    }
在您的视图中,提交按钮:

 <?php echo CHtml::ajaxSubmitButton ("Save", Yii::app()->request->url, array (
            'dataType' => 'json', 
            'type'=>'post',
            'success' =>
            'js:function (data) {


              if(data.status=="success"){
                            //do whatever you need on success
                            //show flash/notification 
                          };

                                      }
              else {//show errors here
                $.each(data, function(key, val) {
                        $("#YourFormIDhere #"+key+"_em_").text(val);
                $("#YourFormIDhere #"+key+"_em_").css(\'display\',\'block\');           
                                });
                        //can show error summarry here or custom notifications      
                    };

              }',
        ), array (
        'id' => 'yourbuttonid_submit_'.rand(1,255), // Need a unique id or they start to conflict with more than one load.
        ));?>

这样做,不要浪费时间。问候。

@ineersa我理解您对其他问题的回答,这是其他问题,您的回答非常有用,我理解ajaxVAr初始化时的内容。现在我明白了我的主要问题是什么:
newEamil
字段默认为空,并且在提交表单时不更改此字段,即使设置了
validateOnChange=>true
,ajaxVar也不会初始化。这个问题可以解决吗?角色是:
array('email,newEmail','required','on'=>'CEmail','message'=>$required\u msg),
。尝试使活动表单
$form=$this->beginWidget('CActiveForm',array('id'=>'user-form','enableClientValidation'=>true,'enableAjaxValidation'=>true,'clientOptions'=>array('validateOnSubmit'=>true,),
如果这没有帮助,请尝试在控制器中使用
$this->performAjaxValidation($model);
此函数代码如下:
受保护的函数performAjaxValidation($model){If(isset($\u POST['ajax'])&$\u POST['ajax']=='user-form{echo CActiveForm::validate($model);Yii::app()->end();}}
如果这对我的答案没有帮助,请再看一次。验证工作非常有效,完全符合你的需要。也请尝试阅读此线程。我认为这正是你需要的。不起作用。如果
新电子邮件
仍然为空(即使填充并再次清空它)然后我们将发送表单ajaxVar don't initialized.ajaxVar:string,表示请求的参数的名称是AJAX请求。当触发AJAX验证时,名为this属性的参数将与其他表单数据一起发送到服务器。参数值是表单ID。然后服务器端可以检测出装配AJAX验证并做出相应的反应。默认为“AJAX”。我对get表单错误没有问题,我只是想知道为什么提交操作上的
ajaxVar
没有初始化,正如文档指出的,正如我在Github上讨论的:
string,指示请求的参数的名称是AJAX请求。当lidation被触发,
但是谢谢你的帮助。我刚刚测试过。通过上面提供的代码,我的更改密码表单包含:
UserChangePassword[passwo…admin UserChangePassword[验证…admin ajax changepassword form yt0 Save
。所以我不理解您的问题。ajaxVar的工作方式与它必须的一样。无论如何,祝您好运。您一直是Github的问题吗?如果看一看,我已经完全解释过了。是的,我看到了。代码与ajaxVar init完全一样,我希望如此。@ineersa,感谢您为我所做的努力。@ineersa解决我的问题,这就是我的答案。致以最崇高的敬意
 <?php echo CHtml::ajaxSubmitButton ("Save", Yii::app()->request->url, array (
            'dataType' => 'json', 
            'type'=>'post',
            'success' =>
            'js:function (data) {


              if(data.status=="success"){
                            //do whatever you need on success
                            //show flash/notification 
                          };

                                      }
              else {//show errors here
                $.each(data, function(key, val) {
                        $("#YourFormIDhere #"+key+"_em_").text(val);
                $("#YourFormIDhere #"+key+"_em_").css(\'display\',\'block\');           
                                });
                        //can show error summarry here or custom notifications      
                    };

              }',
        ), array (
        'id' => 'yourbuttonid_submit_'.rand(1,255), // Need a unique id or they start to conflict with more than one load.
        ));?>
{"UserChangePassword_verifyPassword":["Required field","Retype Password is incorrect."]}