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.

Website field - not validating without http://

  1. I've setup the website field in my form, however it's not validating unless the user specifically types the http:// at the start of the address. So something like "www.test.com" throws up the "Please enter a valid Website URL (i.e. http://www.gravityforms.com)." error.

    That makes things difficult, as even with the http:// placeholder in the field, I don't think many users will actually type it.

    Is this normal behaviour, or is there a way to allow it?

    Form is here: http://ridemusic.net/fans/#form-story-tab

    Thanks.

    Posted 12 years ago on Friday May 4, 2012 | Permalink
  2. kyle
    Member

    You could just use a standard text field, but the URL should have http:// to be valid. Maybe you can add some help text as a reminder? Ride fans are smart (great band), they'll figure it out!

    Posted 12 years ago on Friday May 4, 2012 | Permalink
  3. gecko_guy
    Member

    Hi, I agree that people will figure it out, but only after they have clicked "submit" and the form fails validation, and causes irritation.

    Is there a functional reason for it to work this way? Does the field have the potential to be used in some special way making the http prefix necessary or to somehow prevent spam? Wouldn't it be better to have the http:// prefix hard-coded if the form requires it?

    Thanks

    Posted 12 years ago on Wednesday July 11, 2012 | Permalink
  4. David Peralty

    You could also add a description to the field that says "http:// required". It just makes it easily usable by novices in other scripts and whatnot that they need. Rather than having them add that in through PHP later if they want to make a link.

    Posted 12 years ago on Wednesday July 11, 2012 | Permalink
  5. gecko_guy
    Member

    Yes, its what I've done and made it pretty with a bit of CSS, but would have preferred the http:// to simply be hard-coded in to the field. I understand the logic of requiring it as part of the validation process and that it may be needed for more advanced scripts, but if it is required I don't really understand why it has to be re-typed.

    Its not really a problem, just a mild niggle.

    Posted 12 years ago on Wednesday July 11, 2012 | Permalink
  6. David Peralty

    I completely understand. Again, you don't have to use the URL field, which would allow you to create your own validation rules, or have no validation rules if you prefer.

    Posted 12 years ago on Wednesday July 11, 2012 | Permalink
  7. Phil
    Member

    Here's a possible workaround. Time will tell if it breaks something. I have only tested on OSX versions of the latest Chrome, Firefox, Safari, and Opera, and not yet on a production site. Note that it probably disables browsers' validation of all HTML5 fields in the form.

    1. The function my_novalidate() disables browsers' validation of HTML5 fields like input type="url" fields. Without disabling, I think some browsers (Chrome/Firefox) block the form from even being submitted if "http://"" is missing -- not a Gravity Forms issue, because GF never even receives the data. Opera just adds the http://. Safari doesn't care about missing http://, but then GF will invalidate.
    2. The function my_protocol() then adds "http://" if the user omitted a protocol. This happens before the field value makes it to GF's validation procedure.

    Here are my settings for making this work.

    • GF 1.6+ (I'm on 1.6.12)
    • HTML5 enabled everywhere
    • Use website field type (instead of text field). This uses input type="url" (vs. type="text"), which may have advantages of bringing up custom keyboards on mobile devices.
    • Don't set up any kind of dynamic population of "http://"

    I hope the code isn't too long here -- some dislike links to code on external sites.

    // add the 'novalidate' setting to <form> tag
    // stackoverflow.com/questions/3090369/
    function my_novalidate($form_tag, $form) {
    
      // collect field types
      $types = array();
      foreach ( $form['fields'] as $field ) {
        $types[] = $field['type'];
      }
    
      // bail if form doesn't have a website field
      if ( ! in_array('website', $types) )
        return $form_tag; 
    
      // add the 'novalidate' setting to the website <form> element
      $pattern = "#method=\'post\'#i";
      $replacement = "method='post' novalidate";
      $form_tag = preg_replace($pattern, $replacement, $form_tag);
    
      return $form_tag;
    }
    add_filter('gform_form_tag','my_novalidate',10,2);
    
    // add "http://" to website if protocol omitted
    function my_protocol($form) {
    
      // loop through fields, taking action if website
      foreach ( $form['fields'] as $field ) {
    
      // skip if not a website field
      if ( 'website' != $field['type'] )
          continue;
    
      // retrieve website field value
      $value = RGFormsModel::get_field_value($field);
    
      // if there is no protocol, add "http://"
      // Recognizes ftp://, ftps://, http://, https://
      // stackoverflow.com/questions/2762061/
      if ( ! empty($value) && ! preg_match("~^(?:f|ht)tps?://~i", $value) ) {
        $value = "http://" . $value;
    
        // update value in the $_POST array
        $id = (string) $field['id'];
        $_POST['input_' . $id] = $value;
      }
      }
    
      return $form;
    }
    add_filter('gform_pre_validation','my_protocol');

    GF admins: If the code above is helpful, would it be appropriate to put a link to this post in past related threads here and here, for those coming across those posts while looking for ideas on this issue (like I was)? Maybe this is all beyond the scope of the forums.

    Posted 11 years ago on Thursday February 28, 2013 | Permalink
  8. Thanks for posting that code Phil. If you would like to add links to those other topic, please feel free to do so.

    Posted 11 years ago on Monday March 4, 2013 | Permalink
  9. Phil
    Member

    UPDATE
    A typo In my code above: I returned the wrong variable name on line 13. It should read return $form_tag; instead of return $content;. Otherwise, when a form does not have a website field, the markup for the opening <form> tag will not be returned/printed and the form will not function.

    Posted 11 years ago on Tuesday March 5, 2013 | Permalink
  10. @Phil, I fixed the original code.

    Posted 11 years ago on Wednesday March 6, 2013 | Permalink
  11. Phil
    Member

    UPDATE #2
    My code above works for a website field, but not for a post custom field whose type is set to website. In the latter case, $field['type'] is 'post_custom_field' instead of 'website'. However, in both cases, $field['inputType'] is 'website', so we can use it instead. Given that $field['inputType'] is not set for all fields (e.g., honeypot), we'll include an isset() check to avoid the php "Notice: Undefined Index". So...

    1. Replace line 8 above ($types[] = $field['type'];) with
      if ( isset($field['inputType']) )
        $types[] = $field['inputType'];
    2. Then replace line 31 above (if ( 'website' != $field['type'] )) with
      if ( ! isset($field['inputType']) || 'website' != $field['inputType'] )
    Posted 11 years ago on Saturday June 29, 2013 | Permalink

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