I
I
IvanOne2014-08-04 18:32:44
Yii
IvanOne, 2014-08-04 18:32:44

How to do validation on Yii client?

There is this form

<div class="searchForm">
    <?php $form=$this->beginWidget('CActiveForm', array(
        'id'=>'search-form',
        'enableAjaxValidation'=>true,
        'enableClientValidation'=>true,
        'clientOptions' => array(
            'validateOnSubmit' => true,
        )
    )); ?>
    <p class="note">Поля с <span class="required">*</span> обязательны.</p>

    <?php echo $form->errorSummary($model); ?>
    <div class="inputInline">
        <?php
        echo $form->labelEx($model,'departure');
        $this->widget('CAutoComplete',
            array(
            'name'=>'departure',
            'url'=>array('site/autocomplete'),
            'minChars'=>1,
            )
        );
        echo $form->error($model,'departure');
        ?>
    </div>
    <div class="inputInline">
        <?php
        echo $form->labelEx($model,'destination');
        $this->widget('CAutoComplete',
            array(
                'name'=>'destination',
                'url'=>array('site/autocomplete'),
                'minChars'=>1,
            )
        );
        echo $form->error($model,'destination');
        ?>
    </div>
    <div class="inputInline">
    <?php
        echo $form->labelEx($model,'date');
        $this->widget('zii.widgets.jui.CJuiDatePicker',array(
        'name'=>'date',
        'language'=>'ru',
        'options'=>array(
        'showAnim'=>'fold',
        ),
        'htmlOptions'=>array(
        'style'=>'height:20px;'
        ),
        ));
        echo $form->error($model,'date');
    ?>
    </div>
    <div class="inputInline">
        <?php echo CHtml::ajaxSubmitButton('Найти',array('ajax'),array('update'=>".resBlock")); ?>
    </div>
    <?php $this->endWidget(); ?>

3 fields are generated with autocompletion and one calendar does not show errors when sending fields, how to organize this form correctly? data from the form also leaves asynchronously, so here is the form model
class SearchForm extends CFormModel
{
    public $departure;
    public $destination;
    public $date;
    public function rules()
    {
        return array(
            // username and password are required
            array('departure, destination, date', 'required','message'=>'Поле обязательно для заполнения!'),
            array('date', 'date','format'=>'dd.MM.yyyy','message'=>'Не верно введена дата'),
            array('departure, destination','type','type'=>'string'),
            array('departure, destination, date','safe')
        );
    }

    public function attributeLabels()
    {
        return array(
            'departure'=>'Место отправления',
            'destination'=>'Место назначения',
            'date'=>'Дата'
        );
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander David, 2014-08-05
@alexdevid

you need to make a method in the controller that will check the validity and send the result.
attach ajax request parameters to the ajaxSubmitButton button.
more details here for example

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question