PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Ensure 'Required' box is ticked by default

  1. Mark
    Member

    Hi there

    I'd like to ensure ALL fields when added to forms are required. Tried adding a 'checked="yes"' to the input type declaration but didn't seem to do it.

    Any ideas?

    Posted 13 years ago on Wednesday September 14, 2011 | Permalink
  2. Rather than having to mark each field required when creating a new form, you want the default to be required for all fields you add. Is that correct? That way the default for a form would be all fields required rather than no fields required.

    Posted 13 years ago on Wednesday September 14, 2011 | Permalink
  3. Mark
    Member

    Correct! All added fields should be 'required' as default.

    Posted 13 years ago on Thursday September 15, 2011 | Permalink
  4. I am checking with the developers to see if there is a filter you can use to make this happen. I'll post when I find out. Thanks for your patience.

    Posted 13 years ago on Thursday September 15, 2011 | Permalink
  5. Alex provided the following code:

    [php]
    add_filter("gform_pre_render", "set_required");
    add_filter("gform_validation", "check_required");
    
    function set_required($form){
        foreach($form["fields"] as &$field)
            $field["isRequired"] = true;
    
        return $form;
    }
    
    function check_required($validation_result){
        foreach($validation_result["form"]["fields"] as &$field){
    
            if(GFFormDisplay::is_empty($field, $validation_result["form"]["id"])){
                $field["failed_validation"] = true;
                $field["validation_message"] = "This field is required.";
                $validation_result["is_valid"] = false;
            }
        }
        return $validation_result;

    One side effect of doing it this way is that the form builder will NOT show that fields are required (because they're not at that point.) They are made required when the form is displayed to a visitor.

    There are two functions, two filters there. The set_required function will make each field required before the form is rendered. The check_required handles the validation upon submit.

    Let me know if you need any assistance implementing this. This code will be added to your functions.php file in your current theme.

    http://www.gravityhelp.com/documentation/page/Where_Do_I_Put_This_Code%3F

    Posted 13 years ago on Thursday September 15, 2011 | Permalink