Work with rules

Now I make form lost password and I have field usernameoremail and I want to check in rules both fields username and email if exists How can I do this ?

I’m unsure exactly what you’re asking because it’s broken English however I’m going to assume that you want to make sure the user has input for both those fields.

We do this with




 return array(

        array('username, email', 'required'),

        // Now if you have it all in one model, such as a user model

        // Then you need to set up scenarios - like so:

        array('username, password', 'required', 'on'=>'login'),

        array('username, password, password_repeat, email', 'required', 'on'=>'register'),

        array('username, email', 'required', 'on'=>'forgotPassword'),

        // This is because sometimes, certain fields won't be required for certain scearios,

        // Like you won't require a password for the forgot password <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/wink.gif' class='bbc_emoticon' alt=';)' />

    );

Then when you instantiate your model in the controller:





public function actionLogin()

{

     $user=new User;

     $user->scenario="login";

     ...

     ...

}


public function actionRegister()

 {

      $user=new User;

      $user->scenario="register";

      ...

      ...

 }


public function actionForgotPassword()

 {

      $user=new User;

      $user->scenario="forgotPassword";

      ...

      ...

 }



Hope that’s what you’re looking for :)

He actually try to have a single input and validate it against both username and e-mail depending on what the user entered.

For this scenario, I would create a form model with one property and without validation rules. Before validation, I’d check whether the input is e-mail or not (you can use CEmailValidator for this) and find user model by the proper attribute.

Hope this helps.

Sorry for my broken English I have one input that has name ‘usernameoremail’ and want to validate

Don’t work

Example


array('usernameoremail','in','range'=>$this->findAll(array('select'=>'username, email')))

Or This


array('username, email','exist','attributeName'=>'usernameoremail')

May be it help you to understand what I mean ? Thank you for answers :)

This specific behavior can’t be implemented in validation rules. I recommend you to extend beforeValidate().

I’m going to have to agree with pestaa, you can’t manipulate the rules setup that way (right now at least). You’ll have to create your own function in terms of evaluating if the single input field is a username or email (if I understand you correctly now).

With beforeValidate() like pestaa suggested, you can create the logic as followed:




public function beforeValidate()

{

     $criteria = new CDbCriteria;

     $conditions = array();

     $conditions[] = "username=".$usernameoremail; 

     $conditions[] = "email=".$usernameoremail;

     $criteria->condition = implode(' OR ', $conditions);


     // Now run your logic of finding the data with your model->find() query

     // After test the result, if it's true take that row, do what you need to, else it's false and the name or email wasn't found.

}



I think that might be what you’re looking for.

Thank you very much for your answers :)

If I understand the problem correctly, then you have a single field that could contain either an email address or a username. You want to validate this field on input. I’d try using a function in a rule:




public function rules()	{

    return array(

        array('usernameoremail', 'isEmailOrUsername'),

    );

}


function isEmailOrUsername($attribute, $params) {

    if ( ! isValiEmail($this->$attribute) && ! isValidUsername($this->$attribute) ) {

        $message = Yii::t('yii', $this->getAttributeLabel($attribute). ' invalid email or user name.');

        $this->addError($attribute, $message);

    }

}



I recommend you to have 2 fields: Email and Username.

In case when you have Username as Email, replace one with another upon form submit.