0

How do I display an error in the form, a flash message or someway, indicating what he did wrong?

The code is something like this:

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
    }
}

I know that returning false prevents the options from being saved, but the user won't know why.

4

1 回答 1

1

Try this one,

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;
    ....
于 2013-06-26T10:13:12.827 回答