Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Gravity Forms - Ensure fields are added to form before saving WordPress

  • SOLVED

I have a situation with Gravity Forms where I need to ensure that there are certain fields added to the form before it will allow you to save it.

For example, if I create a new form and don't add any fields then when I click on 'Update Form' I want it to give me a message saying 'Required Fields missing'

If I then add the required fields (Drop Down, Single Line Text and Paragraph Text) it will then let me save.

Can anyone give me an example snippet?

Answers (1)

2014-02-10

Dbranes answers:

Here's one method to check the saved fields and stop the saving process if neccessary:

add_action( 'admin_init', function(){
if( isset( $_POST['gform_meta'] ) )
{
$gform_meta = json_decode( stripslashes_deep( $_POST['gform_meta'] ), TRUE );
$freq = array();

//-------------------------------------------------------
// EDIT:
// Must have field types and their corresponding frequencies.
// For example the setup with 3 x 'text' fields + 1 x 'select' field, is denoted:
// See all field types here (*)
$must_have = array( 'text' => 3, 'select' => 1 );
//-------------------------------------------------------

if( isset( $gform_meta['fields'] ) )
{
$fields = $gform_meta['fields'];
foreach( (array) $fields as $field )
{
if( isset( $freq[$field['type']] ) )
{
$freq[$field['type']]++;
}
else
{
$freq[$field['type']] = 1;
}
}

// Wrong field setup
if( 0 < count( array_merge( array_diff_assoc( $must_have, $freq ), array_diff_assoc( $freq, $must_have ) ) ) )
{
// Method 1: Exit
//wp_die( 'Required Fields missing!' );

// Method 2: Drastically remove all fields
// $gform_meta['fields'] = array();
// $_POST['gform_meta'] = json_encode( $gform_meta );

// Method 3: Trim out all unwanted fields ?
// ...

// Method 4: Show error messages
add_action( 'admin_notices', 'custom_admin_notices' );
}
}
}
} );


/**
* Custom Error messages
*/

function custom_admin_notices()
{
printf( '<div class="error"><p>%s</p></div>', __( 'Required Fields missing!' ) );
}


/*

List of field types:

Standard Fields:
Single Text Line - text
Drop Down - select
Number - number
Radio Buttons - radio
HTML - html
Page Break - page
Paragraph Text - textarea
Multi Select - multiselect
Checkboxes - checkbox
Hidden - hidden
Section Break - section

Advanced Fields:
Name - name
Time - time
Address - address
Email - email
File Upload - fileupload
Date - date
Phone - phone
Website - website
Password - password

Post Fields:
Title - post_title
Excerpt - post_excerpt
Category - post_category
Body - post_content
Tags - post_tags
Image - post_image
*/


macster448 comments:

That is a great solution, thank you!

My only issue with it is, once the error message is displayed your only option is to press the back button in your browser. Then when you go back to the form all of the fields are gone and you have to start again.

Is there a way to just display a notification when 'Update Form' is clicked rather than take them to a new error page?


macster448 comments:

I don't necessarily need to prevent them from submitting the form, I just need to be able to display a warning to them (Maybe at the top of the page) notifying them that they need to add the required fields


Dbranes comments:

There's a hook like <em>gform_after_save_form</em> but no <em>gform_before_save_form</em>.

There is on the other hand a javascript hook <em>gform_pre_form_editor_save</em>.

I updated the answer with a <em>Custom Error Message</em> (Method 4).


Dbranes comments:

I updated the answer with the list of field types, that might be helpful to construct the <em>$must_have</em> array.


macster448 comments:

I have tried to implement with this....

<?php
add_action( 'admin_init', function(){
if( isset( $_POST['gform_meta'] ) )
{
$gform_meta = json_decode( stripslashes_deep( $_POST['gform_meta'] ), TRUE );
$freq = array();

//-------------------------------------------------------
// EDIT:
// Must have field types and their corresponding frequencies.
// For example the setup with 3 x 'text' fields + 1 x 'select' field, is denoted:
//
$must_have = array( 'myfield' => 1);
//-------------------------------------------------------

if( isset( $gform_meta['fields'] ) )
{
$fields = $gform_meta['fields'];
foreach( (array) $fields as $field )
{
echo $field['type'];
echo '<br />';
if( isset( $freq[$field['type']] ) )
{
$freq[$field['type']]++;
}
else
{
$freq[$field['type']] = 1;
}
}

// Wrong field setup
if( 0 < count( array_merge( array_diff_assoc( $must_have, $freq ), array_diff_assoc( $freq, $must_have ) ) ) )
{
// Method 4: Show error messages
add_action( 'admin_notices', 'custom_admin_notices' );
}
}
}
} );


/**
* Custom Error messages
*/
function custom_admin_notices()
{
printf( 'Error - Required Field Not Added' );
}

?>


This works fine and displays the error message if my custom field is not present (myfield)

If I then add myfield and click update form then the message dissapears. But if I then add another field, for example a Select Dropdown, then it displays the error message again.

Am I doing something obviously wrong?


Dbranes comments:

<blockquote>If I then add myfield and click update form then the message dissapears.
But if I then add another field, for example a Select Dropdown, then it displays the error message again.</blockquote>

If you have:

$must_have = array( 'text' => 1);

then this is the progress:


- no fields
- press update
- error message show up
- add a 'text' field
- press update
- error message disappear
- add a 'select' field
- press update
- error message show up


so the <em>must-have</em> fields must match the added fields.



Dbranes comments:

i.e. the current setup is an exact match (1-1 correspondance)


Dbranes comments:

i.e. there must be an exact match of field type frequency.


macster448 comments:

I understand, but in my instance I want it to just ensure that AT LEAST the required fields have been added, I need the error message to only display if the form does not contain them.

I need to be able to add other fields as well!


Dbranes comments:

You can then try replacing:

// Wrong field setup - Exit
if( 0 < count( array_merge( array_diff_assoc( $must_have, $freq ), array_diff_assoc( $freq, $must_have ) ) ) )


with

// Wrong field setup - Exit
if( 0 < count( array_diff_assoc( $must_have, $freq ) ) )


Dbranes comments:

... but it's probably best for you to make a loop and compare field types and freq. (>=)