Yii的ajaxSubmitButton()与字段验证字段、Yii、ajaxSubmitButton

2023-09-11 00:46:19 作者:孤独似野鬼

我使用Yii ajaxSubmitButton()提交表单。此外,我已经设置了enableAjaxValidation参数设置为true,以验证相应的文本框。

I'm using Yii ajaxSubmitButton() to submit a form. Besides, I have set the 'enableAjaxValidation' parameter to true to validate the corresponding textboxes.

我能做什么:

验证领域时,焦点离开它,异步的。 调用服务器端的方法是单击时按钮,异步。

的问题是,不知道如何执行字段验证被点击提交按钮时,如果该模型有效,在客户端执行一个部分呈现

The problem is that I don't know how to perform the fields validation when the submit button is clicked and, if the model is validated, perform a partial rendering in client side.

如果我覆盖ajaxSubmitButton的'成功'事件,我得到的部分呈现,但我不能保持模型验证。

If I override the 'success' event in ajaxSubmitButton, I get the partial rendering, but I can't maintain the model validation..

任何帮助吗?

修改

感谢您的答复,

该validateOnSubmit标志已经被设置,如果没有设置'成功'的事件模型将被正确验证。

The validateOnSubmit flag is already set and the model would be validated correctly if the 'success' event was not set.

在ajaxSubmitButton是这样的:

When the ajaxSubmitButton is like this:

<?php echo CHtml::ajaxSubmitButton( 'Send',
                                        CHtml::normalizeUrl(array('site/ajaxIndexSubmit')),
                                        array(
                                        'error'=>'js:function(){
                                            alert(\'error\');
                                        }',
                                        'beforeSend'=>'js:function(){
                                            alert(\'beforeSend\');
                                        }',
                                        'success'=>'js:function(data){
                                            alert(\'success, data from server: \'+data);
                                        }',
                                        'complete'=>'js:function(){
                                            alert(\'complete\');
                                        }',
                                        //'update'=>'#where_to_put_the_response',
                                        )
                                    );
    ?>

警报(成功),将打印相应的模型验证字符串。有一次,我有一个字符串,什么逻辑必须在客户端进行invoken?

the alert('success') will print the string corresponding to the model validation. Once I have that string, what logic must be invoken in client side?

的原因覆盖成功的javascript处理程序是接收来自服务器的部分呈现,不同的模型验证。我想两件事:确认和局部呈现

The reason to override the 'success' javascript handler is to receive a partial rendering from the server, different to the model validation. I want both things: validation and partial rendering.

推荐答案

嘿,我有同样的问题,力得到它的工作,即使aftervalidate,的beforeValidate和等。而且二来我力喜欢用一个扩展该怎么把我的应用程序已经有很多。 所以,我提出这样的:

Hey I had same problem and dint get it working even with aftervalidate, beforevalidate and so on.And secondly I dint like to use a extension for this coz my app already has many. So I made this:

编辑:按巴特的建议下,我把那些code这里本身

Edit : As per Barth's suggestion , i am putting those code here itself.

步骤1:@你的控制器动作

Step 1: @ your controller action

public function actionMyAction()
    {       
            $model=new User;             
            $this->performAjaxValidation($model);  

            if(isset($_POST['User']))
            {
                    $model->attributes=$_POST['User'];
                    $valid=$model->validate();            
                    if($valid){

                       //do anything here
                         echo CJSON::encode(array(
                              'status'=>'success'
                         ));
                        Yii::app()->end();
                        }
                        else{
                            $error = CActiveForm::validate($model);
                            if($error!='[]')
                                echo $error;
                            Yii::app()->end();
                        }
           }

    }

步骤2:@您的看法 您的形式可能看起来像这样

Step 2: @ your view Your form may look like this

<?php
$form=$this->beginWidget('CActiveForm', array(
    'id'=>'user-form',
    'enableAjaxValidation'=>true,
    'action'=>$this->createUrl('myController/MyAction'),
    'enableClientValidation'=>true,
 )); ?>
    <div class="errorMessage" id="formResult"></div>
    <div id="AjaxLoader" style="display: none"><img src="<?php echo Yii::app()->request->baseUrl; ?>/images/spinner.gif"></img></div>
    <div class="row-user-single">
            <?php echo $form->labelEx($model,'attribute1'); ?>
            <?php echo $form->passwordField($model,'attribute1',array('size'=>60,'maxlength'=>500)); ?>
            <?php echo $form->error($model,'attribute1'); ?>
    </div>

    <div class="row-user-single">
            <?php echo $form->labelEx($model,'attribute2'); ?>
            <?php echo $form->passwordField($model,'attribute2',array('size'=>60,'maxlength'=>500)); ?>
            <?php echo $form->error($model,'attribute2'); ?>
    </div>
    <div class="buttons">

     <?php echo CHtml::ajaxSubmitButton('Save',CHtml::normalizeUrl(array('myController/MyAction','render'=>true)),
             array(
                 'dataType'=>'json',
                 'type'=>'post',
                 'success'=>'function(data) {
                     $("#AjaxLoader").hide();  
                    if(data.status=="success"){
                     $("#formResult").html("form submitted successfully.");
                     $("#user-form")[0].reset();
                    }
                     else{
                    $.each(data, function(key, val) {
                    $("#user-form #"+key+"_em_").text(val);                                                    
                    $("#user-form #"+key+"_em_").show();
                    });
                    }       
                }',                    
                 'beforeSend'=>'function(){                        
                       $("#AjaxLoader").show();
                  }'
                 ),array('id'=>'mybtn','class'=>'class1 class2')); ?>

这是工作得很好,我 和所有的code上面写的都是我的写作风格。如需要,您可以改变他们,但是, 只有两个要注意如下内容: 1. @模型的控制器调用验证,并返回一个JSON对象,如果表格无效 2. @您的观点打破了这个JSON对象(或遍历它),并显示窗体的各元素下的错误消息。

This is working just fine for me And all the code written above are my style of writing. You may change them as required but, there are only two things to be noted as follows: 1. @ your controller call validate of your model and return a json object if form is invalid 2. @ your view break this json object (OR traverse through it) and show the error messages under respective elements of the form.

请简单:)

您可以在这里看到我的博客文章也: http://www.yiiframework.com/forum/index.php/topic/37075-form-validation-with-ajaxsubmitbutton/

You may see my blog post here too: http://www.yiiframework.com/forum/index.php/topic/37075-form-validation-with-ajaxsubmitbutton/