WordPress "MW WP Form" How to display an error with a compound condition in the input field

WordPress “MW WP Form” How to display an error with a compound condition in the input field

Introduction

“MW WP Form” is a plugin that is often used when setting up an inquiry or application form on a site with WordPress .“MW WP Form” is a very convenient plug-in that has a function to display a confirmation screen before sending and a function to send an email to the administrator as standard.Although it is such a convenient “MW WP Form”, it has the disadvantage that it does not have a function to check input by combining multiple input items . When creating a complicated form, it is a problem that you can not check the input under compound conditions. So, this time, I will explain how to realize input check under compound conditions with “MW WP Form” just by adding validation rules to the functions.php file of the theme .

table of contents

What is input check under compound conditions? Form creation and general input check settings Addition of validation rules Finally

1. What is input check under compound conditions?

The input check, whether the input items of the form (text box, select boxes, and radio buttons) value is against that contains, or format you entered is correct, the length of the character and appropriate or before it is sent to check on , If there is an error, an error message is output to prompt you to re-enter . Then, the input check under the compound condition changes the check rule of another item according to the input (selection) content of one item .Input check under compound conditions is required in the following cases, for example.-In the form for entering contact information, <phone number> is required when “phone” is selected, and <phone number> is not required when “mail” is selected.“MW WP Form” has various validation rules as standard, and you can implement the input check function just by selecting which rule to apply for each input item on the management screen. However, the check that spans multiple input items cannot be set from the management screen, so you need to add the original validation rule to the PHP file .

2. Form creation and general input check settings

First, create a form on the WordPress administration screen. This time, I will omit how to create a form and explain from the input check setting method .Suppose that <name>, <contact method>, and <email address> of this form are required items, and <phone number> is required to be entered only when <contact method> is “phone” .First, set the required checks for <name>, <contact>, and <email address>.In the above image, the required input check and the email address format check are set for <email address>. Simply enter the value of the name attribute of the input item in “Items to apply validation” and select the checkbox of the validation rule, it is very easy.For radio buttons, select “Required items (check boxes)”.Now you are ready for input checks other than compound conditions.

3. Add validation rules

3.1 Confirming the form ID

From here , edit the theme’s functions.php and add a new validation rule to the input form created in 2.Before that, you need to get the ID of the form created in 2 . The ID is listed in the “Form Identifier” on the edit screen, so please check it before starting additional work.

3.2 Editing functions.php

Create the original validation rule my_required_phone in the theme file functions.php .By adding (add_filter) the created validation rule to the filter hook of “MW WP Form”, it will be executed at the time of input check execution .

functions.php

function my_required_phone ($ validation, $ data) {my_required_phone ( $ validation , $ data ) { $ method = $ data [ ‘method’ ]; if ( isset ( $ method ) && $ method === ‘call’ ) { $ validation- > set_rule ( ‘phone’ , ‘noEmpty’ , array ( ‘message’ => ‘If you select’phone’as the contact method, the phone number is required.’ ) ); } return $ Validation ; } add_filter ( ‘Mwform_validation_mw-wp-form-9940’ , ‘My_required_phone’ , 10 , 2 );The value entered in $ data will be entered as an array, so get the value used for the input check to be added. In this case, an input check will be added based on the checked items of <Contact method>, so specify the name attribute (= ”method” ) of <Contact method> and retrieve the value.Add a new validation rule with $ validation-> set_rule . Since the standard validation rule set on the management screen is already stored in $ validation, add the original validation rule with set_rule to it.In the first argument of set_rule, specify the name attribute of the input item to be checked, and in the second argument, specify the rule as a character string. The rules are listed in the official manual of “MW WP Form”, so please refer to that.Specify the error message to be displayed in the third argument of set_rule.Add the last created original validation rule my_required_phone to the filter hook. The filter to add is mwform_validation_mw-wp-form, but what you have to be careful about here is that you need to add the ID of the form confirmed in 3.1 to the end of the filter name .In other words, if the form ID is ” 9940 “, the filter name will be mwform_validation_mw-wp-form-9940 .After adding everything, save functions.php and check if the input check with compound conditions is working properly from the screen.

4. Finally

This time, I introduced how to implement a check that spans multiple input items using “MW WP Form”. Adding the original validation rule is a little difficult because you need to edit the PHP file even in the customization of “MW WP Form”, but there is a lot of demand, so please remember and use it.