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.

Make form required, yet still display the default values

  1. sgarten
    Member

    Hi,

    How can I make my form have required fields when using default values?

    For aesthetics, I have the labels hidden and I'm using values to display / direct the user for which information I want ( name & email ). In my form I have both fields set to "required".

    Currently, if you click the button "request an invitation" it will submit. However, I want to make sure that people input their name and/or at least their e-mail address before the form will actually submit. Is there a way to use default values, but make it so people have to enter in new text?

    You can see the form on my hope page: http://virtuecard.org

    Thanks!

    Posted 4 years ago on Wednesday June 26, 2013 | Permalink
  2. David Peralty
    Administrator

    You would have to add custom validation that then makes those default values not an acceptable value. To do this, you would use the following hook: http://www.gravityhelp.com/documentation/page/Gform_validation

    Posted 4 years ago on Wednesday June 26, 2013 | Permalink
  3. sgarten
    Member

    Thanks, David.

    I'm confused. Can you please help me further?

    This form I'm using is form "13" and only has those two fields which are field 4 (name) and field 3 (email).

    Specifically, what code do I need to add to make this work and where do I put it?

    Posted 4 years ago on Wednesday June 26, 2013 | Permalink
  4. David Peralty
    Administrator

    The code goes in your theme's functions.php file, and you would have to look at the examples and create the code needed to check those two fields. You would compare them against the default values you have in your form. So basically if the name field has the default value, then return an error, otherwise, do nothing because it is fine.

    Posted 4 years ago on Wednesday June 26, 2013 | Permalink
  5. sgarten
    Member

    Can you please help me? Here's what I tried, but it didn't work.

    First, you can view the form at http://www.virtuecard.org. All I want, are for the fields to be required, but since I'm using default values someone can click the "request an invitation" button and it will submit the default values "name" and "email". However, if they do click this I want it to give an error message. Make sense?

    The form ID is 13 and the "name" field is field 4 and the "email" field is field 3.

    Here's the code I added to my functions.php file that didn't work. I only attempted fixing field 4, but since that didn't work, I didn't code out for field 3 yet.

    Can you please tell me the correct code I should use?
    -----

    <?php

    add_filter('gform_validation_13', 'custom_validation');
    function custom_validation($validation_result){
    $form = $validation_result["form"];

    //supposing we don't want input 1 to be a value of 86
    if($_POST['input_4'] == name){

    // set the form validation to false
    $validation_result["is_valid"] = false;

    //finding Field with ID of 1 and marking it as failed validation
    foreach($form["fields"] as &$field){

    //NOTE: replace 1 with the field you would like to validate
    if($field["id"] == "4"){
    $field["failed_validation"] = true;
    $field["validation_message"] = "This field is invalid!";
    break;
    }
    }

    }

    //Assign modified $form object back to the validation result
    $validation_result["form"] = $form;
    return $validation_result;

    }

    ?>

    Posted 4 years ago on Friday June 28, 2013 | Permalink
  6. Richard Vav
    Administrator

    If you still require assistance with this please open a new support ticket or a priority support ticket if you are a developer license holder. Thank you.

    Posted 4 years ago on Saturday July 27, 2013 | Permalink

This topic has been resolved and has been closed to new replies.