Yii Framework Forum: Create Rule To Validate Multiple Attribulte At Once? - Yii Framework Forum

Jump to content

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

Create Rule To Validate Multiple Attribulte At Once? Rate Topic: -----

#1 User is offline   clonevn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 111
  • Joined: 25-March 13

Posted 19 May 2013 - 10:23 AM

Hi I need to validate 2 attribute based on 1 special rule. Basically new rule looks like:
        public function myRule($attribute)
        {         
            if(//work here)
              $this->addError($attribute, 'Wrong');
        } 


I would like to do something more complex:
        public function myRule($attribute1,$attribute2)
        {         
            if(//work here with both 2 attributes)
              $this->addError($attribute1, 'Wrong1');
              $this->addError($attribute2, 'Wrong2');
        } 

But it doesnt work. Any better way to mix 2 attributes for one validation?
0

#2 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,165
  • Joined: 05-January 12
  • Location:Morocco

Posted 19 May 2013 - 10:47 AM

You mean like:

    public function rules()
    {
        return array(
            array('attribute1, attribute2', 'myRule'),
        );
    }

    public function myRule($attribute, $params)
    {
        if ($someCondition) {
            $this->addError($attribute, 'Error');
        }
    }

0

#3 User is offline   clonevn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 111
  • Joined: 25-March 13

Posted 19 May 2013 - 11:03 AM

Sorry, it should be like that:
    public function rules()
    {
        return array(
            array('attribute1, attribute2', 'myRule'),
        );
    }

    public function myRule($attribute, $params)
    {
       // $attribute1 do stuff with $attribute2 -> $newStuff
        if (//$newStuff blah blah here) {
            $this->addError($attribute, 'Error');
        }
    }

The question is how do I know which ones is attribute1 or atrribute2 in these code in order to do stuff.
0

#4 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,165
  • Joined: 05-January 12
  • Location:Morocco

Posted 19 May 2013 - 11:07 AM

If I understand, you want to access the values of your current model instance attributes in your rule in order to validate these attributes. Right?

So you could use $this that represents the model instance. And $this->attribute1 and $this->attribute2 give you those values.
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