如何在表单,flash消息或某些方面显示错误,表明他做错了什么?
代码是这样的:
class My_Widget extends WP_Widget {
public function update( $new_instance, $old_instance ) {
if (!valid($new_instance))
// How do I notify the user with a custom message
return false;
else
return $new_instance
}
}
我知道返回false会阻止保存选项,但用户不知道原因。
答案 0 :(得分:1)
试试这个,
public function update( $new_instance, $old_instance ) {
$old_instance['errors'] = array();
if (!valid($new_instance)) {
$$old_instance['errors']['myfield'] = 'Custom error mesasage goes here';
// How do I notify the user with a custom message
return false;
//return $old_instance;
}
else
return $new_instance
}
public function form($instance) {
$myfieldMsg = (isset($instance['errors']) && isset($instance['errors']['myfield'])) ? $instance['errors']['myfield']) : null;
echo $myfieldMsg;
....