In the user profile page of my website, there is a field in url field type, which allows user to enter their website url. But I found that it only works when user entering the url with prefix http:// or https://. However, most users only enter their websites as www.mywebsite.com, without the prefix.

Is their any way to enforce users to enter the http://, or automatically add the "http://" part if users did not enter it? Thanks.

Comments

Jaypan’s picture

You can do this. Let's assume your form element is as follows:

$form['url'] = array
(
  '#type' => 'textfield',
  '#title' => t('URL'),
);

You can add a validation function in which you add the http:// on if it does not exist:

$form['url'] = array
(
  '#type' => 'textfield',
  '#title' => t('URL'),
  '#element_validate' => array('mymodule_url_prepend_protocol'),
);

And mymodule_url_prepend_protocol() will look like this:

function mymodule_url_prepend_protocol($element, $form_state)
{
  if(!preg_match('/^http:\/\//', $form_state['values']['url']))
  {
    form_set_value($element, 'http://' . $form_state['values']['url'], $form_state);
  }
}

This will prepend the protocol (http://) to the front of any URL that does not begin with http://, and will append it before any other validation occurs for the form in general, and on this element.

tce’s picture

I would personally do this in the submit function, by using the ltrim function.

<?php
$url = 'blah.com' // a url the user entered
$url = ltrim($url, "http://");
$url = ltrim($url, "https://");
$url = "http://" . $url;
?>
Jaypan’s picture

Doing it in the submit function will work. However, it should be noted that any validation is then done on the original string, which may or may not contain the protocol (http://).

Attaching the protocol using the #element_validate attribute of the field adds consistency to your system by ensuring that the URL containing the protocol is present for all validation and submission functions.

For example:

function does_something_form($form, &$form_state)
{
  $form['url'] = array
  (
    '#type' => 'textfield',
    '#title' => t('URL'),
    '#element_validate' => array('mymodule_url_prepend_protocol'),
  );
}

function does_something_form_validate($form, &$form_state)
{
  if(!strpos($form_state['values']['url'], 'http://') === 0)
  {
    form_set_error('url', t('Please include the protocol (http://) in the URL'));
  }
}

function mymodule_url_prepend_protocol($element, $form_state)
{
  // Attach protocol (http://) if it is missing
}

It may seem redundant to set a validation check in the form's _validate() function, when the form element itself is pre-pending the protocol (if necessary) in the element's validation callback. However, this redundancy ensures that if someone alters the form in some way in the future, there is a backup test to ensure the protocol is appended. This means that the data entered into the database will always be required to have the protocol, ensuring data consistency.

FireHawkX’s picture

First, I AM SOOO SORRY to revive a 7 year old thread...
but this is like the best one that matches most closely the problem I have and that I need to fix

I am making an small intranet with many functions and features, over 200 modules,
which also includes a "companies directory" node type.
In it, the user can enter the company website url, however,
most of them are entered like this "website.com" without the http or https

I am also using the module "clientside validation" so that if there are validation error,
they show up before the save command has been sent to the server,
otherwise the website breaks css if it returns a validation error on save and everything is displayed wrong.

Keep in mind I have never really used any php code or made any edits to any modules myself
(I did manually apply patches in notepad++ when they would not apply) but thats about it

I tried making a new module based on the info provided by Jaypan above,
addhttp.module :

<?php
$form['url'] = array
(
  '#type' => 'textfield',
  '#title' => t('URL'),
  '#element_validate' => array('mymodule_url_prepend_protocol'),
);

function mymodule_url_prepend_protocol($element, $form_state)
{
  if(!preg_match('/^http:\/\//', $form_state['values']['url']))
  {
    form_set_value($element, 'http://' . $form_state['values']['url'], $form_state);
  }
}

and my addhttp.info :
name = Add HTTP
description = Wonder if it will work
core = 7.x

Sadly it didnt work :) not surprising... any suggestion on how to make it work?

Thank you very much for your time and for reading and have a nice day!