Is it posible to use a custom rgxp in a backend TL_DCA?
Hello.
I started to write a custom module for my own needs.
I see that in $GLOBALS['TL_DCA']['table']['fields']['field']['eval']['rgxp'] is possible to define several named regexes.
But is it possible to add a custom regex?
Or a custom check.
Please point me to the place in the documentation where it is is explained.
An example usage of similar custom functionality in some backend module from the core system or in an extension will also help.
Thanks in advance.
Re: Is it posible to use a custom rgxp in a backend TL_DCA?
Of course it is. Just use the addCustomRegexp hook:
[code=php:3ch0yrww]<span class="syntaxdefault"><?php
</span><span class="syntaxcomment">//*config.php
</span><span class="syntaxdefault">$GLOBALS</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'TL_HOOKS'</span><span class="syntaxkeyword">][</span><span class="syntaxstring">'addCustomRegexp'</span><span class="syntaxkeyword">][]*=*array(</span><span class="syntaxstring">'MyClass'</span><span class="syntaxkeyword">,*</span><span class="syntaxstring">'addCustomRegexp'</span><span class="syntaxkeyword">);
</span><span class="syntaxcomment">//*MyClass.php
</span><span class="syntaxkeyword">public*function*</span><span class="syntaxdefault">addCustomRegexp</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$strRegexp</span><span class="syntaxkeyword">,*</span><span class="syntaxdefault">$varValue</span><span class="syntaxkeyword">,*</span><span class="syntaxdefault">Widget*$objWidget</span><span class="syntaxkeyword">)
{
****if*(</span><span class="syntaxdefault">$strRegexp*</span><span class="syntaxkeyword">==*</span><span class="syntaxstring">'postal'</span><span class="syntaxkeyword">)
****{
********if*(!</span><span class="syntaxdefault">preg_match</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'/^0-9{4,6}$/'</span><span class="syntaxkeyword">,*</span><span class="syntaxdefault">$varValue</span><span class="syntaxkeyword">))
********{
************</span><span class="syntaxdefault">$objWidget</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">addError</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'Field*'*</span><span class="syntaxkeyword">.*</span><span class="syntaxdefault">$objWidget</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">label*</span><span class="syntaxkeyword">.*</span><span class="syntaxstring">'*should*be*a*postal*cod e.'</span><span class="syntaxkeyword">);
********}
********return*</span><span class="syntaxdefault">true</span><span class="syntaxkeyword">;
****}
****return*</span><span class="syntaxdefault">false</span><span class="syntaxkeyword">;
}
</span><span class="syntaxdefault">?></span>[/code:3ch0yrww]
Best,
Helmut
Re: Is it posible to use a custom rgxp in a backend TL_DCA?
Thank you very much.
I will try it right away.
Re: Is it posible to use a custom rgxp in a backend TL_DCA?
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;
}