Question

the scenario is i have a form in my Yii project that will take the username as input and sends the user a mail containing activation link if he has not received one. the form name is ResendActivationLinkForm which extends the CFormModel class. now at the submission time i wanna check if the username exists or not in the database using AJAX...How to use yii's own classes and functions to accomplish that?

Was it helpful?

Solution

well thanks for replies..but i got it in a simpler fashion. i just added an array inside the form model ResendActivationLinkForm depicting my rule.. eg..

public function run(){
return array(
.....
.....
 array('username','exist','className'=>'User'),
);
}

where username is my attributeName, exist is the validator alias and className is the Model class name whose attribute it should look for...

you can look at http://www.yiiframework.com/wiki/56/ for more details. :) Happy Coding. :)

OTHER TIPS

If Usermodel is the model that has a username attribute, you can do something like this.

if(Usermodel::model()->findByAttributes(
                            array(),
                            'username = :inputtedUsername',
                            array(':inputtedUsername' => $_POST['username'],)))
{ 
//user found    
}else{
//user not found
}

For more information about the various CActiveRecord methods check the website

I'd extend the form model with a custom validator, such as array('username','UsernameExistsValidator'). This validator class should extend CValidator and implement public function validateAttribute($object,$attribute) which can use $this->addError() to flag if the username is not available. See the source for other Yii validators if you need some more input on the bits in there.

The logic from the answer above would fit into validateAttribute,

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top