0

我有这个cformmodel

<?php

class CombinationForm extends CFormModel {

    public $a;
    public $b;
    public $c;
    public $d;
    public $e;
    public $f;

    public function rules() {
        return array(
            array('a, b, c, d, e, f', 'required'),
            array('a, b, c, d, e, f', 'numerical', 'integerOnly' => true),
        );
    }

}

当用户输入字母而不是数字时,我想使用 ajax 捕获错误。

这是动作:

    public function actionIndex() {
        $model_combination_form = new CombinationForm();
        $this->performAjaxValidation($model_combination_form);
        if (isset($_POST['CombinationForm'])) {
//            echo '<pre>';
//            print_r($_POST['CombinationForm']);
//            echo '</pre>';
        }
        $this->render('combination_form', array(
            'model_combination_form' => $model_combination_form,
        ));
    }

它应该触发了 ajax 验证,但它没有。

这是表格:

<div style="position:relative;float:left;">

    <?php
    $form = $this->beginWidget('CActiveForm', array(
        'id' => 'combination-form',
        'enableAjaxValidation' => false,
    ));
    ?>

    <?php echo $form->errorSummary($model_combination_form); ?>

    <div style="float:left;padding:5px 5px;">
        <?php echo $form->textField($model_combination_form, 'a', array('style' => 'width:20px;')); ?>
    </div>

    <div style="float:left;padding:5px 5px;">
        <?php echo $form->textField($model_combination_form, 'b', array('style' => 'width:20px;')); ?>
    </div>

    <div style="float:left;padding:5px 5px;">
        <?php echo $form->textField($model_combination_form, 'c', array('style' => 'width:20px;')); ?>
    </div>

    <div style="float:left;padding:5px 5px;">
        <?php echo $form->textField($model_combination_form, 'd', array('style' => 'width:20px;')); ?>
    </div>

    <div style="float:left;padding:5px 5px;">
        <?php echo $form->textField($model_combination_form, 'e', array('style' => 'width:20px;')); ?>
    </div>

    <div style="float:left;padding:5px 5px;">
        <?php echo $form->textField($model_combination_form, 'f', array('style' => 'width:20px;')); ?>
    </div>

    <div style="float:left;padding:5px 5px;">
        <?php echo CHtml::submitButton('Genereaza'); ?>
    </div>

    <?php $this->endWidget(); ?>

</div>
<div style="clear:both;"></div>
4

1 回答 1

1

您需要在CActiveform 中将 clientOptionsvalidateOnSubmit属性设置为true

<div style="position:relative;float:left;">

<?php
$form = $this->beginWidget('CActiveForm', array(
    'id' => 'combination-form',
    'enableAjaxValidation' => true,
    'clientOptions=>array(
         //validation when submitting form, if validation don't perform, form will not send
        'validateOnSubmit'=>true,
    )
));
?>

<?php echo $form->errorSummary($model_combination_form); ?>

<div style="float:left;padding:5px 5px;">
    <?php echo $form->textField($model_combination_form, 'a', array('style' => 'width:20px;')); ?>
</div>

<div style="float:left;padding:5px 5px;">
    <?php echo $form->textField($model_combination_form, 'b', array('style' => 'width:20px;')); ?>
</div>

<div style="float:left;padding:5px 5px;">
    <?php echo $form->textField($model_combination_form, 'c', array('style' => 'width:20px;')); ?>
</div>

<div style="float:left;padding:5px 5px;">
    <?php echo $form->textField($model_combination_form, 'd', array('style' => 'width:20px;')); ?>
</div>

<div style="float:left;padding:5px 5px;">
    <?php echo $form->textField($model_combination_form, 'e', array('style' => 'width:20px;')); ?>
</div>

<div style="float:left;padding:5px 5px;">
    <?php echo $form->textField($model_combination_form, 'f', array('style' => 'width:20px;')); ?>
</div>

<div style="float:left;padding:5px 5px;">
    <?php echo CHtml::submitButton('Genereaza'); ?>
</div>

<?php $this->endWidget(); ?>

于 2013-07-29T02:04:23.220 回答