1

I want to require all checkboxes in the set

My code looks like this:

     $this->widgetSchema['consent_confirmation'] = new sfWidgetFormSelectCheckbox(
        array(
          'choices' => Doctrine_Core::getTable('MyTable')->getOptions(),
        )
    ); 

UPDATE:

My validation looks like this:

$this->validatorSchema['consent_confirmation'] = new sfValidatorChoice(array(
    'choices' => array(Doctrine_Core::getTable('MyTable')->getOptions()),
    'multiple' => true,
    'required' => true
));  

How can I make it return 'Required' if they're not all checked, and be valid if they're all checked?

2
  • Have you added any rules to the validationSchema for the checkbox? if you do can you post it? Commented Nov 10, 2013 at 12:27
  • See update in post for validationSchema Commented Nov 10, 2013 at 13:03

1 Answer 1

1

My symfony 1.* memory is very hazy at this point but I think what you need to do here is add a rule to the validatorSchema to handle validation of this widget.

According to the Validation Appendix the validator you need is sfValidatorChoice.

This widget has a number of options, including:

  • multiple
  • min
  • max

Assuming that you have two options as above, and you want to enforce selecting both, I'm guessing that you might need to add the following to your form's configure() method:

public function configure()
{
    $this->widgetSchema['consent_confirmation'] = new sfWidgetFormSelectCheckbox(array(
        'choices' => array(
            '1' => 'Yes I agree to #1',
            '2' => 'Yes I agree to #2',             
        )),
    );

    $this->validatorSchema['consent_confirmation'] = new sfValidatorChoice(array(
        'multiple' => true,
        'min'      => 2,
        'max'      => 2,
    ));
}

Something like that - I'm not sure about the assignment to the validatorSchema to be honest, there might be something like addValidator() or setValidator()methods instead. EDIT: I think there were some helper methods added, but some of these might be 1.4 specific. The above assignment should work either way...

Hope this helps :)

Sign up to request clarification or add additional context in comments.

3 Comments

Please see my update. I tried adding that, but it gives me 'Invalid' if more than one is checked, even if they're all checked.
Hmm... it's hard to advise on this from memory alone. Invalid is very cryptic isn't it? Does the same error occur if you remove choices from the validator?
ah! great :) glad to hear it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.