Formidable Forms

The Most Advanced WordPress Forms Plugin and Form Builder

  • Features
  • Pricing
  • Blog
  • Support
  • Login
  • Get Formidable Forms

frm_validate_entry

Last Updated: June 11, 2018

Knowledge Base → Extend Formidable Forms → Formidable Hooks - for Developers → Entry Management → frm_validate_entry
Heads up!
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.

Formidable Forms is the best WordPress Form Builder plugin. Get it for free!

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:

http://php.net/manual/en/datetime.formats.relative.php

  • Usage
  • Parameters
  • Examples
    • Remove all errors
    • Basic format
    • Customize the spam message
    • Require one field from a set
    • Prevent submissions from an IP
    • Populate fields from User ID
    • Limit submissions per time period (or any stat)
Categories
×

Categories

  • Installation & Getting Started
  • Account Management
  • Forms
  • Entries
  • Views
  • Styles
  • Importing & Exporting
  • Add-Ons
  • Extend Formidable Forms
Install Formidable Lite

Using WordPress and want to get Formidable Forms for free?

This article may contain affiliate links. Once in a while, we earn commissions from those links. But we only recommend products we like, with or without commissions.

Take on bigger projects Right Now

Get the tools you need to revolutionize your workflow and architect a masterpiece. Build the most advanced WordPress forms and actually use the data you collect in meaningful ways.

Get the most advanced WordPress form plugin and the only form builder with integrated Views.

Get Formidable Forms Now

Resources

  • Community
  • Affiliates
  • Contact
  • Free Online Form Builder

Top Features

  • Front End Editor
  • Repeating Fields
  • Views from Entries
  • Calculator Forms
  • Conditional Logic
  • Visual Form Styler
  • Form Templates
  • User Submitted Posts
  • File Upload Forms
  • Spam Protection
  • Multi Page Forms
  • Surveys & Polls
  • Form Graphs & Charts
  • Save and Continue
  • Mobile Forms
  • Stripe Forms
  • PayPal Forms
  • WooCommerce Forms
  • MailChimp Forms
  • User Registration
  • Signature Forms
  • Bootstrap Forms
  • Quiz Maker
  • Zapier Forms
  • Salesforce Forms
  • HubSpot Forms
  • API Webhooks
  • Multilingual Forms
  • Directories

Company

  • About Us
  • Giving Back
  • Careers
  • Newsletter
  • Blog

Copyright © 2021 Strategy11, LLC. Formidable Forms® is a registered trademark Strategy11, LLC.
Privacy Policy | Terms of Service | Sitemap

Join 300,000+ using Formidable Forms to create form-focused solutions fast. Get Started See User Reviews