Skip to content

Commit

Permalink
Add 'valid_url' rule to Form Validation (issue bcit-ci#1966)
Browse files Browse the repository at this point in the history
  • Loading branch information
narfbg committed Nov 26, 2012
1 parent 61797f6 commit daaca88
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
42 changes: 42 additions & 0 deletions system/libraries/Form_validation.php
Expand Up @@ -1083,6 +1083,48 @@ public function exact_length($str, $val)


// -------------------------------------------------------------------- // --------------------------------------------------------------------


/**
* Valid URL
*
* @param string $str
* @return bool
*/
public function valid_url($str)
{
if (empty($str))
{
return FALSE;
}
elseif (preg_match('/^(?:([^:]*)\:)?\/\/(.+)$/', $str, $matches))
{
if (empty($matches[2]))
{
return FALSE;
}
elseif ( ! in_array($matches[1], array('http', 'https'), TRUE))
{
return FALSE;
}

$str = $matches[2];
}

$str = 'http://'.$str;

// There's a bug affecting PHP 5.2.13, 5.3.2 that considers the
// underscore to be a valid hostname character instead of a dash.
// Reference: https://bugs.php.net/bug.php?id=51192
if (version_compare(PHP_VERSION, '5.2.13', '==') === 0 OR version_compare(PHP_VERSION, '5.3.2', '==') === 0)
{
sscanf($str, 'http://%[^/]', $host);
$str = substr_replace($str, strtr($host, array('_' => '-', '-' => '_')), 7, strlen($host));
}

return (filter_var($str, FILTER_VALIDATE_URL) !== FALSE);
}

// --------------------------------------------------------------------

/** /**
* Valid Email * Valid Email
* *
Expand Down
5 changes: 3 additions & 2 deletions user_guide_src/source/changelog.rst
Expand Up @@ -220,11 +220,12 @@ Release Date: Not Released
- Added method ``reset_validation()`` which resets internal validation variables in case of multiple validation routines. - Added method ``reset_validation()`` which resets internal validation variables in case of multiple validation routines.
- Added support for setting error delimiters in the config file via ``$config['error_prefix']`` and ``$config['error_suffix']``. - Added support for setting error delimiters in the config file via ``$config['error_prefix']`` and ``$config['error_suffix']``.
- ``_execute()`` now considers input data to be invalid if a specified rule is not found. - ``_execute()`` now considers input data to be invalid if a specified rule is not found.
- Removed method ``is_numeric()`` as it exists as a native PHP function and ``_execute()`` will find and use that (the *is_numeric* rule itself is deprecated since 1.6.1). - Removed method ``is_numeric()`` as it exists as a native PHP function and ``_execute()`` will find and use that (the **is_numeric** rule itself is deprecated since 1.6.1).
- Native PHP functions used as rules can now accept an additional parameter, other than the data itself. - Native PHP functions used as rules can now accept an additional parameter, other than the data itself.
- Updated ``set_rules()`` to accept an array of rules as well as a string. - Updated ``set_rules()`` to accept an array of rules as well as a string.
- Fields that have empty rules set no longer run through validation (and therefore are not considered erroneous). - Fields that have empty rules set no longer run through validation (and therefore are not considered erroneous).
- Added rule *differs* to check if the value of a field differs from the value of another field. - Added rule **differs* to check if the value of a field differs from the value of another field.
- Added rule **valid_url**.
- Added support for setting :doc:`Table <libraries/table>` class defaults in a config file. - Added support for setting :doc:`Table <libraries/table>` class defaults in a config file.
- :doc:`Caching Library <libraries/caching>` changes include: - :doc:`Caching Library <libraries/caching>` changes include:
- Added Wincache driver. - Added Wincache driver.
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/libraries/form_validation.rst
Expand Up @@ -894,6 +894,7 @@ Rule Parameter Description
0, 1, 2, 3, etc. 0, 1, 2, 3, etc.
**is_natural_no_zero** No Returns FALSE if the form element contains anything other than a natural **is_natural_no_zero** No Returns FALSE if the form element contains anything other than a natural
number, but not zero: 1, 2, 3, etc. number, but not zero: 1, 2, 3, etc.
**valid_url** No Returns FALSE if the form element does not contain a valid URL.
**valid_email** No Returns FALSE if the form element does not contain a valid email address. **valid_email** No Returns FALSE if the form element does not contain a valid email address.
**valid_emails** No Returns FALSE if any value provided in a comma separated list is not a valid email. **valid_emails** No Returns FALSE if any value provided in a comma separated list is not a valid email.
**valid_ip** No Returns FALSE if the supplied IP is not valid. **valid_ip** No Returns FALSE if the supplied IP is not valid.
Expand Down

0 comments on commit daaca88

Please sign in to comment.