Skip to content

Instantly share code, notes, and snippets.

@jaredatch
Last active October 18, 2024 02:02
Show Gist options
  • Select an option

  • Save jaredatch/def524c06d13cbb06c33da596c47eb48 to your computer and use it in GitHub Desktop.

Select an option

Save jaredatch/def524c06d13cbb06c33da596c47eb48 to your computer and use it in GitHub Desktop.

Revisions

  1. jaredatch revised this gist Dec 15, 2016. 1 changed file with 6 additions and 8 deletions.
    14 changes: 6 additions & 8 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -22,16 +22,14 @@ function ja_custom_text_validation( $field_id, $field_submit, $form_data ) {
    // Since a field can have multiple classes we have to accomodate that
    $classes = explode( ' ', $fields[$field_id]['css'] );

    foreach ( $classes as $class ) {
    if ( 'some-custom-field-class' == trim( $class ) ) {
    if ( in_array( 'some-custom-field-class', $classes ) ) {

    // Class matches so now we validate
    if ( 'foobar' !== $field_submit ) {
    // Class matches so now we validate
    if ( 'foobar' !== $field_submit ) {

    // Throw error!
    wpforms()->process->errors[$form_id][$field_id] = 'Value must be foobar';
    return;
    }
    // Throw error!
    wpforms()->process->errors[$form_id][$field_id] = 'Value must be foobar';
    return;
    }
    }
    }
  2. jaredatch revised this gist Oct 13, 2016. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions custom-validation.js
    Original file line number Diff line number Diff line change
    @@ -48,9 +48,11 @@
    }

    // Apply the rule to the input
    $('.some-custom-field-class input').rules( 'add', {
    foo: true
    });
    if ( $('.some-custom-field-class input').length ) {
    $('.some-custom-field-class input').rules( 'add', {
    foo: true
    });
    }
    },
    }

  3. jaredatch created this gist Oct 12, 2016.
    58 changes: 58 additions & 0 deletions custom-validation.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    ;(function($) {

    var JA_Custom_Validation = {

    /**
    * Start the engine.
    *
    * @since 1.0.0
    */
    init: function() {

    $(document).on('wpformsReady', JA_Custom_Validation.customRules);
    },

    /**
    * Custom validation rules.
    *
    * @since 1.0.0
    */
    customRules: function() {

    // Only load if jQuery validation library exists
    if (typeof $.fn.validate !== 'undefined') {

    /**
    * Documentation for addMethod is:
    * https://jqueryvalidation.org/jQuery.validator.addMethod/
    *
    * "foo" is the name of the rule, which requires the value
    * to be "foobar"
    *
    * Validation can also be forced by using the wpforms_field_atts
    * filter to add a 'rule-foo' data attribute that is set to true,
    * eg data-rule-foo="true"
    *
    * https://jqueryvalidation.org/rules/
    */

    // Register the validation rule
    $.validator.addMethod("foo", function(value, element, param) {

    // In case you need to reference the element with jQuery
    // Not actually used in this rule
    var $ele = $(element);

    return this.optional(element) || value === 'foobar';
    }, $.validator.format("Value is not foobar"));
    }

    // Apply the rule to the input
    $('.some-custom-field-class input').rules( 'add', {
    foo: true
    });
    },
    }

    JA_Custom_Validation.init();
    })(jQuery);
    39 changes: 39 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    <?php
    /**
    * Add PHP validation for custom rule
    *
    * @param int $field_id
    * @param array $field_submit
    * @param array $form_data
    * @since 1.0.0
    */
    function ja_custom_text_validation( $field_id, $field_submit, $form_data ) {

    // You could target the field specifically with the field ID or you could
    // use the field ID to look up the field class stored inside $form_data which
    // is what we do below.

    $form_id = $form_data['id'];
    $fields = $form_data['fields'];

    // Check if field has custom CSS class configured
    if ( !empty( $fields[$field_id]['css'] ) ) {

    // Since a field can have multiple classes we have to accomodate that
    $classes = explode( ' ', $fields[$field_id]['css'] );

    foreach ( $classes as $class ) {
    if ( 'some-custom-field-class' == trim( $class ) ) {

    // Class matches so now we validate
    if ( 'foobar' !== $field_submit ) {

    // Throw error!
    wpforms()->process->errors[$form_id][$field_id] = 'Value must be foobar';
    return;
    }
    }
    }
    }
    }
    add_action( 'wpforms_process_validate_text', 'ja_custom_text_validation', 10, 3 );