Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

SmartyValidate and form arrays
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Smarty Forum Index -> Add-ons
View previous topic :: View next topic  
Author Message
d.runk3n
Smarty Rookie


Joined: 07 Feb 2005
Posts: 5

PostPosted: Mon Feb 07, 2005 9:02 am    Post subject: SmartyValidate and form arrays Reply with quote

Hi everyone,

I usually have my form variables submitted as multidimensional arrays, but I seem to be having trouble getting SmartyValidate to validate these variables.

This is an examples of my form field:

Code:
<input type="text" name="contactDetails[name]" />


Here is what I've tried to validate with:

Code:
{validate field="contactDetails.name" criteria="notEmpty" transform="trim" message="Full Name Cannot Be Empty"}
{validate field="contactDetails[name]" criteria="notEmpty" transform="trim" message="Full Name Cannot Be Empty"}
{validate field="contactDetails['name']" criteria="notEmpty" transform="trim" message="Full Name Cannot Be Empty"}


Can anyone help?
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Mon Feb 07, 2005 3:10 pm    Post subject: Reply with quote

SmartyValidate can currently validate zero-indexed arrays, such as:

<input type=text name=foobar[]>

But it currently does not have support for name-keyed arrays such as foobar[blah]

You could easily build a custom validator to handle it:

{validate field="foobar[blah]" criteria="keyNotEmpty" ... }

or

{validate field="foobar" key="blah" criteria="keyNotEmpty" ... }

I'll see about support for name-keyed arrays
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


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

PostPosted: Mon Feb 07, 2005 5:14 pm    Post subject: Reply with quote

I've committed to CVS the support for array keys.

examples:

{validate field="foo[bar]" ... } {* apply to one array element *}
{validate field="foo[]" ... } {* apply to each array element *}
{validate field="foo" ... } {* same as above *}

The last two are identical, the validator will apply to each array value.

Please test and give feedback!
Back to top
View user's profile Send private message Visit poster's website
d.runk3n
Smarty Rookie


Joined: 07 Feb 2005
Posts: 5

PostPosted: Tue Feb 08, 2005 12:31 am    Post subject: Reply with quote

Wow, thanks for that.

I'll give it a go.
Back to top
View user's profile Send private message
d.runk3n
Smarty Rookie


Joined: 07 Feb 2005
Posts: 5

PostPosted: Tue Feb 08, 2005 2:27 am    Post subject: Reply with quote

Ok, so I've done some testing, and it seems to work fine.

But it doesn't seem to work for multidimensional arrays. I've gotten around this by making the form data less complicated; however is it difficult to get it working with multidimensional arrays also?
Back to top
View user's profile Send private message
d.runk3n
Smarty Rookie


Joined: 07 Feb 2005
Posts: 5

PostPosted: Tue Feb 08, 2005 2:30 am    Post subject: Reply with quote

Sorry, let me do some further testing and rephrase that.
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Tue Feb 08, 2005 2:30 am    Post subject: Reply with quote

I know it doesn't, and it's not as trivial. multi-dimensional arrays, I don't think I've ever seen or used that on a form Wink I doubt I'll mess with it unless there is a lot of requests for it.
Back to top
View user's profile Send private message Visit poster's website
d.runk3n
Smarty Rookie


Joined: 07 Feb 2005
Posts: 5

PostPosted: Tue Feb 08, 2005 3:35 am    Post subject: Reply with quote

I did notice something else though, say I had a form like this:

Code:
<input type="text" name="contactDetails[name]" />
<input type="text" name="contactDetails[email]" />


It will only validate one field at a time. So if I were to submit a blank form, an error would only be displayed for the name field. The email error won't actually come up until the name field is valid.

Which files should I look at if I were to have a look at this myself? I doubt I'll have the time, but I might be able to take a quick look at it to understand it better.[/code]
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Tue Feb 08, 2005 4:30 am    Post subject: Reply with quote

You can get an error for both:

{validate field="contactDetails[name]" criteria="notEmpty" message="foo" append="bar"}
{validate field="contactDetails[email]" criteria="notEmpty" message="foo" append="bar"}
Back to top
View user's profile Send private message Visit poster's website
mjerom
Smarty Rookie


Joined: 09 Feb 2005
Posts: 9

PostPosted: Wed Feb 09, 2005 6:20 pm    Post subject: Reply with quote

Hi everybody,
the cvs code is working good,
still i get a error (notice) :

Notice: Array to string conversion in /var/www/html/_DEV/redust/inc/smarty/SmartyValidate.class.php on line 203

no pb so far !

thx for your great work !
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Wed Feb 09, 2005 8:04 pm    Post subject: Reply with quote

The notice is fixed in CVS, please give it a try.

Thanks!
Back to top
View user's profile Send private message Visit poster's website
mjerom
Smarty Rookie


Joined: 09 Feb 2005
Posts: 9

PostPosted: Thu Feb 10, 2005 3:29 pm    Post subject: Reply with quote

glup :

->from tpl
{validate form="track_form" field="liste[]" criteria="notEmpty" message="Vous devez s&lectionner au moins un artiste !"}

->resulting errors
Notice: Undefined variable: fromvars in /var/www/html/_DEV/redust/inc/smarty/SmartyValidate.class.php on line 204

Notice: Undefined variable: fromvars in /var/www/html/_DEV/redust/inc/smarty/SmartyValidate.class.php on line 204

Notice: Array to string conversion in /var/www/html/_DEV/redust/inc/smarty/SmartyValidate.class.php on line 204


[edit]
line 204 you wrotte fromvars instead of formvars
[/edit]

much better now
but I still get the notice ...

Notice: Array to string conversion in /var/www/html/_DEV/redust/inc/smarty/SmartyValidate.class.php on line 204

here is line 204
((is_array($formvars[$_field]) && count($_field) == 0) || strlen($formvars[$_field]) == 0) && $_empty

I tried
((is_array($formvars[$_field]) && count($_field) == 0) ) && $_empty

and the notice disapeared...


Last edited by mjerom on Thu Feb 10, 2005 5:15 pm; edited 1 time in total
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Feb 10, 2005 5:11 pm    Post subject: Reply with quote

ack!

Its fixed in CVS. Give it another try Wink
Back to top
View user's profile Send private message Visit poster's website
mjerom
Smarty Rookie


Joined: 09 Feb 2005
Posts: 9

PostPosted: Thu Feb 10, 2005 5:18 pm    Post subject: Reply with quote

SmartyValidate.class.php/1.33/Thu Feb 10 17:17:09 2005//
sorry not better...

Question maybe it's stupid but :
can't you substitute
strlen($formvars[$_field]) == 0
with
$formvars[$_field] == ""

I don't know about the consequence of this because
...I don't know that much PHP
Laughing


Last edited by mjerom on Thu Feb 10, 2005 5:28 pm; edited 1 time in total
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Feb 10, 2005 5:25 pm    Post subject: Reply with quote

Committed another fix to CVS, wasn't handling non empty arrays correctly...
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Add-ons All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
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