This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.
This hook allows you to add custom errors that are not specific to one field.
Usage
add_filter('frm_validate_entry', 'validate_my_form', 20, 2);
Parameters
- $errors (array)
- $posted_values (array)
Examples
Remove all errors
Use this code to remove all errors from a form if a field has a particular value.
add_filter('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;
}
Basic format
Use this format if you would like to add an error to the form.
add_filter('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;
}
Customize the spam message
add_filter('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;
}
Require one field from a set
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;
}
Prevent submissions from an IP
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;
}
Populate fields from User ID
Fill other fields based on the id of the user selected in your user ID field.
add_filter('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;
}
Limit submissions per time period (or any stat)
Limit form submissions to anything that can be determined by a stat.
Examples:
- 1 submission per user per week
- 10 submissions from all users with "Platinum" selected in the membership field.
- 100 submissions from all users per year
- total of 10 votes per user
add_filter('frm_validate_entry', 'check_submitted', 20, 2);
function check_submitted($errors, $values){
if ( $values['form_id'] !== 30 ) {//Change 30 to the ID of your form
return $errors;
}
$entries_submitted = FrmProStatisticsController::stats_shortcode( array( 'id' => 182, 'type' => 'count', 'user_id' => 'current', 'created_at_greater_than' => 'Monday this week' ) );//change the params to the params of your stat
if ( $entries_submitted >= 1 ){//change 0 to your limit number
$errors['too_many'] = 'You have already submitted this form this week. You are most welcome to submit again next week.';//Change this to your error message
}
return $errors;
}
Recommendation: first, create the stat and test to make sure it's working as you want. Then, add the params to the code.
For dates, you can use exact dates or relative dates.
You can read about relative dates here: