Created
November 12, 2021 08:13
-
-
Save wpmudev-sls/9a2dc6ced8ba2a5c8777b534dfcae486 to your computer and use it in GitHub Desktop.
[Forminator] - Validation form fields.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Plugin Name: [Forminator] - Validation form fields. | |
| * Description: [Forminator] - Validate form fields based on defined validation rule. | |
| * Plugin URI: https://wpmudev.com/ | |
| * Author: Abdoljabbar Bakhshande | |
| * Author URI: https://wpmudev.com/ | |
| * License: GPLv2 or later | |
| */ | |
| if ( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { | |
| return; | |
| } | |
| add_action( 'after_setup_theme', 'forminator_form_field_validation', 100 ); | |
| function forminator_form_field_validation() { | |
| if ( defined( 'FORMINATOR_PRO' ) && class_exists( 'Forminator' ) ) { | |
| class Forminator_Field_Custom_Validation { | |
| // EDIT HERE ######################. | |
| private $form_custom_validation_list = array( | |
| // Set your form validation list here. | |
| array( | |
| 'form_id' => '160', // form id. | |
| 'field_name' => 'text-1', // the field name that you want to validate. | |
| // value validation list - you can also use php regex pattern in /your-pattern/ format. | |
| 'field_validation_pattern' => array( | |
| 'TeamTactical', | |
| 'AnotherWord', | |
| ), | |
| 'error_message' => 'This field is incorect. Please enter the correct value.', // error message. | |
| ), | |
| ); | |
| // DONT EDIT BELOW THIS LINE ##################################. | |
| /** | |
| * Class instance. | |
| * | |
| * @var Forminator_Field_Custom_Validation | |
| */ | |
| private static $instance; | |
| /** | |
| * Forminator_Field_Custom_Validation constructor. | |
| * | |
| * @return void | |
| */ | |
| public function __construct() { | |
| add_action( 'wp_footer', array( $this, 'forminator_field_custom_validation_js' ), 100 ); | |
| add_action( 'wp_ajax_forminator_form_custom_validation', array( $this, 'forminator_form_field_custom_validation' ) ); | |
| add_action( 'wp_ajax_nopriv_forminator_form_custom_validation', array( $this, 'forminator_form_field_custom_validation' ) ); | |
| // server-side validation after submission. | |
| add_filter( 'forminator_custom_form_submit_errors', array( $this, 'forminator_form_ajax_submit_response_handler' ), 100 ); | |
| } | |
| /** | |
| * Return class instance. | |
| * | |
| * @return Forminator_Field_Custom_Validation | |
| */ | |
| public static function get_instance() { | |
| if ( is_null( self::$instance ) ) { | |
| self::$instance = new self(); | |
| } | |
| return self::$instance; | |
| } | |
| /** | |
| * Add custom validation js | |
| * | |
| * @return void | |
| */ | |
| public function forminator_field_custom_validation_js() { | |
| foreach ( $this->form_custom_validation_list as $form_custom_validation ) { | |
| $form_id = $form_custom_validation['form_id']; | |
| $field_name = $form_custom_validation['field_name']; | |
| ?> | |
| <script type="text/javascript"> | |
| document.addEventListener('DOMContentLoaded', function(){ | |
| let form_id = document.querySelector('input[name="form_id"]').value; | |
| let field_name = document.querySelector('input[name="<?php echo $field_name; ?>"]'); | |
| if ( form_id && field_name ) { | |
| (function($){ | |
| $(field_name).on('keyup blur', function(){ | |
| let field_value = $(this).val(); | |
| if ( field_value.length < 5 ) { | |
| $('.forminator-button-next').prop('disabled', true); | |
| $(field_name).parent().addClass('forminator-has_error'); | |
| $('.forminator-error-message').remove(); | |
| $(field_name).parent().append('<span class="forminator-error-message">At least 5 characters required</span>'); | |
| return; | |
| }; | |
| let ajax_nonce = '<?php echo wp_create_nonce( 'forminator_form_custom_validation' ); ?>'; | |
| $.ajax({ | |
| url: '<?php echo admin_url( 'admin-ajax.php' ); ?>', | |
| type: 'POST', | |
| data: { | |
| action: 'forminator_form_custom_validation', | |
| field_name: '<?php echo $field_name; ?>', | |
| field_value: field_value, | |
| ajax_nonce: ajax_nonce, | |
| }, | |
| success: function(response){ | |
| if ( ! response.success ) { | |
| $('.forminator-button-next').prop('disabled', true); | |
| $(field_name).parent().addClass('forminator-has_error'); | |
| $('.forminator-error-message').remove(); | |
| $(field_name).parent().append('<span class="forminator-error-message">'+response.message+'</span>'); | |
| } else { | |
| $(field_name).parent().removeClass('forminator-has_error'); | |
| $('.forminator-error-message').remove(); | |
| $('.forminator-button-next').prop('disabled', false); | |
| } | |
| } | |
| }); | |
| }); | |
| })(jQuery); | |
| } | |
| }, false); | |
| </script> | |
| <?php | |
| } | |
| } | |
| /** | |
| * Ajax handler for form field custom validation. | |
| * | |
| * @return void | |
| */ | |
| public function forminator_form_field_custom_validation() { | |
| if ( ! isset( $_POST['field_value'] ) || ! isset( $_POST['ajax_nonce'] ) || ! isset( $_POST['field_name'] ) ) { | |
| return; | |
| } | |
| $field_name = sanitize_text_field( wp_unslash( $_POST['field_name'] ) ); | |
| $field_value = sanitize_text_field( wp_unslash( $_POST['field_value'] ) ); | |
| $ajax_nonce = sanitize_text_field( wp_unslash( $_POST['ajax_nonce'] ) ); | |
| if ( ! wp_verify_nonce( $ajax_nonce, 'forminator_form_custom_validation' ) ) { | |
| return; | |
| } | |
| $validation_result = $this->form_custom_validation( $field_name, $field_value ); | |
| // send result with message. | |
| wp_send_json( $validation_result ); | |
| } | |
| /** | |
| * Validate form field based on defined validation rule. | |
| * | |
| * @param string $field_name Field name. | |
| * @param string $field_value Field value. | |
| * | |
| * @return array | |
| */ | |
| public function form_custom_validation( $field_name, $field_value ) { | |
| foreach ( $this->form_custom_validation_list as $form_custom_validation ) { | |
| if ( $field_name === $form_custom_validation['field_name'] ) { | |
| $field_validation_patterns = $form_custom_validation['field_validation_pattern']; | |
| foreach ( $field_validation_patterns as $field_validation_pattern ) { | |
| if ( substr( $field_validation_pattern, 0, 1 ) === '/' ) { | |
| if ( preg_match( $field_validation_pattern, $field_value ) ) { | |
| return array( | |
| 'success' => true, | |
| 'message' => '', | |
| ); | |
| } | |
| } elseif ( $field_value === $field_validation_pattern ) { | |
| return array( | |
| 'success' => true, | |
| 'message' => '', | |
| ); | |
| } | |
| } | |
| return array( | |
| 'success' => false, | |
| 'message' => $form_custom_validation['error_message'], | |
| ); | |
| } | |
| } | |
| } | |
| /** | |
| * Ajax response handler for custom validation on submit. | |
| * | |
| * @param array $response - ajax response. | |
| * @return array | |
| */ | |
| public function forminator_form_ajax_submit_response_handler( $response ) { | |
| if ( ! empty( $response ) ) { | |
| return $response; | |
| } else { | |
| foreach ( $this->form_custom_validation_list as $form_custom_validation ) { | |
| if ( isset( $_POST[ 'form_id'] ) && $_POST[ 'form_id'] === $form_custom_validation['form_id'] ) { // phpcs:ignore | |
| $field_name = $form_custom_validation['field_name']; | |
| if ( isset( $_POST[ $field_name ] ) ) { // phpcs:ignore | |
| $field_value = sanitize_text_field( $_POST[ $field_name ] ); // phpcs:ignore | |
| $validation_result = $this->form_custom_validation( $field_name, $field_value ); | |
| if ( ! $validation_result['success'] ) { | |
| $response_error = array( $field_name => $validation_result['message'] ); | |
| $response = array(); | |
| array_push( $response, $response_error ); | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| return $response; | |
| } | |
| } | |
| // Initialize. | |
| Forminator_Field_Custom_Validation::get_instance(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment