Good morning all,
I’ve followed this http://www.yiiframework.com/doc/api/CActiveForm#enableAjaxValidation-detail and http://www.yiiframework.com/forum/index.php/topic/7940-performajaxvalidation-in-cactiveform/ , I tried to use ajax validation including unique rule in certain model attribute. Please look at my code below.
This is my model rules
public function rules() {
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('fullName, email, password, username', 'required'),
array('fullName', 'length', 'max' => 25),
array('username', 'length', 'max' => 15),
array('email, password', 'length', 'max' => 60),
array('password', 'length', 'min' => 6, 'max' => 60),
array('email', 'email', 'checkMX' => true),
array('email', 'unique', 'message' => 'Email is already used'),
array('username', 'unique', 'message' => 'Username is already taken'),
array('username', 'match', 'pattern' => "/^[A-Za-z0-9_]+$/"),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, fullName, username, email, password, registerDate', 'safe', 'on' => 'search'),
);
}
This is my view
<?php
$formID = 'register_right';
$form = $this->beginWidget('CActiveForm', array(
'id' => $formID,
'method' => 'POST',
'action' => 'register',
'enableAjaxValidation' => true,
'enableClientValidation' => true,
'clientOptions' => array(
'validateOnSubmit' => true,
'validateOnChange' => true,
'validateOnType' => true
),
));
?>
<div class="row">
<?php $form->error($model, "fullName"); ?>
<?php echo $form->textField($model, 'fullName', array('value' => 'Full name', 'class' => 'text_field text_field_register', 'autocomplete' => 'off')); ?>
</div>
<div class="row">
<?php $form->error($model, "email"); ?>
<?php echo $form->textField($model, 'email', array('value' => 'Email', 'class' => 'text_field text_field_register', 'autocomplete' => 'off')); ?>
</div>
<div class="row">
<?php $form->error($model, "password"); ?>
<?php echo $form->passwordField($model, 'password', array('value' => 'Password', 'class' => 'text_field text_field_register', 'autocomplete' => 'off')); ?>
</div>
<div class="row">
<?php $form->error($model, "username"); ?>
<?php echo $form->textField($model, 'username', array('value' => 'Username', 'class' => 'text_field text_field_register', 'autocomplete' => 'off')); ?>
</div>
<div class="row buttons">
<?php
echo CHtml::ajaxSubmitButton('Register', 'register', array(
'update' => '#error-panel',
'type' => 'POST',
'data' => 'js:$("#' . $formID . '").serialize()',
'dataType' => 'json',
'success' => 'js:function(data){
if(data.status){
location.href = "getstarted/invite";
}
else{
$("#register_error").html(data.message);
$("#register_error").show();
}
}'
), array('class' => 'button_1', 'value' => 'Sign up', 'id' => 'register_button'));
?>
</div>
<div id="register_error"></div>
<?php $this->endWidget(); ?>
And this is my controller
public function actionIndex() {
$user = new User;
$this->performAjaxValidation($user);
$this->layout = "layout_anonymous";
$this->render('anonymous', array('model' => $user));
}
protected function performAjaxValidation($model) {
if (isset($_POST['ajax'])/* && $_POST['ajax'] === 'user-form'*/) {
var_dump($_POST);
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
The client validation works well. But I didn’t see if there is any ajax validation executed, because when i tried to input duplicate attribute, there are no error shown.
If you have any idea, please let me know.
Thx all