Reputation: 6950
I am trying to validate a form against the model in Yii keeing ajax validation on. I want to keep a field unique in my database . The problem I am facing is the ajax message is not getting displayed for the unique validator but for all other rules it is working fine.
Please let me know where I am wrong . Relevant code is posted below
MODEL RULES
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
-----------
------------
array('user_code', 'length', 'max'=>20),
array('user_code','unique','message'=>'This code already exists. Please try different code', 'className' => 'User',
'attributeName' => 'user_code',),
);
}
FORM
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'frm-useraccount',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'action'=>Yii::app()->createUrl('dashboard/index').'#user',
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
));
?>
CONTROLLER
$objMdl = new User;
$isUserSetUpFormSubmitted = (isset($postedData['User']))?true:false;
// handle form submit
if($isUserSetUpFormSubmitted)
{
//validate the model
$isMdlValidated = $objMdl->validate($postedData['user_code']);
if($isMdlValidated)
{
//handle insert
}
else
{
//display model errors
}
}
Upvotes: 0
Views: 469
Reputation: 9357
what he said ^^^. In adition to what Deele said, you are creating a new object without assigning it ever any values. $objMdl->validate($postedData['user_code']);
means validate the attribute $postedData['user_code'] of the model objMdl. It does NOT mean validate the attribute user_code but whatever random stuff you inserted into $postedData['user_code'].
Upvotes: 0
Reputation: 3684
To display errors for specific field, you either need error summary or error field in your form.
Error summary
<?php echo $form->errorSummary($model); ?>
Error field
<?php echo $form->error($model,'user_code'); ?>
In addition to that, you need to set up your controller to return ajax validation result
/**
* Provides output to controller action request "/dashboard/index"
*/
public function actionIndex() {
// Prepare login form
$model = new User('update');
// Perform AJAX validation if required
$this->performAjaxValidation($model, 'frm-useraccount');
// Perform basic validation
if ($model->attributes = $req->getPost('User')) {
if ($model->validate()) {
// Save in DB
$model->save(false);
// Show confirmation for user
Yii::app()->user->setFlash(
'success',
Yii::t(
'site',
'Save successfull.'
)
);
// Refresh
$this->refresh();
}
else {
Yii::app()->user->setFlash(
'error',
CHtml::errorSummary($model)
);
}
}
}
/**
* Creates data validation output for ajax requests with data from given form ID
*/
protected function performAjaxValidation($model, $formId) {
/**
* @var $app CWebApplication
* @var $req CHttpRequest
*/
$app = Yii::app();
$req = $app->getRequest();
if ($req->getIsAjaxRequest() && $req->getPost('ajax') === $formId) {
echo CActiveForm::validate($model);
$app->end();
}
}
In addition to your model and rules, this should be enough, to display error you require.
Upvotes: 1