Add Extra Validation to a Custom Field

Extra Validation to a Custom Field

This filter will help you add an extra validation for a custom field:

wppb_check_form_{field_type}

{field_type} can be one of the following:

  • avatar
  • checkbox
  • checkbox-terms-and-conditions
  • colorpicker
  • datepicker
  • input
  • map
  • phone
  • radio
  • recaptcha
  • select
  • select-country
  • select-currency
  • select-multiple
  • select-timezone
  • textarea
  • timepicker
  • upload
  • select-user-role
  • validation
  • wysiwyg

Below is an example on how we can use this filter to add a Custom Validation for an Input Field Type

Custom Input Validation

In this case since we use an Input Field Type, the filter name will be:

add_filter( ‘wppb_check_form_field_input’, ‘wppbc_custom_input_validation’, 20, 4);

This function will add a custom validation for an specific Input Field, with the meta name of “special_input”.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 * Custom Input Validation
 */
 
/* handle field validation */
add_filter( 'wppb_check_form_field_input', 'wppbc_custom_input_validation', 20, 4 );
function wppbc_custom_input_validation( $message, $field, $request_data, $form_location ){
	if( $field['field'] == 'Input' && $field['meta-name'] == 'special_input' ){
		if ( isset( $request_data[$field['meta-name']] ) && trim( $request_data[$field['meta-name']] ) != '' ){
			$input = $request_data[$field['meta-name']];
			$possible_values = array('one' ,'two', 'three', 'four', 'five');
			if( !in_array( $input, $possible_values) ) {
				return 'You have entered an incorrect value for this field.';
			}
		}
		if ( ( isset( $request_data[$field['meta-name']] ) && ( trim( $request_data[$field['meta-name']] ) == '' ) ) && ( $field['required'] == 'Yes' ) ){
			return wppb_required_field_error($field["field-title"]);
		}
	}
 
	return $message;
}
  • For more details on the sent parameters ( $message, $field, $request_data, $form_location ) you can have a look in Profile Builder: front-end/extra-fields/input/input.php file line 59
  • wppb_check_form_field_input is located in Profile Builder: /front-end/extra-fields/input/input.php line 71