Thanks for this explanation, Helmut
I'm curious... there used to be an inbuilt 'url' rgxp
Code:
'eval' => array('mandatory'=>false, 'maxlength'=>124, 'rgxp'=>'url', 'tl_class'=>'w50')
but it doesn't work any more (i'm sure it used to!), and is not mentioned anymore in the documentation.
Was it removed?
EDIT: I see it exists still, but only validates that the characters used are valid, not that the URL itself is.
The best URL validation I can come up with (after testing heaps of CRAP regular expressions) is below.
I've used Helmuts hook code and added PHP's filter_var function.
This will check that it has http://, a domain, and a path after the domain (which can just be /).
Its not perfect, eg: http://a/ will validate, but it does allow complex URLs like http://www.youtube.com/watch?v=Y-8nJ1p4Vkg
The difficulty with any validation is the .com part, as there are too many possibilities, like .co.nz or .travel etc.
If you don't want a trailing path to be enforced, just remove the
Code:
, FILTER_FLAG_PATH_REQUIRED
bit.
Heres more on it all, including info on validating other types of input...
http://mattiasgeniar.be/2009/02/07/i...r-expressions/
and the code for a custom rgxp of type 'website'
In the field you want to validate in your DCA:
Code:
'eval' => array('rgxp'=>'website')
Add the hook in your config
Code:
$GLOBALS['TL_HOOKS']['addCustomRegexp'][] = array('MyClass', 'addCustomRegexp');
And the function in your 'MyClass' class
Code:
public function addCustomRegexp($strRegexp, $varValue, Widget $objWidget)
{
//read http://mattiasgeniar.be/2009/02/07/i...r-expressions/ for other options
if ($strRegexp == 'website')
{
if (!filter_var($varValue, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED))
{
$objWidget->addError($objWidget->label . ' should be a Full URL.');
}
return true;
}
return false;
}
Bookmarks