Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Smarty Validate and Regular expressions

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Add-ons
View previous topic :: View next topic  
Author Message
gestionti
Smarty Rookie


Joined: 02 Mar 2005
Posts: 15
Location: Gatineau, Qc, Canada

PostPosted: Tue Mar 08, 2005 10:58 pm    Post subject: Smarty Validate and Regular expressions Reply with quote

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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Mar 08, 2005 11:08 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue Mar 08, 2005 11:10 pm    Post subject: Reply with quote

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 Wink
Back to top
View user's profile Send private message
gestionti
Smarty Rookie


Joined: 02 Mar 2005
Posts: 15
Location: Gatineau, Qc, Canada

PostPosted: Wed Mar 09, 2005 12:20 am    Post subject: Reply with quote

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
View user's profile Send private message
gestionti
Smarty Rookie


Joined: 02 Mar 2005
Posts: 15
Location: Gatineau, Qc, Canada

PostPosted: Wed Mar 09, 2005 12:29 am    Post subject: Reply with quote

Never mind!! I figured it out :

!^[a-zA-Z]{2,}$!

What is the purpose of the exclamation mark anyway(!)?
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Mar 09, 2005 2:40 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Add-ons All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP