Skip to content

Instantly share code, notes, and snippets.

@ichikaway
Created July 24, 2009 06:42
Show Gist options
  • Save ichikaway/153894 to your computer and use it in GitHub Desktop.
Save ichikaway/153894 to your computer and use it in GitHub Desktop.
<?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