Skip to content

Commit

Permalink
Feature - Regex validation for field (#1118)
Browse files Browse the repository at this point in the history
* Feature - Regex validation for field

* Fix - PHPCS issue

* changelog updated

---------

Co-authored-by: unknown <elendoff11@gmail.com>
  • Loading branch information
dipuchaudhary and deepench authored Jul 17, 2023
1 parent ee708a8 commit 680ac86
Show file tree
Hide file tree
Showing 12 changed files with 326 additions and 2 deletions.
25 changes: 25 additions & 0 deletions assets/js/admin/form-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,21 @@
}
});

$builder.on( 'change', '.everest-forms-field-option-row-enable_regex_validation input', function( event ) {
var id = $( this ).parent().data( 'field-id' );

$( '#everest-forms-field-' + id ).toggleClass( 'regex_value' );

// Toggle "Parameter Name" option.
if ( $( event.target ).is( ':checked' ) ) {
$( '#everest-forms-field-option-row-' + id + '-regex_value' ).show();
$( '#everest-forms-field-option-row-' + id + '-regex_message' ).show();
} else {
$( '#everest-forms-field-option-row-' + id + '-regex_value' ).hide();
$( '#everest-forms-field-option-row-' + id + '-regex_message' ).hide();
}
});

// Real-time updates for "Description" field option.
$builder.on( 'input', '.everest-forms-field-option-row-description textarea', function() {
var $this = $( this ),
Expand Down Expand Up @@ -3043,6 +3058,9 @@ jQuery( function ( $ ) {
} else if ( 'other' === type ) {
$input.val( $input.val() + '{'+field_id+'}' );
$textarea.val($textarea.val() + '{'+field_id+'}' );
} else if ( 'regex' === type ) {
$input.val($input.val() + field_id.replace(field_label+'_','') );
$textarea.val( $textarea.val() + field_id.replace(field_label+'_','') );
}
});

Expand Down Expand Up @@ -3160,6 +3178,13 @@ jQuery( function ( $ ) {
}
}

if( 'regex' == type ){
var regex_lists = evf_data.regex_expression_lists;
regex_lists.forEach(function(key,value) {
$(el).parent().find('.evf-smart-tag-lists .evf-regex').append('<li class = "smart-tag-field" data-type="regex" data-field_id="'+key.value+'">'+key.text+'</li>');
});
}

if ( 'fields' === type || 'all' === type ) {
if ( allowed_field === 'email' ) {
if ( Object.keys(email_field).length < 1 ){
Expand Down
3 changes: 3 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
== Changelog ==

- 2.0.2 - xx-xx-2023
* Feature - Regex validation for field.

- 2.0.1 - 14-06-2023
* Enhancement - Upgrade to pro link.
* Feature - Cloudflare Turnstile.
Expand Down
132 changes: 132 additions & 0 deletions includes/abstracts/class-evf-form-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -1539,6 +1539,121 @@ public function field_option( $option, $field, $args = array(), $echo = true ) {
);
break;

/**
* Regex Validation
*/
case 'regex_validation':
$default = ! empty( $args['default'] ) ? $args['default'] : '0';
$value = ! empty( $field['enable_regex_validation'] ) ? esc_attr( $field['enable_regex_validation'] ) : '';
$tooltip = esc_html__( 'Enable this option to allow regex validation for this field.', 'everest-forms' );
$output = $this->field_element(
'checkbox',
$field,
array(
'slug' => 'enable_regex_validation',
'value' => $value,
'desc' => esc_html__( 'Enable Regex Validation ', 'everest-forms' ),
'tooltip' => $tooltip,
),
false
);
$output = $this->field_element(
'row',
$field,
array(
'slug' => 'enable_regex_validation',
'content' => $output,
),
$echo
);
break;

case 'regex_value':
$toggle = '';
$tooltip = esc_html__( 'Regular expression value is checked against.', 'everest-forms' );
$value = ! empty( $field['regex_value'] ) ? esc_attr( $field['regex_value'] ) : '';

// Build output.
$output = $this->field_element(
'label',
$field,
array(
'slug' => 'regex_value',
'value' => esc_html__( 'Regex Value', 'everest-forms' ),
'tooltip' => $tooltip,
'after_tooltip' => $toggle,
),
false
);
$output .= $this->field_element(
'text',
$field,
array(
'slug' => 'regex_value',
'value' => $value,
),
false
);
// Smart tag for default value.
$include_fields = array( 'email', 'first-name', 'last-name', 'number', 'text', 'url' );

if ( in_array( $field['type'], $include_fields, true ) ) {
$output .= '<a href="#" class="evf-toggle-smart-tag-display" data-type="regex"><span class="dashicons dashicons-editor-code"></span></a>';
$output .= '<div class="evf-smart-tag-lists" style="display: none">';
$output .= '<div class="smart-tag-title other-tag-title">Regular Expression</div><ul class="evf-regex"></ul></div>';
}

$output = $this->field_element(
'row',
$field,
array(
'slug' => 'regex_value',
'content' => $output,
'class' => ! in_array( $field['type'], $include_fields, true ) && isset( $field['enable_regex_validation'] ) ? '' : ' hidden evf_smart_tag',
),
$echo
);

break;

case 'regex_message':
$toggle = '';
$tooltip = esc_html__( 'if the regular expression value does not match it will show this message.', 'everest-forms' );
$value = ! empty( $field['regex_message'] ) ? esc_attr( $field['regex_message'] ) : 'Please provide a valid value for this field.';

// Build output.
$output = $this->field_element(
'label',
$field,
array(
'slug' => 'regex_message',
'value' => esc_html__( 'Validation Message for Regular expression', 'everest-forms' ),
'tooltip' => $tooltip,
'after_tooltip' => $toggle,
),
false
);
$output .= $this->field_element(
'text',
$field,
array(
'slug' => 'regex_message',
'value' => $value,
),
false
);
$output = $this->field_element(
'row',
$field,
array(
'slug' => 'regex_message',
'content' => $output,
'class' => isset( $field['enable_regex_validation'] ) ? '' : 'hidden',
),
$echo
);
break;

/*
* CSS classes.
*/
Expand Down Expand Up @@ -2373,12 +2488,28 @@ public function validate( $field_id, $field_submit, $form_data ) {
update_option( 'evf_validation_error', 'yes' );
}

// validate regex validation.
if ( isset( $form_data['form_fields'][ $field_id ]['enable_regex_validation'] ) && '1' === $form_data['form_fields'][ $field_id ]['enable_regex_validation'] ) {
$regex_value = ! empty( $form_data['form_fields'][ $field_id ]['regex_value'] ) ? $form_data['form_fields'][ $field_id ]['regex_value'] : '';
$regex_message = ! empty( $form_data['form_fields'][ $field_id ]['regex_message'] ) ? $form_data['form_fields'][ $field_id ]['regex_message'] : esc_html__( 'Please provide a valid value for this field', 'everest-forms' );
$value = '';
if ( is_array( $field_submit ) ) {
$value = ! empty( $field_submit['primary'] ) ? $field_submit['primary'] : '';
} else {
$value = ! empty( $field_submit ) ? $field_submit : '';
}
if ( ! preg_match( '/' . $regex_value . '/', $value ) ) {
evf()->task->errors[ $form_data['id'] ][ $field_id ] = $regex_message;
update_option( 'evf_validation_error', 'yes' );
}
}
// Type validations.
switch ( $field_type ) {
case 'url':
if ( ! empty( $_POST['everest_forms']['form_fields'][ $field_id ] ) && filter_var( $field_submit, FILTER_VALIDATE_URL ) === false ) { // phpcs:ignore WordPress.Security.NonceVerification
$validation_text = get_option( 'evf_' . $field_type . '_validation', esc_html__( 'Please enter a valid url', 'everest-forms' ) );
}

break;
case 'email':
if ( is_array( $field_submit ) ) {
Expand Down Expand Up @@ -2421,6 +2552,7 @@ public function validate( $field_id, $field_submit, $form_data ) {
}
}
}

break;
}

Expand Down
1 change: 1 addition & 0 deletions includes/admin/class-evf-admin-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ public function admin_scripts() {
'email_fields' => evf_get_all_email_fields_by_form_id( isset( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : 0 ), // phpcs:ignore WordPress.Security.NonceVerification
'all_fields' => evf_get_all_form_fields_by_form_id( isset( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : 0 ), // phpcs:ignore WordPress.Security.NonceVerification
'smart_tags_other' => evf()->smart_tags->other_smart_tags(),
'regex_expression_lists' => evf()->smart_tags->regex_expression_lists(),
'entries_url' => ! empty( $_GET['form_id'] ) ? esc_url( admin_url( 'admin.php?page=evf-entries&amp;form_id=' . absint( $_GET['form_id'] ) ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification
'preview_url' => ! empty( $_GET['form_id'] ) ? esc_url( // phpcs:ignore WordPress.Security.NonceVerification
add_query_arg(
Expand Down
144 changes: 143 additions & 1 deletion includes/class-evf-smart-tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,148 @@ public function other_smart_tags() {
return $smart_tags;
}

/**
* Other Regex Expression Lists.
*
* @return string|array
*/
public function regex_expression_lists() {
$regex_lists = apply_filters(
'everest_forms_regex_expression_lists',
array(
array(
'text' => __( 'Alpha', 'everest-forms' ),
'value' => '^[a-zA-Z]+$',
),
array(
'text' => __( 'Alphanumeric', 'everest-forms' ),
'value' => '^[a-zA-Z0-9]+$',
),
array(
'text' => __( 'Color', 'everest-forms' ),
'value' => '^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$',
),
array(
'text' => __( 'Country Code (2 Character)', 'everest-forms' ),
'value' => '^[A-Za-z]{2}$',
),
array(
'text' => __( 'Country Code (3 Character)', 'everest-forms' ),
'value' => '^[A-Za-z]{3}$',
),
array(
'text' => __( 'Date (mm/dd)', 'everest-forms' ),
'value' => '^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])$',
),
array(
'text' => __( 'Date (dd/mm)', 'everest-forms' ),
'value' => '^(0[1-9]|1\d|2\d|3[01])\/(0[1-9]|1[0-2])$',
),
array(
'text' => __( 'Date (mm.dd.yyyy)', 'everest-forms' ),
'value' => '^(0[1-9]|1[0-2])\.(0[1-9]|1\d|2\d|3[01])\.\d{4}$',
),
array(
'text' => __( 'Date (dd.mm.yyyy)', 'everest-forms' ),
'value' => '^(0[1-9]|1\d|2\d|3[01])\.(0[1-9]|1[0-2])\.\d{4}$',
),
array(
'text' => __( 'Date (yyyy-mm-dd)', 'everest-forms' ),
'value' => '^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|1\d|2\d|3[01])$',
),
array(
'text' => __( 'Date (mm/dd/yyyy)', 'everest-forms' ),
'value' => '^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/\d{4}$',
),
array(
'text' => __( 'Date (dd/mm/yyyy)', 'everest-forms' ),
'value' => '^(0[1-9]|1\d|2\d|3[01])\/(0[1-9]|1[0-2])\/\d{4}$',
),
array(
'text' => __( 'Email', 'everest-forms' ),
'value' => '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$',
),
array(
'text' => __( 'IP (Version 4)', 'everest-forms' ),
'value' => '^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$',
),
array(
'text' => __( 'IP (Version 6)', 'everest-forms' ),
'value' => '((^|:)([0-9a-fA-F]{0,4})){1,8}$',
),
array(
'text' => __( 'ISBN', 'everest-forms' ),
'value' => '^978(?:-[\d]+){3}-[\d]$',
),
array(
'text' => __( 'Latitude or Longitude', 'everest-forms' ),
'value' => '-?\d{1,3}\.\d+',
),
array(
'text' => __( 'Numeric', 'everest-forms' ),
'value' => '^[0-9]+$',
),
array(
'text' => __( 'Password (Numeric, lower, upper)', 'everest-forms' ),
'value' => '^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*$',
),
array(
'text' => __( 'Password (Numeric, lower, upper, min 8)', 'everest-forms' ),
'value' => '(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}',
),
array(
'text' => __( 'Phone - General', 'everest-forms' ),
'value' => '[0-9+()-. ]+',
),
array(
'text' => __( 'Phone - UK', 'everest-forms' ),
'value' => '^\+44\d{10}$',
),
array(
'text' => __( 'Phone - US: 123-456-7890', 'everest-forms' ),
'value' => '\d{3}[\-]\d{3}[\-]\d{4}',
),
array(
'text' => __( 'Phone - US: (123)456-7890', 'everest-forms' ),
'value' => '\([0-9]{3}\)[0-9]{3}-[0-9]{4}',
),
array(
'text' => __( 'Phone - US: (123) 456-7890', 'everest-forms' ),
'value' => '\([0-9]{3}\) [0-9]{3}-[0-9]{4}',
),
array(
'text' => __( 'Phone - US: Flexible', 'everest-forms' ),
'value' => '(?:\(\d{3}\)|\d{3})[- ]?\d{3}[- ]?\d{4}',
),
array(
'text' => __( 'Postal Code (UK)', 'everest-forms' ),
'value' => '^[A-Za-z]{1,2}\d{1,2}[A-Za-z]?\s?\d[A-Za-z]{2}$',
),
array(
'text' => __( 'Price (1.23)', 'everest-forms' ),
'value' => '\d+(\.\d{2})?$',
),
array(
'text' => __( 'Slug', 'everest-forms' ),
'value' => '^[a-zA-Z0-9-]+$',
),
array(
'text' => __( 'Time (hh:mm:ss)', 'everest-forms' ),
'value' => '(0[0-9]|1[0-9]|2[0-3])(:[0-5][0-9]){2}',
),
array(
'text' => __( 'URL', 'everest-forms' ),
'value' => '^(https?|ftp):\/\/[^\s\/$.?#].[^\s]*$',
),
array(
'text' => __( 'Zip Code', 'everest-forms' ),
'value' => '(\d{5}([\-]\d{4})?)',
),
)
);
return $regex_lists;
}

/**
* Process and parse smart tags.
*
Expand Down Expand Up @@ -280,7 +422,7 @@ public function process( $content, $form_data, $fields = '', $entry_id = '' ) {
case 'last_name':
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
$name = sanitize_text_field( $user->user_lastname );
$name = sanitize_text_field( $user->user_lastname );
} else {
$name = '';
}
Expand Down
3 changes: 3 additions & 0 deletions includes/fields/class-evf-field-email.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public function __construct() {
'sublabel_hide',
'default_value',
'css',
'regex_validation',
'regex_value',
'regex_message',
),
),
);
Expand Down
3 changes: 3 additions & 0 deletions includes/fields/class-evf-field-first-name.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public function __construct() {
'label_hide',
'default_value',
'css',
'regex_validation',
'regex_value',
'regex_message',
),
),
);
Expand Down
Loading

0 comments on commit 680ac86

Please sign in to comment.