Skip to content

Instantly share code, notes, and snippets.

@Narven
Last active January 2, 2016 22:19
Show Gist options
  • Select an option

  • Save Narven/8369080 to your computer and use it in GitHub Desktop.

Select an option

Save Narven/8369080 to your computer and use it in GitHub Desktop.
Yii Model Custom Rules
<?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