Yii ajaxform验证仅验证表单的一部分

时间:2013-01-28 08:14:36

标签: php ajax forms yii

我有一个Yii表单,它收集三个模型的数据。我正在使用ajaxvalidation,在更改和提交时使用:

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

现在问题是只有前三个输入被验证,这些输入的代码:

1:

<div class="input-bg wide-input">

                    <?php 
                    //fill this field after selecting newspaper
                    echo $form->hiddenField($newspaper, 'id'); ?>
                    <?php echo $form->textField($newspaper, 'name', array('maxlength' => 45, 'placeholder'=>'Krantnaam')); ?>
                    <?php echo $form->error($newspaper, 'name', array('class'=>'error')); ?>
                </div>
                <a class="button gray-button">Aanbieden</a>

2和3:

<div class="input-bg linked-input">
                    <?php echo $form->textField($address, 'postalcode', array('maxlength' => 45, 'placeholder'=>'Postcode')) ?>
                    <?php echo $form->error($address, 'postalcode'); ?>
                </div>

                <div class="input-bg linked-input">
                    <?php echo $form->textField($address, 'street_number', array('maxlength' => 45, 'placeholder'=>'Huisnummer')); ?>
                    <?php echo $form->error($address, 'street_number'); ?>
                </div>

这三个输入用于$newspaper and $address两个模型。现在的情况是,这三个输入完全验证(在更改和提交时),其他七个输入未经验证,其中一个输入未经验证的示例:

 <div class="input-bg linked-input">
            <?php echo $form->textField($offer, 'firstname', array('maxlength' => 45, 'placeholder'=>'Voornaam')); ?>
            <?php echo $form->error($offer, 'firstname'); ?>
        </div>

最后,我将发布我的控制器代码:

$this->performAjaxValidation($offer, 'offer-form');
$this->performAjaxValidation($address, 'offer-form');
$this->performAjaxValidation($newspaper, 'offer-form');

if(Yii::app()->getRequest()->getIsAjaxRequest()) {
    echo CActiveForm::validate( array($address));  
    echo CActiveForm::validate( array($offer));
    echo CActiveForm::validate( array($newspaper)); 
    print_r($_POST);
    die();
    //Yii::app()->end(); 
}

如果我正在提交或更改表单,我会收到JSON,其中包含未在眼睛上验证的字段的错误,但它们不会出现错误状态。

我希望有人可以看到错误或其他任何内容。

格尔茨,

1 个答案:

答案 0 :(得分:0)

一旦发现错误,它返回的原因是performAjaxValidation在验证模型后调用Yii::app()->end()。为了验证几个模型,请使用以下方法

protected function performTabularAjaxValidation($models, $form = null) {
    if (Yii::app()->getRequest()->getIsAjaxRequest() && (($form === null) || ($_POST['ajax'] == $form))) {
        echo CActiveForm::validateTabular($models);
        Yii::app()->end();
    }
}

并在您的控制器方法中

$this->performTabularAjaxValidation(array($address,$offer,$newspaper),'offer-form');