I think the easisiest way would be making your
http://www.symfony-project.org/api/1_2/sfValidatorSchema
accepting as a first argument the array of the checkbox fields names
and, in "doClean" method validating that at least one is checked (and
use this as a post validator). I've never implemented something
similar so take my words just as "this is the way I'd go" and not as
"I'm sure this is the best way" :-)


On Wed, Feb 18, 2009 at 2:49 PM, Mark Smith
<marksmith5...@jungle-monkey.com> wrote:
>
> That is what I tried first, but because my array of checkboxes is
> dynamic construction of the global validator is quite complicated.
>
> I'm not even sure if it's possible, the code below was my first
> attempt, but it doesn't work as I expected.
>
> Can you let me know if I am on the right path?
>
> Thanks
>
>        $validationArray=array();
>        foreach($this->services as $serviceId => $serviceName)
>        {
>            $this->widgetSchema['requiredServices'][$serviceName] =
> new sfWidgetFormSchema();
>            $this->validatorSchema['requiredServices'][$serviceName] =
> new sfValidatorSchema();
>
>            foreach($this->regions as $regionCode => $regionName)
>            {
>                $this->widgetSchema['requiredServices'][$serviceName]
> [$regionCode] = new sfWidgetFormInputCheckbox(array(),array
> ('value'=>'true','class'=>'styled'));
>                $this->validatorSchema['requiredServices']
> [$serviceName][$regionCode] = new sfValidatorPass();
>                array_push($validationArray,$this->validatorSchema
> ['requiredServices'][$serviceName][$regionCode]);
>            }
>        }
>
>        $this->validatorSchema->setPostValidator(new sfValidatorOr
> ($validationArray),array('required' => true));
>
>
>
> On Feb 18, 12:52 pm, Martino Piccinato <m...@yoolab.com> wrote:
>> See the paragraph "Global validators" 
>> inhttp://www.symfony-project.org/book/forms/1_2/en/02-Form-Validation.
>>
>> I think your problem will be better handled by a global validator as
>> it regards more than one input field.
>>
>> Martino
>>
>> On Wed, Feb 18, 2009 at 1:16 PM, Mark Smith
>>
>> <marksmith5...@jungle-monkey.com> wrote:
>>
>> > I have a dynamic 2d array of checkboxes, and I want to require that at
>> > least one is selected. Trying to write validation rules for this is
>> > proving to be excruciatingly difficult.
>>
>> > The only way I could accomplish this is to override the isValid
>> > function and put in some logic to manually check each box.
>>
>> > It causes the form not to validate, but obviously does nothing to the
>> > error schema:
>>
>> >    public function isValid()
>> >    {
>> >        $result=false;
>>
>> >        foreach($this['requiredServices'] as $service)
>> >        {
>> >            foreach($service as $region)
>> >            {
>> >                if($region->getValue())
>> >                {
>> >                    $result=true;
>> >                    break;
>> >                }
>> >            }
>> >        }
>>
>> >        return $result && parent::isValid();
>> >    }
>>
>> > Is there a better way than this?
>>
>> > Thanks
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"symfony users" group.
To post to this group, send email to symfony-users@googlegroups.com
To unsubscribe from this group, send email to 
symfony-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/symfony-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to