在datamapper中,您可以使用$u->error_message('custom', 'This is a custom error message.');
在保存时验证失败时设置错误消息。
但是,我希望能够使用此方法返回消息,即使在验证没有严格失败的情况下 - 例如保存后的警告或状态消息,我想返回给用户。
任何人都知道实现这一目标的方法吗?
答案 0 :(得分:0)
成功后,您无法简单地将值解析为视图文件吗?
<强>控制器:强>
/* Validated code */
/* Updates / Inserts */
$data['success_update'] = TRUE;
查看:强>
<? if ( ! empty($success_update)): ?>
<div id="success_update">
Thingie updated successfully
</div>
<? endif; ?>
答案 1 :(得分:0)
感谢您的回复,通过查看datamapper源代码,我发现当$ item-&gt; error-&gt; all不为空时,验证失败。因此,我可以在datamapper库中添加一个与$ item-&gt; error_message()完全相同的方法,除了填充$ item-&gt; error-&gt; all数组。像这样:
/**
* Status Message
*
* Adds an status message to this objects error object.
*
* @param string $field Field to set the error on.
* @param string $error Error message.
*/
public function status_message($field, $error)
{
if ( ! empty($field) && ! empty($error))
{
// Set field specific value
$this->error->{$field} = $this->error_prefix . $error . $this->error_suffix;
// Add field error to errors all list
// $this->error->all[$field] = $this->error->{$field};
// Append value to error message string
$this->error->string .= $this->error->{$field};
}
}