WooCommerce 自定义设置选项卡验证 [英] WooCommerce custom settings tab validation

查看:36
本文介绍了WooCommerce 自定义设置选项卡验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WooCommerce 中使用自定义设置选项卡时,例如:

When using a custom settings tab in WooCommerce, like:

add_filter('woocommerce_settings_tabs_array', 'add_my_custom_tab', 50);

function add_my_custom_tab($settings_tabs) {
    $settings_tabs['my_custom_tab'] = __('My Custom Tab', 'my-custom-tab');
    return $settings_tabs;
}

add_action('woocommerce_settings_tabs_my_custom_tab', 'my_custom_tab');

function my_custom_tab() {
    woocommerce_admin_fields(get_custom_settings());
}

add_action('woocommerce_update_options_my_custom_tab', 'update_my_custom_tab_settings');

function update_my_custom_tab_settings() {
    woocommerce_update_options(get_custom_settings());
}

function get_custom_settings() {
    $settings = array(
        'section_title' => array(
            'name' => __('Custom Options', 'woocommerce-my-custom-tab'),
            'type' => 'title',
            'desc' => '',
            'id' => 'wc_custom_tab'
        ),
        'example_input' => array(
            'name' => __('My Input', 'woocommerce-my-custom-tab'),
            'type' => 'text',
            'desc' => '',
            'id' => 'wc_my_input'
        ),
        'section_end' => array(
            'type' => 'sectionend',
            'id' => 'wc_section_end'
        )
    );
    return apply_filters('wc_my_custom_tab_settings', $settings);
}

如何在允许将 example_input 字段保存到数据库之前对其执行自定义验证,并在需要时抛出错误以告诉用户输入有什么问题?

How can I perform custom validation on the example_input field before allowing it to save to the database, and, if needed, throw an error to tell the user what's wrong with the input?

推荐答案

您可以通过向字段添加 html5 required 属性来使字段成为必需的.

You can make field required by just adding the html5 required attribute to the field.

'example_input' => array(
    'name' => __('My Input', 'woocommerce-my-custom-tab'),
    'type' => 'text',
    'desc' => '',
    'custom_attributes' => array( 'required' => 'required' )
    'id' => 'wc_my_input'
),

但是在您的情况下,如果您想在字段更新时显示错误消息,您可以执行以下操作.添加过滤器 woocommerce_admin_settings_sanitize_option_

But in you case if you want to display error message on field update you can do something like this. add filter woocommerce_admin_settings_sanitize_option_<option_name>

// define the woocommerce_admin_settings_sanitize_option_<option_name> callback 
function filter_woocommerce_admin_settings_sanitize_option_wc_my_input( $value, $option, $raw_value ) { 
    add_action( 'admin_notices', function() use($value) {
        if($value == ""){
            echo '<div id="message" class="notice notice-error is-dismissible"><p>Option is required</p></div>';    
        }
    });
    return $value; 
}; 
// add the filter 
add_filter( "woocommerce_admin_settings_sanitize_option_wc_my_input", 'filter_woocommerce_admin_settings_sanitize_option_wc_my_input', 10, 3 ); 

编辑

您可以像这样遍历所有选项

You can loop through all options like this

$options = ['wc_my_input' => 'My Input', 'other_field' => 'Other Field']; // get all the options here
foreach($options as $option_name => $option_val){
    // define the woocommerce_admin_settings_sanitize_option_$option_name callback 
    add_filter( "woocommerce_admin_settings_sanitize_option_$option_name", function($value, $option, $raw_value) use($option_val) {
        add_action( 'admin_notices', function() use($value) {
            if($value == ""){
                echo "<div id=\"message\" class=\"notice notice-error is-dismissible\"><p>$option_val is required</p></div>";
            }
        });
        return $value;
    }, 10, 3 ); 
}

这篇关于WooCommerce 自定义设置选项卡验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆