Skip to content

Instantly share code, notes, and snippets.

@k1LoW
Forked from ichikaway/gist:153894
Created July 24, 2009 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k1LoW/153897 to your computer and use it in GitHub Desktop.
Save k1LoW/153897 to your computer and use it in GitHub Desktop.
<?php
class AppModel extends Model {
var $withFieldName = false;
var $error_messages = array();
function _setErrorMessageI18n() {
$this->error_messages['empty'] = __('Error Hoge',true);
}
function beforeValidate(){
$this->_setErrorMessageI18n();
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 ){
if( array_key_exists($rule, $this->error_messages) ){
return $this->error_messages[$rule];
}else{
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment