I
I
Ivan Trofimov2014-02-25 20:11:23
Yii
Ivan Trofimov, 2014-02-25 20:11:23

How to properly set up clientValidation in Yii 1.1.14?

Guys, please explain. I want to validate the form on the client side (and when validating, only add the .error class to the clumsy input without displaying error messages, so I don’t add $form->errorSummary, etc. to the view). Judging by the documentation, I realized that it is necessary to pass TRUE to the enableClientValidation parameter like this:

$checkOrderForm = $this->beginWidget('CActiveForm', array(
     'id' => 'check-order-form',
     'stateful' => true,
     'enableClientValidation' => TRUE,
));

But nothing works and the validation JS code does not appear anywhere on the page. How in general should it appear and in what place?
Rules in the model:
rules() {
        return array(
            // типы полей
            array('id, order_start_time, order_finish_time, order_status_id, boiler_id, price_boiler, price_delivery', 'numerical', 'integerOnly' => true),
            array('name, email, address_delivery', 'length', 'max' => 255),
            array('name', 'length', 'min' => 2),
            array('phone', 'length', 'max' => 25),
            array('phone', 'length', 'min' => 5),
            array('email', 'email'),
            // сценарий оставления заявки
            array('name, phone, email, boiler_id', 'required', 'on' => 'add'),
            // сценарий подтверждения заявки (через админку)
            array('name, phone, email, address_delivery, order_status_id, boiler_id, price_boiler, price_delivery', 'required', 'on' => 'confirm'),
            // сценарий проверки статуса заявки
            array('id', 'required', 'on' => 'check'),
}

On the server side, everything is perfectly validated and works, I've been fighting for days.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mikitachu, 2014-02-25
@cbone

In order for client-side validation to work, the form must have calls to CActiveForm::error() for all fields that require validation, regardless of whether errors should be displayed visually or not. In the latter case, you also need to add the corresponding parameter:

$checkOrderForm = $this->beginWidget('CActiveForm', array(
     'id' => 'check-order-form',
     'stateful' => true,
     'enableClientValidation' => TRUE,
     'clientOptions' => array(
          'hideErrorMessage'  => true,
     ),
));

Also note that by default the error class is assigned not to the input, but to the nearest parent that matches the selector specified in the inputContainer of the same clientOptions parameter, by default it is 'div'.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question