I have a form that has a unique username field and a unique email field. If I fill out the form with non-unique values for either username or email, the page just reloads but does not show any notification of the errors.
If I fill out the form with unique values, everything works as expected.
The form view:
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'register-form',
'enableClientValidation'=>true,
'clientOptions' => array(
'validateOnSubmit'=>true,
'validateOnChange'=>true,
'validateOnType'=>false,
),
)); ?>
<?php echo $form->errorSummary($model); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->labelEx($model,'username'); ?>
<?php echo $form->textField($model,'username'); ?>
<?php echo $form->error($model,'username'); ?>
</div>
…
<div class="row buttons">
<?php echo CHtml::submitButton('Register'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
The RegisterForm model rules:
public function rules()
{
return array(
// username and password are required
array('username, password, passwordRepeat, email', 'required'),
//Make sure passwords match
array('passwordRepeat', 'compare', 'compareAttribute'=>'password'),
array('username, first_name, last_name', 'length', 'max'=>72),
array('email', 'length', 'min'=>4, 'max'=>72),
array('role', 'length', 'max'=>15),
array('email', 'email'),
// username and pass are unique
array('username', 'unique'),
array('email', 'unique'),
//Image rules
array('image', 'file', 'types'=>'jpg, gif, png', 'allowEmpty'=>true, 'safe'=>true),
array('age_id, genre_id, system_id, communication_id, image', 'safe'),
);
}
Please let me know if you need more information from me.
Edit: As requested, the controller action (I know, it’s currently a mess):
/**
* Displays the register page
*/
public function actionRegister()
{
$model=new RegisterForm;
$newUser = new User;
// if it is ajax validation request
if(isset($_POST['ajax']) && $_POST['ajax']==='register-form')
{
$model->attributes=$_POST['RegisterForm'];
echo CActiveForm::validate($model);
Yii::app()->end();
}
// collect user input data if the form is set.
if(isset($_POST['RegisterForm']))
{
$rnd = rand(0,9999); // generate random number between 0-9999
$model->attributes=$_POST['RegisterForm'];
//Store the user information to the User model
$newUser->username = $model->username;
//hash the password
$hash = crypt($model->password, User::blowfishSalt());
$newUser->password = $hash;
//Create a hash for email activation.
$emailHash = crypt($model->email, User::blowfishSalt());
$newUser->activation_key = $emailHash;
$newUser->email = $model->email;
$newUser->first_name = $model->first_name;
$newUser->last_name = $model->last_name;
$newUser->username == 'admin' ? $newUser->role='admin' : $newUser->role='user';
$newUser->joined = date('Y-m-d');
//Set contact info:
$newUser->steam_id = $model->steam_id;
$newUser->skype_id = $model->skype_id;
$newUser->psn_id = $model->psn_id;
$newUser->xbox_live_id = $model->xbox_live_id;
$newUser->nintendo_id = $model->nintendo_id;
//Set additional profile info:
$newUser->fav_game = $model->fav_game;
$newUser->currently_playing = $model->currently_playing;
$newUser->last_online = time();
//Make the profile able to be displayed
$newUser->display_eligible = 1;
//Profile image handling
$uploadedFile=CUploadedFile::getInstance($model,'image');
if ($uploadedFile)
{
$fileName = "{$rnd}-".time()."-{$uploadedFile}"; // random number + file name
$newUser->image = $fileName;
}
//If the data is saved to the database, log them in and redirect them to previous page.
if($newUser->save())
{
//Save the age UserResponse.
if (isset($model->age_id))
{
$newAgeUserResponse = new UserResponse;
$newAgeUserResponse->createUserResponse($model->age_id, $newUser->id);
}
//Set the genre UserResponse.
if (isset($model->genre_id))
{
$newGenreUserResponse = new UserResponse;
$newGenreUserResponse->createUserResponse($model->genre_id, $newUser->id);
}
//Set the system UserResponse.
if (isset($model->system_id))
{
$newSystemUserResponse = new UserResponse;
$newSystemUserResponse->createUserResponse($model->system_id, $newUser->id);
}
//Set the communication UserResponse.
if (isset($model->communication_id))
{
$newCommunicationUserResponse = new UserResponse;
$newCommunicationUserResponse->createUserResponse($model->communication_id, $newUser->id);
}
//Save the image path.
if (isset($uploadedFile))
{
$uploadedFile->saveAs(Yii::app()->basePath.'/../images/uploads/'.$fileName);
}
//Send the activation email
$activation_url = $this->createAbsoluteUrl('registration/validateKey', array('key'=>$newUser->activation_key));
$newUser->sendActivationEmail($activation_url);
//Log the user in once the data is saved to the database
$identity=new UserIdentity($newUser->username,$model->password);
$identity->authenticate();
Yii::app()->user->login($identity,0);
//redirect the user to page he/she came from
$this->redirect(Yii::app()->user->returnUrl);
}
}
// display the register form with model variable being the $model from this function.
$this->render('register',array('model'=>$model));
}