Yii Framework Forum: Method-based validators - Yii Framework Forum

Jump to content

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

Method-based validators Rate Topic: -----

#1 User is offline   Drewniacki 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 19-July 10
  • Location:Gdańsk, Poland

Posted 22 July 2010 - 01:41 AM

Where I can read more about method-based validators? (Was looking in Guide and Class reference but found nothing.) How do I return an error message if validation is negative?
0

#2 User is offline   Mike 

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

Posted 22 July 2010 - 02:47 AM

If you create a webapp you already have an example in protected/models/LoginForm. authenticate is a method based validator. Errors are added with addError() inside such a method.
1

#3 User is offline   Drewniacki 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 19-July 10
  • Location:Gdańsk, Poland

Posted 22 July 2010 - 04:04 AM

Thanks a lot!

May I ask you also about class-based validators? I was trying to wite a specific class for validation but it simply... does not validate my data. It allows any value specified (no PHP exception, nothing).

class NUserValidator extends CValidator
{
	public $message = 'This value is not allowed';

	protected function validateAttribute($object, $attribute)
	{
		if (!(NUser::model()->getUserId($object->$attribute)))
			$this->addError($object, $attribute, $this->message);
	}
	
}


in model:
array('authorUsername', 'application.components.validators.NUserValidator')

0

#4 User is offline   Mike 

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

Posted 22 July 2010 - 04:23 AM

The class looks o.k. to me. Maybe first hardcode the addError() (like: always add the error, no matter which condition). If this works, check your validation condition.
1

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