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

SmartyValidate and form arrays
Goto page Previous  1, 2
 
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
mjerom
Smarty Rookie


Joined: 09 Feb 2005
Posts: 9

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

& it's rocking !
Very Happy

yeah
Back to top
View user's profile Send private message
gawelczyk
Smarty n00b


Joined: 28 Jan 2005
Posts: 4

PostPosted: Sun Feb 13, 2005 1:22 pm    Post subject: 2-dimensional array Reply with quote

mohrt wrote:
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.


I find it quite common in nearly every case... Let's imagine you ask end user to specify how many persons are coming with him, the answear is 3, ok, so next form displays a nice widnow asking for details of this 3 persons and you have something like
person[0][name]
person[0][surname]

and the same 2 more times for person[1] and person[2]

What then?

I tried to modify notEmpty and wrote notEmptyArray2
[php:1:859afe123e]<?php

function smarty_validate_criteria_notEmptyArray2($value, $empty, &$params, &$formvars) {

if(!isset($params['key'])) {
trigger_error("SmartyValidate: [notEmptyArray2] parameter 'key' is missing.");
return false;
}
$key = $params['key'];
if (!is_array($value) && (empty($value))){
return $empty;
}
if (isset($value[$key])){
return strlen($value[$key])>0;
}else{
return false;
}
}
?>
[/php:1:859afe123e]

when I call it within section loop
Code:

{assign var="ii" value=$smarty.section.nn.index}

{validate field="person[$ii][s_m]" key='s_m' criteria="notEmptyArray2" message="...." }

{html_radios name="person[$ii][s_m]" options=$s_m
selected=person[$ii].s_m|default:-1 labels=true separator=" "}



and any radio is not selected, it marks just the first place, for person[0], does not let go through untill you mark the radios for all persons...

almost good but... why the error message is missing for person[1] and person[2] ??

is there another work-around or way to deal with such cases?

---------------------------------------------------

all works nice after applying changes mentioned at the next post Cool


Last edited by gawelczyk on Mon Feb 14, 2005 12:02 am; edited 1 time in total
Back to top
View user's profile Send private message
gawelczyk
Smarty n00b


Joined: 28 Jan 2005
Posts: 4

PostPosted: Sun Feb 13, 2005 11:57 pm    Post subject: small and maybe usefull modyfication of SmartyValidate.class Reply with quote

mohrt wrote:
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"}


It does not work - at least in my case, I downloaded the latest version from CVS.
If you want it to work you have to change some of code in SmartyValidate.class.php

old version, that does not work:
[php:1:b64d44b52c]<?php
if(!$_sess[$_key]['valid']) {
$_failed_fields[] = $_field;
if(isset($_append))
$_smarty_assign_fields[$_append][$_field] = $_message;
if(isset($_assign))
$_smarty_assign_fields[$_assign] = $_message;
$_ret = false;
if($_sess[$_key]['halt'])
break;
}

?>[/php:1:b64d44b52c]
$_field is always array name without key (e.g. contactDetails), so when testing next key you will stop at
[php:1:b64d44b52c]<?php
if(in_array($_field, $_failed_fields)) {
// already failed, skip this test
continue;
}
?>[/php:1:b64d44b52c]


new version, modified by me
[php:1:b64d44b52c]<?php
if(!$_sess[$_key]['valid']) {
$_failed_fields[] = $_val['field'];
if(isset($_append))
$_smarty_assign_fields[$_append][$_field] = $_message;
if(isset($_assign))
$_smarty_assign_fields[$_assign] = $_message;
$_ret = false;
if($_sess[$_key]['halt'])
break;
}

?>[/php:1:b64d44b52c]

$_val['field'] contains full name, array name and key, e.g. contactDetails[email]

and testing $_failed_fields
[php:1:b64d44b52c]<?php
if(in_array($_val['field'], $_failed_fields)) {
// already failed, skip this test
continue;
}
?>[/php:1:b64d44b52c]


another, maybe worth of mentioning:

instead of:
[php:1:b64d44b52c]<?php

// field is name-keyed array, pull it apart
if(($_lpos = strpos($_field, '[')) !== false && ($_rpos = strpos($_field, ']')) !== false) {
if (($_keylen = ($_rpos - $_lpos - 1)) > 0) {
$_field_key = substr($_field, $_lpos+1, $_keylen);
}
$_field = substr($_field, 0, $_lpos);
}

?>[/php:1:b64d44b52c]

use this
[php:1:b64d44b52c]<?php
$_keys = explode("[",$_field);
array_walk($_keys, create_function('&$a,$b', '$a = str_replace("]","",$a);'));
$_field = array_shift($_keys);

//take just the first one
$_field_key = array_shift($_keys);

?>[/php:1:b64d44b52c]
this would be a good start to include multi-dimension arrays, at least 2D array Wink

any comments/feedback welcome

happy coding!
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
Goto page Previous  1, 2
Page 2 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