Pregunta

I have this 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),
        );
    }

}

When the user enters letters instead of numbers, I want to catch the error using ajax.

This is the action:

    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,
        ));
    }

It should have triggered the ajax validation, but it did not.

This is the form:

<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>
¿Fue útil?

Solución

You need to set validateOnSubmit property of clientOptions to true in CActiveform

<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(); ?>

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top