G
G
GM2mars2014-01-15 09:22:38
JavaScript
GM2mars, 2014-01-15 09:22:38

Ajax validation in Yii. Why is a validation error not displayed for the uniqueness of the field?

Several validation parameters are set for the form, including the uniqueness of the field value.
yii honestly fulfills other validation rules and gives an error in the form, but if you enter a value that is already in the database (validation for a unique value), there will be no error on the client and the form will be sent, but the server will not skip the value and will not write it to the database .

Validation rules:
public function rules() {
  return array(
    array('username, password, email', 'required'),
    array('username', 'unique', 'className'=>'Users', 'message'=>'Данный логин занят' ),
    array('username', 'length', 'min'=>3, 'max'=>20),
    array('email', 'email'),
    array('password', 'length', 'min'=>5, 'max'=>100),
    array('id, username', 'safe', 'on'=>'search'),
  );
}

The form:
$form=$this->beginWidget('CActiveForm', array(
  'id'=>'registr-form',
  'enableClientValidation'=>true,
  'action'=>Yii::app()->createUrl('/site/CreateProfil'),
  'clientOptions'=>array(
    'validateOnSubmit'=>true,
  ),
));
    
echo '<label for="login">Логин:</label>';
echo $form->textField($model,'username', array('class'=>"input_text"));
echo $form->error($model,'username'); 
echo '<label for="password">Пароль:</label>';
echo $form->passwordField($model,'password', array('class'=>"input_text"));
echo $form->error($model,'password'); 
echo '<label for="login">E-mail:</label>';
echo $form->textField($model,'email', array('class'=>"input_text"));
echo $form->error($model,'email'); 
echo CHtml::submitButton('Создать', array('class'=>"btn"));
      
$this->endWidget();

Why is a validation error not displayed for the uniqueness of the field?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vit, 2014-01-15
@GM2mars

At a minimum, you need to specify 'enableAjaxValidation' => true.
enableClientValidation and enableAjaxValidation are not the same thing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question