Last active
January 2, 2016 22:19
-
-
Save Narven/8369080 to your computer and use it in GitHub Desktop.
Yii Model Custom Rules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // rule for date format | |
| array('birth_date', 'date', 'format'=>'yyyy-mm-dd', 'message'=>'Birth Date, wrong format. Must be YYYY-MM-DD', 'allowEmpty'=>false), | |
| // Check for UNIQUE | |
| // USAGE: array('email', 'unique'), | |
| public function unique( $attribute, $params ) | |
| { | |
| if( self::model()->count( 't.email=:email', array( ':email' => $this->attribute ) ) > 0 ) $this->addError('email', 'Email already exists'); | |
| } | |
| // converters attribute to lowercase | |
| public function lowerCase( $attribute, $params ) | |
| { | |
| strtolower( $this->$attribute ); | |
| } | |
| // mandatory checkbox | |
| public function checked( $attribute, $params ) | |
| { | |
| if ( $this->$attribute != 1 ) | |
| { | |
| $this->addError( $attribute, 'Checkbox is mandatory.' ); | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment