Yii Framework Forum: How to validate unique field on client-side? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to validate unique field on client-side? unique, validation, validator, rule, rules Rate Topic: -----

#1 User is offline   terrasoff 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 17-July 11

Posted 23 November 2011 - 07:45 AM

Hi, there!
I need to show if user name is not unique by ajax request (before user submit registration form).
How can i do that using validation rules and activeform?

array('username', 'unique', 'message' => UserModule::t("This user's name already exists."))

rule works, but only when user's submited form

i've tried bind own onchange-event on input field.
When I send ajax-request to specify action, it also works
$item = new User;
$item->username = $login;
if (!$item->validate('username',$login))
{
  if ($item->hasErrors())
  {
...

i get 'duplicate user' error by validator!
But when i try to show it in error-field it seems conflict with activeform onchange-event function and not appears

tried options:
'on'=>'scenario'... 'enableClientValidation' => true...
not working :(
has a solution?
0

#2 User is offline   jellysandwich 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 86
  • Joined: 03-May 11

Posted 23 November 2011 - 08:38 AM

This is actually built into Yii already. You don't really need to create any custom code for it.

1) Create model + CRUD using gii.
2) Ensure model has unique validator.
array('username', 'unique', 'message' => UserModule::t("This user's name already exists."))

3) Go into CRUD controller and uncomment ajax validation.
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);

4) Go into view file _form.php and enable ajax validation.
<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'register-form',
    'enableAjaxValidation'=>true,
    'enableClientValidation'=>true,
)); ?>

0

#3 User is offline   terrasoff 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 17-July 11

Posted 23 November 2011 - 10:07 AM

View Postjellysandwich, on 23 November 2011 - 08:38 AM, said:

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'register-form',
    'enableAjaxValidation'=>true,
    'enableClientValidation'=>true,
)); ?>


It's clear...
Ajax validation is working
I need ajax validation of username (check unique rule) working before submit
Strange, but only this rule not responde on 'change' event.
Other rules are working!
array('username', 'unique', 'message' => UserModule::t("This user's name already exists.")),
array('username', 'length', 'max'=>20, 'min' => 3,'message' => UserModule::t("Incorrect username (length between 3 and 20 characters).")),
array('username', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u','message' => UserModule::t("Incorrect symbols (A-z0-9).")),
After form submitted unique-rule also working!
What could be wrong?
0

#4 User is offline   rooney10 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 29-August 12

Posted 29 August 2012 - 08:21 AM

View Postterrasoff, on 23 November 2011 - 10:07 AM, said:

It's clear...
Ajax validation is working
I need ajax validation of username (check unique rule) working before submit
Strange, but only this rule not responde on 'change' event.
Other rules are working!
array('username', 'unique', 'message' => UserModule::t("This user's name already exists.")),
array('username', 'length', 'max'=>20, 'min' => 3,'message' => UserModule::t("Incorrect username (length between 3 and 20 characters).")),
array('username', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u','message' => UserModule::t("Incorrect symbols (A-z0-9).")),
After form submitted unique-rule also working!
What could be wrong?



Has anybody found a solution for that?
0

#5 User is offline   reshmijibi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 13-June 12
  • Location:India

Posted 13 March 2013 - 05:01 AM

Hi any solution im also stuck with the same
0

#6 User is offline   Aneesh Asokan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 240
  • Joined: 26-September 12

Posted 13 March 2013 - 05:28 AM

In controller, action create() check the code like this
$model=new User; 
$this->performAjaxValidation($model);


also check the ajax validation function
protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='user-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}


Post your complete rules.
Thanks.
Thanks
Aneesh.
0

#7 User is offline   reshmijibi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 13-June 12
  • Location:India

Posted 14 March 2013 - 02:08 AM

View PostRvr101, on 13 March 2013 - 05:28 AM, said:

In controller, action create() check the code like this
$model=new User; 
$this->performAjaxValidation($model);


also check the ajax validation function
protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='user-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}


Post your complete rules.
Thanks.



Hi ,

Issue resolved :)
0

#8 User is offline   Aneesh Asokan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 240
  • Joined: 26-September 12

Posted 14 March 2013 - 02:14 AM

Great..!! How it solve?
Thanks
Aneesh.
0

#9 User is offline   reshmijibi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 13-June 12
  • Location:India

Posted 14 March 2013 - 03:00 AM

View PostRvr101, on 14 March 2013 - 02:14 AM, said:

Great..!! How it solve?

Hi,

Actually one of the model form element was not defined properly .
When i fixed that then both ajax and client validation worked properly as specified in the above posts.

:)
0

#10 User is offline   Aneesh Asokan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 240
  • Joined: 26-September 12

Posted 14 March 2013 - 03:06 AM

Good... :)
Thanks
Aneesh.
0

#11 User is offline   rooney10 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 29-August 12

Posted 14 March 2013 - 10:42 AM

For others who may also have a problem with the validation:

I use 2 different models in 1 form. Here the ajax validation function looks as follows:

if(isset($_POST['ajax']) && $_POST['ajax']==='user-form') {
	echo CActiveForm::validate(array($model1, $model2));
	Yii::app()->end();
}


Maybe this helps ;)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users