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.
- 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.
- 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