View previous topic :: View next topic |
Author |
Message |
d.runk3n Smarty Rookie
Joined: 07 Feb 2005 Posts: 5
|
Posted: Mon Feb 07, 2005 9:02 am Post subject: SmartyValidate and form arrays |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Feb 07, 2005 3:10 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Feb 07, 2005 5:14 pm Post subject: |
|
|
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 |
|
d.runk3n Smarty Rookie
Joined: 07 Feb 2005 Posts: 5
|
Posted: Tue Feb 08, 2005 12:31 am Post subject: |
|
|
Wow, thanks for that.
I'll give it a go. |
|
Back to top |
|
d.runk3n Smarty Rookie
Joined: 07 Feb 2005 Posts: 5
|
Posted: Tue Feb 08, 2005 2:27 am Post subject: |
|
|
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 |
|
d.runk3n Smarty Rookie
Joined: 07 Feb 2005 Posts: 5
|
Posted: Tue Feb 08, 2005 2:30 am Post subject: |
|
|
Sorry, let me do some further testing and rephrase that. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Feb 08, 2005 2:30 am Post subject: |
|
|
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 I doubt I'll mess with it unless there is a lot of requests for it. |
|
Back to top |
|
d.runk3n Smarty Rookie
Joined: 07 Feb 2005 Posts: 5
|
Posted: Tue Feb 08, 2005 3:35 am Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Feb 08, 2005 4:30 am Post subject: |
|
|
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 |
|
mjerom Smarty Rookie
Joined: 09 Feb 2005 Posts: 9
|
Posted: Wed Feb 09, 2005 6:20 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Feb 09, 2005 8:04 pm Post subject: |
|
|
The notice is fixed in CVS, please give it a try.
Thanks! |
|
Back to top |
|
mjerom Smarty Rookie
Joined: 09 Feb 2005 Posts: 9
|
Posted: Thu Feb 10, 2005 3:29 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Feb 10, 2005 5:11 pm Post subject: |
|
|
ack!
Its fixed in CVS. Give it another try |
|
Back to top |
|
mjerom Smarty Rookie
Joined: 09 Feb 2005 Posts: 9
|
Posted: Thu Feb 10, 2005 5:18 pm Post subject: |
|
|
SmartyValidate.class.php/1.33/Thu Feb 10 17:17:09 2005//
sorry not better...
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
Last edited by mjerom on Thu Feb 10, 2005 5:28 pm; edited 1 time in total |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Feb 10, 2005 5:25 pm Post subject: |
|
|
Committed another fix to CVS, wasn't handling non empty arrays correctly... |
|
Back to top |
|
|