Skip to content

Instantly share code, notes, and snippets.

@spivurno
Last active October 18, 2018 00:23
Show Gist options
  • Select an option

  • Save spivurno/8481177 to your computer and use it in GitHub Desktop.

Select an option

Save spivurno/8481177 to your computer and use it in GitHub Desktop.
Gravity Forms: Example of conditionally requiring a field based on the value of another field.
<?php
/**
* Gravity Forms: Example of conditionally requiring a field based on the value of another field.
*/
add_filter( 'gform_pre_render', 'gw_conditional_requirement' );
add_filter( 'gform_pre_validation', 'gw_conditional_requirement' );
function gw_conditional_requirement( $form ) {
$value = rgpost( 'input_2' );
if( $value == 'no' )
return $form;
foreach( $form['fields'] as &$field ) {
if( $field['id'] == 1 ) {
$field['isRequired'] = true;
}
}
return $form;
}
@bdgeorge

bdgeorge commented Jun 3, 2014

Copy link
Copy Markdown

Thanks - am I correct in thinking this will kick in on the form regular and Ajax POST action and then trigger the form to fail validation, but it won't create the javascript conditional logic?

@prionkor

prionkor commented Nov 1, 2016

Copy link
Copy Markdown

Can anyone comment on the above point? Need to know the JS solutions as well.

ghost commented May 3, 2018

Copy link
Copy Markdown

Awesome. Thanks!

@MatusVrsansky

Copy link
Copy Markdown

Hi, thanks for the advice, but I have one question, is that possible to get checkbox status, if is checked or not ? I have a big problem with that

@cliffordp

Copy link
Copy Markdown

In case anyone's wondering about this, this snippet is at https://docs.gravityforms.com/gform_pre_validation/ so you could ask Gravity Forms' support about "their own" snippet if you have follow-up questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment