Hi guys, i’m trying to make a registration form with my user model…I’m having a difficult validating unique field. My ajax doenst recognize them.
public function rules() {
return array(
array('username, password, confirm_password, email, first_name, last_name, date_born, sex', 'required'),
array('username', 'unique'),
array('email', 'unique'),
array('username, email', 'length', 'max' => 45),
array('email', 'email', 'message' => 'Isso daí não parece um email hein...'),
array('password', 'length', 'min' => 6, 'max' => 45),
array('confirm_password', 'compare', 'compareAttribute' => 'password', 'message' => 'Tá igual não hein'),
array('first_name, last_name', 'length', 'max' => 45),
array('sex', 'length', 'min' => 1, 'max' => 1),
array('username, email, first_name, last_name, description', 'safe', 'on' => 'search'),
);
}
Here are my Model Rules.
<div id="background">
<div class="register-form">
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'register-form',
'action' => '/users/register',
'enableAjaxValidation' => true,
'enableClientValidation' => true,
'clientOptions' => array(
'validateOnChange' => true,
'validateOnSubmit' => true,
),
));
?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model, 'username'); ?>
<?php echo $form->textField($model, 'username', array('size' => 20, 'maxlength' => 45, 'placeholder' => 'Vai ser seu link!')); ?>
<div class="clear"></div>
<?php echo $form->error($model, 'username'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'password'); ?>
<?php echo $form->passwordField($model, 'password', array('size' => 20, 'maxlength' => 45, 'placeholder' => 'Cuidado pra ninguém saber')); ?>
<div class="clear"></div>
<?php echo $form->error($model, 'password'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'confirm_password'); ?>
<?php echo $form->passwordField($model, 'confirm_password', array('size' => 20, 'maxlength' => 45, 'placeholder' => 'Igual a de cima')); ?>
<div class="clear"></div>
<?php echo $form->error($model, 'confirm_password'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'email'); ?>
<?php echo $form->textField($model, 'email', array('size' => 20, 'maxlength' => 45, 'placeholder' => 'something@somethingelse.com')); ?>
<div class="clear"></div>
<?php echo $form->error($model, 'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'first_name'); ?>
<?php echo $form->textField($model, 'first_name', array('size' => 20, 'maxlength' => 45, 'placeholder' => 'Qual seu nome?')); ?>
<div class="clear"></div>
<?php echo $form->error($model, 'first_name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'last_name'); ?>
<?php echo $form->textField($model, 'last_name', array('size' => 20, 'maxlength' => 45, 'placeholder' => 'E sobrenome?')); ?>
<div class="clear"></div>
<?php echo $form->error($model, 'last_name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'date_born'); ?>
<?php
$this->widget('CMaskedTextField', array(
'model' => $model,
'attribute' => 'date_born',
'mask' => '99/99/9999',
'htmlOptions' => array('size' => 20, 'placeholder' => 'dd/mm/aaaa')
));
?>
<div class="clear"></div>
<?php echo $form->error($model, 'date_born'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'sex'); ?>
<?php echo $form->dropDownList($model, 'sex', array('m' => 'Masculino', 'f' => 'Feminino'), array('size' => 1, 'maxlength' => 1)); ?>
</div>
<div class="row-buttons">
<?php echo CHtml::submitButton('Registrar'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
</div>
My form
and my controller
<?php
class UsersController extends Controller {
/**
* @return array action filters
*/
public function filters() {
return array(
'accessControl', // perform access control for CRUD operations
);
}
/**
* Specifies the access control rules.
* This method is used by the 'accessControl' filter.
* @return array access control rules
*/
public function accessRules() {
return array(
array('allow', // allow all users to perform 'create' action
'actions' => array('register'),
'users' => array('*'),
),
array('allow', // allow authenticated user to perform 'selfdelete' and 'update' actions
'actions' => array('selfdelete', 'update'),
'users' => array('@'),
),
array('allow', // allow admins users to perform 'delete'
'actions' => array('delete'),
'users' => Yii::app()->params['admins'],
),
array('deny', // deny all users
'users' => array('*'),
),
);
}
/**
* Creates and saves a new User
*/
public function actionRegister() {
$model = new User;
$this->performAjaxValidation($model);
if (isset($_POST['User'])) {
$model->attributes = $_POST['User'];
if ($model->validate())
if ($model->save()) {
$this->redirect(array('/site/index'));
}
}
}
/**
* Performs the AJAX validation.
* @param CModel the model to be validated
*/
protected function performAjaxValidation($model) {
if (isset($_POST['ajax']) && $_POST['ajax'] === 'create-user-form') {
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
}
The non server validation works fine(like blank fields and invalid email), but my unique validation isn’t working.
Can’t guess why