Yii Framework Forum: Creating custom validation rule - Yii Framework Forum

Jump to content

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

Creating custom validation rule custom validation validators Rate Topic: -----

#1 User is offline   CoLT 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 246
  • Joined: 22-November 09

Posted 06 May 2010 - 10:12 AM

Hello,

I have one problem, in Create view I need to check two date attributes.
SBeg(Begin) must be earlier date than SEnd(End).

How would this validator look like?

Thanks for help in advance!

CoLT
0

#2 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,654
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 06 May 2010 - 10:33 AM

View PostCoLT, on 06 May 2010 - 10:12 AM, said:

Hello,

I have one problem, in Create view I need to check two date attributes.
SBeg(Begin) must be earlier date than SEnd(End).

How would this validator look like?

Thanks for help in advance!

CoLT


I would consider to use CCompareValidator (and extend it if neccessary). There's a builtin comparison 'operator' attribute.

(not tested)
/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#3 User is offline   Junior - df9 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 416
  • Joined: 24-May 09
  • Location:Brazil

Posted 06 May 2010 - 10:43 AM

View PostCoLT, on 06 May 2010 - 10:12 AM, said:

Hello,

I have one problem, in Create view I need to check two date attributes.
SBeg(Begin) must be earlier date than SEnd(End).

How would this validator look like?

Thanks for help in advance!

CoLT


if you want to perform the validation in the view, you should use javascript but if you want to perform the validation in your model or activeform I think that the following links will help you:

by using comparevalidator
http://www.yiiframew...ompareValidator

or declaring your own function
http://www.yiiframew...alidation-rules

also, take a look at the pdf file cheat sheet for validators, here
http://www.yiiframework.com/doc/

hope it helps

:)

regards!!
______________________________________
Junior
df9.com.br
Linux Registered User #364954
GNU/Linux: together we're ready!
1

#4 User is offline   CoLT 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 246
  • Joined: 22-November 09

Posted 06 May 2010 - 11:11 AM

View Posttri, on 06 May 2010 - 10:33 AM, said:

I would consider to use CCompareValidator (and extend it if neccessary). There's a builtin comparison 'operator' attribute.

(not tested)
/Tommy

Oh really in this case its already done ;)

If anyone needs a sample:
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
...
						array('SEnd','compare','compareAttribute'=>'SBeg','operator'=>'>', 'allowEmpty'=>false , 'message'=>'{attribute} must be greater than "{compareValue}".', 'on'=>'create, update'),
...
		);
	}


Thanks for good advice :D

By the way, I need some custom validator in more complex situation too.

Like: When adding a record in Course(table) I need to check whether Trainer and Branch does not have any courses in time range of Course length (I use two attributes for this CrTime (date) (hh:mm:ss) and CrLength (int) (minutes)).

I believe I should do a check first for either Trainer or Branch to find out whether there is a time Gap and new inserted course would not duplicate time schedule. Mostly it should be more an advice error, because in business rule - two Courses may be on same time in same Branch - so the user decide to leave the inserted record or fix until error stops bugging.

Any ideas, examples? I appreciate your help and hope these examples should be put in Cookbook "Creating custom Validator"

[edit] @scoob.junior
Yep already looked up those links;) practical examples in manipulating validation from two or more models would be good article in cookbook. As for the beginning simple custom validation should also drag user to know Yii deeper ;)


Regards,
CoLT

This post has been edited by CoLT: 06 May 2010 - 11:53 AM

0

#5 User is offline   schmunk 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 581
  • Joined: 02-November 08
  • Location:Stuttgart, Germany

Posted 07 May 2010 - 02:12 PM

Hi CoLT,

here's a custom validator from phundament:
http://code.google.c...ayValidator.php

Best regards,
schmunk
Phundament - Yii Application Boilerplate with composer support
Fork on github

Follow phundament on Twitter

DevSystem: Mac OS X 10.7 - PHP 5.3 - Apache2 - Yii 1.1 / trunk - Firefox or Safari
0

#6 User is offline   CoLT 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 246
  • Joined: 22-November 09

Posted 07 May 2010 - 03:24 PM

View Postschmunk, on 07 May 2010 - 02:12 PM, said:

Hi CoLT,

here's a custom validator from phundament:
http://code.google.c...ayValidator.php

Best regards,
schmunk

Very good ;) Where do you use it?? (code sample)

Regards,
CoLT
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