Yii Framework Forum: Validation order changed since version 1.0? - Yii Framework Forum

Jump to content

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

Validation order changed since version 1.0? Rate Topic: -----

#1 User is offline   Ilnore 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 31-July 09

Posted 26 July 2010 - 10:48 AM

Hi,

I'm refactoring a bunch of my own code from 1.0.12 to 1.1.3.

Now then, I used to be able to create validation rules on the fly, and depending a bit on class attributes. This doesn't seem to work anymore; has validate() become somehow static?

To clarify, pseudocode:

public function rules()
{
   return array(
      array('someAttribute', 'in', 'range'=>$this->createSuitableRange()),
   );
}


So, previously at least, in $this->createSuitableRange() I was able to rely on the attribute values set for $this object. Now as I've changed to 1.1, I'm having trouble doing this. Instead, $this object appears to have empty attribute values within the rules() function.

Is rules() called from a static context after all while validating in 1.1? Documentation seems to claim it non-static.


Thanks.
0

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 27 July 2010 - 02:45 AM

The validator objects only get created on first access. This can happen quite early, e.g. when you do a massive assignment. Then the safe attributues are searched which in the end triggers createValidators(). It returns a CList of all Validators.

So one idea of a dirty workaround: In beforeValidate you use getValidatorList(), clear the CList, create another list from createValidators() and merge this list back to the first CList. A bit clumsy, though...

Maybe you find a better solution. You should check the source of CModel.
1

#3 User is offline   Ilnore 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 31-July 09

Posted 27 July 2010 - 03:13 AM

Ugh, kind of dirty, yes, but seems to do the trick. Thanks for the heads-up!

I'll go with this for a while, as there's only this one place I need it in. Might look for a better solution if I need to do it again.
0

#4 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 27 July 2010 - 03:20 AM

Method-based validator should work too. It will look like:

if (!in_array($this->someAttribute, $this->createSuitableRange()))
    $this->addError('my error');

1

#5 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 27 July 2010 - 04:11 AM

Agree, method based is much better here. Maybe you can refactor your createSuitableRange() right into a validator method.
0

#6 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 27 July 2010 - 06:46 AM

Take a look at this thread.

Is a bit tedious but there are some good tips
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