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

Validating at least one checkbox

 
Post new topic   Reply to topic    Smarty Forum Index -> Add-ons
View previous topic :: View next topic  
Author Message
geoB
Smarty Regular


Joined: 09 Sep 2010
Posts: 46

PostPosted: Thu Jan 12, 2012 6:58 pm    Post subject: Validating at least one checkbox Reply with quote

Please forgive my thickness...and ignorance.

I am trying to validate a set of checkboxes for ethnicity. There are seven, and at least one must be checked. The ethnicities are eth1 - eth7, [edit]each with value 1 when checked[/edit].

I've tried the following method (plus a variety of other even less successful):
Code:
function smarty_validate_criteria_isethnicity($value, $empty, &$params, &$formvars) {
    foreach ($params as $key => $item) {
        if (substr($key, 0, 5) == 'field') {
            if ($formvars[$item] == 1) {
                return true;
            }
         }
    }
    return  false;
}

registered by:
Code:
SmartyValidate::register_validator('eth','eth1:eth2:eth3:eth4:eth5:eth6:eth7', 'isethnicity');


Surely there is a more effective way to accomplish this!

Thanks for setting me straight.

George
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Jan 12, 2012 8:52 pm    Post subject: Reply with quote

did you verify the data passed into the validator is the correct data, and in the format you expect?

print_r($params)
Back to top
View user's profile Send private message Visit poster's website
geoB
Smarty Regular


Joined: 09 Sep 2010
Posts: 46

PostPosted: Thu Jan 12, 2012 9:39 pm    Post subject: Reply with quote

Thanks for your reply.

print_r($params) yields
Code:
Array ( [field] => eth1 [field2] => eth2 [field3] => eth3 [field4] => eth4 [field5] => eth5 [field6] => eth6 [field7] => eth7 [id] => eth [criteria] => isethnicity [message] => At least one ethnicity must be selected [trim] => [empty] => [halt] => [transform] => [append] => error [form] => Update [valid] => 1 )

print_r($formvars); yields, in part,
Code:
[eth1] => 1 [eth2] => 1 [eth3] => 1


The fault occurs when any ethnicity other than eth1 is selected and eth1 is not selected.

George
Back to top
View user's profile Send private message
geoB
Smarty Regular


Joined: 09 Sep 2010
Posts: 46

PostPosted: Fri Jan 13, 2012 3:58 am    Post subject: Solved! Reply with quote

What started me on a new track was the discovery that if eth1 was not checked the validation function was never run. Seemed odd to me.

I discovered that it's easier to confirm whether a variable is an array than to determine if at least one variable is checked. The ethnicity fields were changed from eth1, eth2, ... to eth[1], eth[2], ... I modified the data insert and update routines to handle the new variables and changed the validation function to
Code:
function smarty_validate_criteria_isethnicity($value, $empty, &$params, &$formvars) {
    return is_array($formvars[$params['field']]);
}


If I've created a headache for myself to be discovered later, please let me know. But for now it sure seems to do what I need it to do.

Thanks for listening.

George
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    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