【Fuel】Validationクラスのget_messageの前後に任意のタグを設定する
FuelPHPのValidationクラスでget_messageメソッドでエラーを出力するときに
前後にデフォルトで指定のタグを付加する方法。
といっても\fuel\core\classes\validation\error.phpをのぞいたら
ドキュメントに無い設定項目があったのでそれをconfigに設定するだけ。
※試しているバージョンは1.7.2
■fuel/core/classes/validation/error.php
/**
* Get Message
*
* Shows the error message which can be taken from loaded language file.
*
* @param string HTML to prefix error message
* @param string HTML to postfix error message
* @param string Message to use, or false to try and load it from Lang class
* @return string
*/
public function get_message($msg = false, $open = '', $close = '')
{
$open = empty($open) ? \Config::get('validation.open_single_error', '') : $open;
$close = empty($close) ? \Config::get('validation.close_single_error', '') : $close;
if ($msg === false and ! ($msg = $this->field->get_error_message($this->rule)))
{
if (is_null($msg))
{
$msg = $this->field->fieldset()->validation()->get_message($this->rule);
}
if ($msg === false)
{
$msg = \Lang::get('validation.'.$this->rule) ?: \Lang::get('validation.'.\Arr::get(explode(':', $this->rule), 0));
}
}
if ($msg == false)
{
return $open.'Validation rule '.$this->rule.' failed for '.$this->field->label.$close;
}
// only parse when there's tags in the message
return $open.(strpos($msg, ':') === false ? $msg : $this->_replace_tags($msg)).$close;
}
■fuel/app/config/config.php
・・・
/**
* Validation settings
*/
'validation' => array(
/**
* Wether to fallback to global when a value is not found in the input array.
*/
// 'global_input_fallback' => true,
'open_single_error' => '<div class="error">',
'close_single_error' => '</div>',
),
・・・
これでget_messageのたびにタグを渡さなくて済む。