In my WordPress website I installed the Contact Form-7 plugin, and have been having a problem. I think there is no special validation for text fields. Also for phone fields, Contact Form-7 plugin only provides basic validation for not null
or blank
fields.
My question is: for those issues what should I do to fix them? I could either modify the core plugin file OR create my own contact form.
In Contact Form 7, a user-input validation is implemented as a filter function. The filter hook used for the validation varies depending on the type of form-tag and is determined as: wpcf7_validate_ + {type of the form-tag}. So, for text form-tags, the filter hook wpcf7_validate_text is used. Likewise, wpcf7_validate_email* is used for email* form-tags.
Letâs say you have the following email fields in a form:
The following listing shows code that verifies whether the two fields have identical values.
Two parameters will be passed to the filter function: $result and $tag. $result is an instance of WPCF7_Validation class that manages a sequence of validation processes. $tag is an associative array composed of given form-tag components; as you saw in the previous recipe, you can use WPCF7_Shortcode class to handle this type of data.
Look through the inside of the filter function. First, check the name of the form-tag to ensure the validation is applied only to the specific field (your-email-confirm).
The two email field values are then compared, and if they donât match, $result->invalidate() will be called. You need to pass two parameters to the invalidate() method: the first parameter should be the $tag variable, and the second parameter is the validation error message that you want the field to display.
Lastly, donât forget to return the $result.
The new contact form 7 plugin provides inbuilt validations on its latest update.