Created
July 24, 2009 06:42
-
-
Save ichikaway/153894 to your computer and use it in GitHub Desktop.
This file contains 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 | |
class AppModel extends Model { | |
var $withFieldName = false; | |
function beforeValidate(){ | |
foreach( $this->validate as $fieldname => $ruleSet ){ | |
foreach( $ruleSet as $rule => $rule_info ){ | |
if(!empty($this->validate[$fieldname][$rule]['rule'])) { | |
$rule_option = $this->validate[$fieldname][$rule]['rule']; | |
} | |
if( $error_message = $this->_getErrorMessageI18n( $rule ) ) { | |
$this->validate[$fieldname][$rule]['message'] = vsprintf($error_message, $rule_option); | |
}elseif( !empty($this->validate[$fieldname][$rule]['message']) ){ | |
$this->validate[$fieldname][$rule]['message'] = __( $this->validate[$fieldname][$rule]['message'], true); | |
} | |
if( $this->withFieldName && !empty($this->validate[$fieldname][$rule]['message']) ){ | |
$this->validate[$fieldname][$rule]['message'] = __( $fieldname ,true) . ' : ' . $this->validate[$fieldname][$rule]['message']; | |
} | |
} | |
} | |
return true; | |
} | |
function _getErrorMessageI18n( $rule ){ | |
$error_messages = array( | |
'require' => 'Please be sure to input.', | |
'email_invalid' => __('Invalid Email address.',true), | |
'between' => __('Between %2$d and %3$d characters.',true), | |
); | |
if( array_key_exists($rule, $error_messages) ){ | |
return $error_messages[$rule]; | |
}else{ | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment