|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
gestionti Smarty Rookie
Joined: 02 Mar 2005 Posts: 15 Location: Gatineau, Qc, Canada
|
Posted: Tue Mar 08, 2005 10:58 pm Post subject: Smarty Validate and Regular expressions |
|
|
I have an error when I use such an expression : ^[a-zA-Z]{2,}$ with smartyvalidate (criteria ="isRegExp". It does not seem to like the curly braces. Can curlybraces be used with SmartyValidate
Mario |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Mar 08, 2005 11:08 pm Post subject: |
|
|
That is because they conflict with the template delimiters. The best solution is to create a custom validation criteria like criteria="twoPlusChars" And use that instead of putting the regex directly in the template. As a matter of fact, you should always use custom criteria instead of the supplied RegExp criteria function, that "feature" was added with little forethought, this isn't the right place to do regex. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Mar 08, 2005 11:10 pm Post subject: |
|
|
You can do any of the following:
1) change your smarty delimiters to something other than { and }
2) use {ldelim} and {rdelim} in place of { and } respectively
3) use {capture assign=foo} around a {literal} block that contains your text
That said, in-template regex's are bad form. You are better off creating a custom plugin validator that hides the regex implementation from your template users.
EDIT: oops, cross-posted with Monte. At least we have the same advice |
|
Back to top |
|
gestionti Smarty Rookie
Joined: 02 Mar 2005 Posts: 15 Location: Gatineau, Qc, Canada
|
Posted: Wed Mar 09, 2005 12:20 am Post subject: |
|
|
Guys, Thanks for such a quick reply. I like the idea of making my own custom validation criteria. I tried but for some reason, what ever I input in my form gets rejected by the validate I made. It goes like so:
function smarty_validate_criteria_isTwoPlusChars($value, $empty, &$params, &$formvars) {
if(strlen($value) == 0)
return $empty;
return preg_match('^[a-zA-Z]$', $value);
}
I neglected the {2,} for now as I can't get the basic work... What am I doing wrong? |
|
Back to top |
|
gestionti Smarty Rookie
Joined: 02 Mar 2005 Posts: 15 Location: Gatineau, Qc, Canada
|
Posted: Wed Mar 09, 2005 12:29 am Post subject: |
|
|
Never mind!! I figured it out :
!^[a-zA-Z]{2,}$!
What is the purpose of the exclamation mark anyway(!)? |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Mar 09, 2005 2:40 am Post subject: |
|
|
gestionti wrote: | What is the purpose of the exclamation mark anyway(!)? |
They are delimiters and needn't necessarily be question marks. For details, see the first paragraph of http://php.net/pcre |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|