Skip to content

Instantly share code, notes, and snippets.

@luistinygod
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luistinygod/03913dcbd4263d9bac77 to your computer and use it in GitHub Desktop.
Save luistinygod/03913dcbd4263d9bac77 to your computer and use it in GitHub Desktop.
GravityView Make (some) Search Fields Required
<?php
/**
* If you need to make some of the GravityView search fields required before search is submited
* - add as many lines as you need to define which fields are required
* e.g. -> jQuery('#gv_search_XYZZ').prop('required', true );
*
* Add this gist to your theme functions.php
*
*/
add_action('gravityview_after', 'my_theme_make_search_fields_required' );
function my_theme_make_search_fields_required() {
?>
<script>
//define here the search fields that are required
jQuery('#gv_search_2247').prop('required', true );
// validation magic
jQuery('form.gv-widget-search').submit( function(e) {
e.stopPropagation();
var isValid = true;
jQuery('.gv-widget-search input:required').each( function(){
if( jQuery(this).val() === '' ) {
isValid = false;
}
});
return isValid;
});
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment