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

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

<?php
$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:

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

And mymodule_url_prepend_protocol() will look like this:

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

Jaypan
Our newest Drupal site: PacificAikido.com (Drupal showcase)

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;
?>

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:

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

Jaypan
Our newest Drupal site: PacificAikido.com (Drupal showcase)