使用设置API验证提交的表单值并显示警告消息

时间:2012-08-20 作者:Teno

我正在用设置API制作插件管理页面。我想知道一种在用户提交无效值时显示警告消息的方法。

例如,使用下面的代码,如果用户为选项A(第一个输入字段)发送一个空值,我想显示一条消息,说明它无效,请填写表单。其他提交的选项值不应保存到数据库中,而应与页面上的消息一起保留在输入字段中。

<?php
/*
Plugin Name: Demo Verification Notice with Settings API
Description: Learning how to warn invalid values when submitting the form and take back the user to the setting page without saving the options.
Author: Teno
*/

add_action(\'admin_init\', \'settingsapi_init\');
function settingsapi_init(){
    register_setting( \'settingsapi_optiongroupname\', \'settingsapi_optionname\');
    add_settings_section(\'plugin_main\', \'Section 1\', \'settingsapi_sectiondescription\', \'settingsapi_pageslug\');
    add_settings_field(\'plugin_text_string_a\', \'Option A\', \'settingsapi_setting_string_a\', \'settingsapi_pageslug\', \'plugin_main\');
    add_settings_field(\'plugin_text_string_b\', \'Option B\', \'settingsapi_setting_string_b\', \'settingsapi_pageslug\', \'plugin_main\');
}

function settingsapi_sectiondescription() {
    echo \'<p>This is a section description.</p>\';
}
function settingsapi_setting_string_a() {
    $options = get_option(\'settingsapi_optionname\');
    echo "<input id=\'plugin_text_string\' name=\'settingsapi_optionname[option_a]\' size=\'40\' type=\'text\' value=\'{$options[\'option_a\']}\' />"; 
} 
function settingsapi_setting_string_b() {
    $options = get_option(\'settingsapi_optionname\');
    echo "<input id=\'plugin_text_string\' name=\'settingsapi_optionname[option_b]\' size=\'40\' type=\'text\' value=\'{$options[\'option_b\']}\' />"; 
}
// admin menu
add_action(\'admin_menu\', \'plugin_admin_add_page\');
function plugin_admin_add_page() {
    add_options_page(\'Custom Plugin Page\', \'Demo Settings API Menu for Verification\', \'manage_options\', \'settingsapi_pageslug\', \'settingsapi_adminpage\');
}

function settingsapi_adminpage() {
    ?>
    <div class="wrap">
        <?php screen_icon(); ?> <h2>Demo Plugin for Settings API</h2>
        <form action="options.php" method="post">
            <?php settings_fields(\'settingsapi_optiongroupname\'); ?>
            <?php do_settings_sections(\'settingsapi_pageslug\'); ?>
            <?php submit_button(); ?>
        </form>
    </div>
    <?php
}
?>
设置API是否可以实现这一点?

register\\u setting()函数接受第三个参数作为验证函数调用。但是,它可以用于修复传递的值,但它接受表单提交并保存选项。这不是我想要的。

[编辑]

好了,我快到了。这将使用register\\u setting()函数中设置的回调函数验证提交的值。如果该值无效,它会在选项数组中留下一个标志invalid,并显示一条警告消息。

然而,还有一个问题。否则,就几乎完成了。

将空值设置为第一个输入选项后,将显示警告消息。这就是我们迄今为止所取得的成就。如果用户离开显示警告的页面,则会将空值保存到选项中。它应该恢复以前的值下面的代码可以用作插件。如果你运行它,你就会明白我在说什么。

<?php
/*
Plugin Name: Demo Verification Notice with Settings API
Description: Learning how to warn invalid values when submitting the form and take back the user to the setting page without saving the options.
Author: Teno
*/

add_action(\'admin_init\', \'settingsapi_init\');
function settingsapi_init(){
    register_setting( \'settingsapi_optiongroupname\', \'settingsapi_optionname\',\'validation_callback\');

    add_settings_section(\'plugin_main\', \'Section 1\', \'settingsapi_sectiondescription\', \'settingsapi_pageslug\');
    add_settings_field(\'plugin_text_string_a\', \'Option A\', \'settingsapi_setting_string_a\', \'settingsapi_pageslug\', \'plugin_main\');
    add_settings_field(\'plugin_text_string_b\', \'Option B\', \'settingsapi_setting_string_b\', \'settingsapi_pageslug\', \'plugin_main\');
}

function settingsapi_sectiondescription() {
    echo \'<p>This is a section description.</p>\';
}
function settingsapi_setting_string_a() {
    $options = get_option(\'settingsapi_optionname\');
    ?>
    <input type="hidden" name="settingsapi_optionname[submitted]" value="1" />
<? print_r($options); ?><br />
    <input id="plugin_text_string" name="settingsapi_optionname[preview][option_a]" size="40" type="text" value="<?php echo $options[\'submitted\']  ? $options[\'preview\'][\'option_a\'] : $options[\'valid\'][\'option_a\']; ?>" />
    <?php echo ($options[\'invalid\'] && $options[\'submitted\'] ) ? \'<font color="red">* cannot be blank.</font>\' :\'\' ?>
    <?php
} 
function settingsapi_setting_string_b() {
    $options = get_option(\'settingsapi_optionname\');
    ?>
<? print_r($options); ?><br />  
    <input id="plugin_text_string" name="settingsapi_optionname[preview][option_b]" size="40" type="text" value="<?php echo $options[\'submitted\'] ? $options[\'preview\'][\'option_b\'] : $options[\'valid\'][\'option_b\']; ?>" />
    <?php
}
// admin menu
add_action(\'admin_menu\', \'plugin_admin_add_page\');
function plugin_admin_add_page() {
    add_options_page(\'Custom Plugin Page\', \'Demo Settings API Menu for Verification\', \'manage_options\', \'settingsapi_pageslug\', \'settingsapi_adminpage\');
}

function settingsapi_adminpage() {
    ?>
    <div class="wrap">
        <?php screen_icon(); ?> <h2>Demo Plugin for Validation Message with Settings API</h2>

        <?php
        $options = get_option(\'settingsapi_optionname\');
        print_r($options);      // for debugging purpose
        if (!($options[\'invalid\'] && $options[\'submitted\'])) {

            // update the correct option values
            $options[\'valid\'] = is_array($options[\'valid\']) ? $options[\'valid\'] : array();  // if the options[\'valid\'] is called for the first time, make it an array
            $options[\'valid\'] = array_merge($options[\'valid\'], $options[\'preview\']);
            // $options[\'preview\'] = \'\';    // initialize the preview option data. //<-- this seems to cause a problem
            update_option(\'settingsapi_optionname\', $options);
        }
        ?>

        <form action="options.php" method="post">
            <?php 
            settings_fields(\'settingsapi_optiongroupname\');
            do_settings_sections(\'settingsapi_pageslug\'); 

            // after displaying the form content, set the invalid flag and the subbmited flag to false
            $options[\'invalid\'] = False;
            $options[\'submitted\'] = False;      
            update_option(\'settingsapi_optionname\', $options);  

            submit_button();
            ?>          
        </form>

    </div>
        <?php 
            print_r($options);
        ?>  
    <?php
}

function validation_callback($input){
    if (strlen(trim($input[\'preview\'][\'option_a\'])) == 0 && $input[\'submitted\']) {
        // $input[\'preview\'][\'option_a\'] = \'\';
        unset($input[\'preview\'][\'option_a\']);
        $input[\'invalid\'] = True;   // <-- this one is the problem. It seems to be called multiple times and setting the invalid flag to true when it\'s not necessary.
        add_settings_error(\'unique_identifyer\',esc_attr(\'settings_updated\'),__(\'Settings NOT saved.\'),\'error\');
        add_action(\'admin_notices\', \'print_errors\');
    } else {
        $input[\'invalid\'] = False;
    }
    return $input;
}

function print_errors(){
    settings_errors( \'unique_identifyer\' );
}

?>
[编辑]

如果没有设置API,可能更容易实现。下面是一个工作示例。

<?php
/*
Plugin Name: Demo Verification Warning without Settings API
Description: Demo plugin demonstrating how to display a warning message when the user tries to save an invalid value in the option page.
Author: Teno
*/

// define constants
define("DEMOVERIFICATIONKEY", "demo_verification_option");

// create an option array
$arrOption = get_option(DEMOVERIFICATIONKEY);
if (!(is_array($arrOption))) {
    $arrOption = array(
        "tab1" => array(),
        "tab2" => array(),
        "tab3" => array()
    );
    update_option(DEMOVERIFICATIONKEY, $arrOption);
}

// admin menu
add_action(\'admin_menu\', \'demo_verification_adminmenu\');
function demo_verification_adminmenu() {
    add_options_page(\'Demo Plugin Menu for Verification without Settings API\', \'Demo Plugin Menu for Verification without Settings API\', \'manage_options\', \'demo_verification_admin_menu\', \'demo_verification_adminpage\');
}
// admin page
function demo_verification_adminpage() {
    $options = get_option(DEMOVERIFICATIONKEY);
    print_r($_POST); // for debugging
    echo \'<br />\';
    if(isset($_POST[DEMOVERIFICATIONKEY][\'tab1\'][\'submitted\']) && $_POST[DEMOVERIFICATIONKEY][\'tab1\'][\'submitted\'] == 1){
        if (strlen(trim($_POST[DEMOVERIFICATIONKEY][\'tab1\'][\'option_a\'])) == 0) {
            $options[\'tab1\'][\'invalid\'] = True;     
            echo \'<div class="error settings-error"><p>Options are not saved.</p></div>\';
        } else {    
            $options = array_merge($options, $_POST[DEMOVERIFICATIONKEY]);
            update_option(DEMOVERIFICATIONKEY, $options);
            echo \'<div class="updated"><p>Updates are saved.</p></div>\';
        }
    }   
    ?>
    <div class="wrap">
        <?php screen_icon(); ?> <h2>Demo Plugin for Validation Message without Settings API</h2>
        <form method="post" action="">  <?php // \'action=""\' means to send the form post data to this page itself. ?>
            <input type="hidden" name="<?php echo DEMOVERIFICATIONKEY; ?>[tab1][submitted]" value="1" />
            <table class="form-table">
                <tbody>
                <tr valign="top">
                    <th scope="row">Option A</th>
                    <td>
                        <input name="<?php echo DEMOVERIFICATIONKEY; ?>[tab1][option_a]" size="40" type="text" value="<?php echo $options[\'tab1\'][\'invalid\']  ? $_POST[DEMOVERIFICATIONKEY][\'tab1\'][\'option_a\'] : $options[\'tab1\'][\'option_a\']; ?>" />
                        <?php echo ($options[\'tab1\'][\'invalid\'] && $options[\'tab1\'][\'invalid\'] ) ? \'<font color="red">* cannot be blank.</font>\' :\'\' ?>
                    </td>
                </tr>
                <tr valign="top">
                    <th scope="row">Option B</th>
                    <td>
                        <input name="<?php echo DEMOVERIFICATIONKEY; ?>[tab1][option_b]" size="40" type="text" value="<?php echo $options[\'tab1\'][\'invalid\'] ? $_POST[DEMOVERIFICATIONKEY][\'tab1\'][\'option_b\'] : $options[\'tab1\'][\'option_b\']; ?>" />
                    </td>
                </tr>
                </tbody>
            </table>
            <?php submit_button(); ?>   
        </form>     
    </div>
    <?php
}
?>
但如果有人能用设置API解决这个问题,请发布解决方案。

2 个回复
SO网友:Bainternet

首先将验证回调添加到register_settings:

register_setting( \'settingsapi_optiongroupname\', \'settingsapi_optionname\',\'validation_callback\');
然后定义validation\\u回调:

function validation_callback($input){
    //check if all is good
    //If so then return the wanted value to be saved
    //If Not then hook your admin notice function and return null to be saved ex:
    if ($something){
        return $input;
    }else{
        add_settings_error(\'unique_identifyer\',esc_attr(\'settings_updated\'),__(\'Settings saved.\'),\'updated\');
        add_action(\'admin_notices\', \'print_errors\');
        return null;
    }
}
最后定义print\\u错误如下:

function print_errors(){
    settings_errors( \'unique_identifyer\' );
}
如果什么都没有发生,那么尝试添加include_once \'options-head.php\'; 在您的页面中

Update下面是一个插件,可以测试它是否正常工作:

<?php
/*
Plugin Name: wpse62418
Plugin URI: http://en.bainternet.info
Description: answer to http://wordpress.stackexchange.com/questions/62418/verify-submitted-form-values-and-show-warning-messages-with-setting-api/62434
Version: 1.0
Author: Bainternet
Author URI: http://en.bainternet.info
*/


add_action(\'admin_init\', \'settingsapi_init\');
function settingsapi_init(){
    register_setting( \'settingsapi_optiongroupname\', \'settingsapi_optionname\',\'validation_callback\');       //

    add_settings_section(\'plugin_main\', \'Section 1\', \'settingsapi_sectiondescription\', \'settingsapi_pageslug\');
    add_settings_field(\'plugin_text_string_a\', \'Option A\', \'settingsapi_setting_string_a\', \'settingsapi_pageslug\', \'plugin_main\');
}

function settingsapi_sectiondescription() {
    echo \'<p>This is a section description.</p>\';
}
function settingsapi_setting_string_a() {
    $options = get_option(\'settingsapi_optionname\');
    ?>
    <input type="hidden" name="settingsapi_optionname[submitted]" value="1" />
    <input id="plugin_text_string" name="settingsapi_optionname[preview][option_a]" size="40" type="text" value="<?php echo isset( $options[\'preview\'][\'option_a\']) ? $options[\'preview\'][\'option_a\'] : \'\'; ?>" />
    <?php
} 
// admin menu
add_action(\'admin_menu\', \'plugin_admin_add_page\');
function plugin_admin_add_page() {
    add_options_page(\'Custom Plugin Page\', \'Demo Settings API Menu for Verification\', \'manage_options\', \'settingsapi_pageslug\', \'settingsapi_adminpage\');
}

function settingsapi_adminpage() {
    ?>
    <div class="wrap">
        <?php screen_icon(); ?> <h2>Demo Plugin for Validation Message with Settings API</h2>

        <?php
        $options = get_option(\'settingsapi_optionname\');
        print_r($options);      // for debugging purpose
        ?>

        <form action="options.php" method="post">
            <?php 
            settings_fields(\'settingsapi_optiongroupname\');
            do_settings_sections(\'settingsapi_pageslug\'); 

            // after displaying the form content, set the invalid flag and the subbmited flag to false

            submit_button(); 
            ?>
        </form>

    </div>
        <?php 
            print_r($options);
        ?>  
    <?php
}

function validation_callback($input){
    add_settings_error(\'unique_identifyer\',esc_attr(\'settings_updated\'),__(\'Settings NOT saved.\'),\'error\');
    add_action(\'admin_notices\', \'print_errors\');
    return $input;
}

function print_errors(){
    settings_errors( \'unique_identifyer\' );
}

SO网友:Chip Bennett

一旦调用了验证回调,您就太晚了,无法进行表单验证,因为表单已经提交。在提交表单之前,您可能需要查看页面上发生的javascript表单验证解决方案。

结束

相关推荐

Comment form validation

如何设置注释字段的验证规则?我更改了评论者姓名/电子邮件/主页onmouseover和onblur的值(我使用它而不是标签-因此如果字段为空,它会显示“您的电子邮件”、“您的主页”等)。问题是,在提交时,它会在主页字段中提交此文本(因为它没有验证,而不像电子邮件字段,如果您输入了除[email protected]).如何验证主页字段?