frm_validate_entry

Back to Top
Knowledge BaseEntry Management → frm_validate_entry

This hook allows you to add custom errors that are not specific to one field.

Usage

add_action('frm_validate_entry', 'validate_my_form', 20, 2);

Parameters

  • $errors (array)
  • $posted_values (array)

Examples

Formidable Hook - Helpful

Remove all errors

Submitted by  — 7 years ago

Use this code to remove all errors from a form if a field has a particular value.

add_action('frm_validate_entry', 'validate_my_form', 20, 2);
function validate_my_form($errors, $values){
  if ( $values['form_id'] == 45 && $_POST['item_meta'][125] != 'Yes' ) { //Change 45 to the ID of your form, 125 to the ID of a field, and 'Yes' to any value
    return array();
  }
return $errors;
}
Formidable Hook - Helpful

Customize the spam message

Submitted by  — 7 years ago

add_action('frm_validate_entry', 'validate_my_form', 20, 2);
function validate_my_form($errors, $values){
  if ( isset($errors['spam']) ) {
    $errors['spam'] = 'No submissions for you!';
  }
return $errors;
}
Formidable Hook - Helpful

Require one field from a set

Submitted by  — 7 years ago

Make sure one of the fields in a group of fields is required.

add_filter('frm_validate_entry', 'check_phone_fields', 10, 2);
function check_phone_fields( $errors, $values ) {
  if ( $values['form_id'] == 45 ) { // change 45 to your form id
    $group_fields = array(25, 26, 27); // change 25, 26, 27 to the ids of your fields
    foreach ( $group_fields as $field_id ) {
      if ( $_POST['item_meta'][$field_id] != '' ) {
        return $errors; // do not continue checking if any of these fields have a value
      }
    }
    foreach ( $group_fields as $field_id ) {
      $errors['field'. $field_id] = 'Please fill in one of the fields.';
    }
  }
  return $errors;
}
Formidable Hook - Helpful

Prevent submissions from an IP

Submitted by  — 7 years ago

Blacklist any IPs and prevent entries from being created.

add_filter('frm_validate_entry', 'frm_check_ip_blacklist', 10, 2);
function frm_check_ip_blacklist( $errors, $values ) {
  $ips = array( '107.150.42.226', '195.154.232.138', '155.108.70.195' );
  $current_ip = FrmAppHelper::get_ip_address();
  if ( in_array( $current_ip, $ips ) ) {
    $errors['spam'] = 'You are not allowed to do that';
  }
  return $errors;
}
Formidable Hook - Helpful

Populate fields from User ID

Submitted by  — 7 years ago

Fill other fields based on the id of the user selected in your user ID field.

add_action('frm_validate_entry', 'frm_add_user_name', 20, 2);
function frm_add_user_name( $errors, $values ) {
  if ( isset( $_POST['item_meta'][25] ) ) { //change 25 to the id of the user id field
    $user = get_userdata( absint( $_POST['item_meta'][25] ) ); //change 25 here too
    $_POST['item_meta'][26] = $user->last_name; //change 26 to the id of the last name field
    $_POST['item_meta'][27] = $user->first_name; //change 27 to the id of the first name field
  }
  return $errors;
}
Formidable Hook - Helpful

Basic format

Submitted by  — 7 years ago

Use this format if you would like to add an error to the form.

add_action('frm_validate_entry', 'validate_my_form', 20, 2);
function validate_my_form($errors, $values){
  if($values['form_id'] == 45){//Change 45 to the ID of your form and add additional conditions here
    $errors['my_error'] = 'You are not allowed to submit this form.';//Change this to your error
  }
return $errors;
}
If you would like to add your own example, click here.

Have something to add?

Click here to provide feedback on this page.