+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14

Thread: Validating multiple sets of checkboxes as one

  1. #1
    ro88o is offline Junior Member
    Join Date
    Jan 2010
    Location
    UK
    Posts
    16

    Default Validating multiple sets of checkboxes as one

    Hi all,

    I have 4 different sets of Zend_Form_Element_MultiCheckbox that I can't combine into one. My problem is that I want to validate these 4 different sets as one and say 'At least one of the checkboxes must be selected'.

    I know I need to create a custom validator but could someone give me a pointer on how I then apply this to the 4 separate Zend_Form_Element_MultiCheckbox instances as one?

    Cheers,
    Tom

  2. #2
    SirAdrian's Avatar
    SirAdrian is offline Member
    Join Date
    Apr 2008
    Posts
    87

    Default

    Create a new validator, and apply it to each of the elements. Inside the validator, you also have access to other elements and their values.

    Check out the validation documentation as it should explain how to do the custom validators.

    Cheers

  3. #3
    ro88o is offline Junior Member
    Join Date
    Jan 2010
    Location
    UK
    Posts
    16

    Default

    Thanks for the input Adrian.
    Unfortunately it appears even if I apply a custom validator to each of the sets of checkboxes, if no checkboxes are selected then none of the validators get called.
    My current solution would be to apply a validator to a random element (the submit button for example) and check the state of the checkboxes from there. But surely there's a better way?

  4. #4
    ro88o is offline Junior Member
    Join Date
    Jan 2010
    Location
    UK
    Posts
    16

    Default

    This is the code currently by the way...

    Code:
    class BBC_Buzzpage_ReportForm extends Zend_Form {
    	
    	public function __construct() {
    		
    		$offensive = new Zend_Form_Element_MultiCheckbox('o');
    		$offensive->setLabel("O...")
    				  ->addMultiOptions(array(
    				  		'0' => 'P', 
    				  		'1' => 'H',
    				  		'2' => 'G'
    				  ));
    				  
    		$unlawful = new Zend_Form_Element_MultiCheckbox('u');
    		$unlawful->setLabel("U...")
    				 ->addMultiOptions(array(
    				  		'0' => 'C',
    				  		'1' => 'B',
    				  		'2' => 'D',
    				  		'3' => 'H',
    				  		'4' => 'D'
    				  ));
    		
    		$accessSafety = new Zend_Form_Element_MultiCheckbox('a');
    		$accessSafety->setLabel("P...")
    				     ->addMultiOptions(array(
    				  		'0' => 'P',
    				  		'1' => 'S',
    				  		'2' => 'S',
    				  		'3' => '1',
    				  		'4' => 'F',
    				  		'5' => 'S'
    				  ));
    				  
    		$promotional = new Zend_Form_Element_MultiCheckbox('p');
    		$promotional->setLabel("Pr")
    				    ->addMultiOptions(array(
    				  		'0' => 'S',
    				  		'1' => 'P',
    				  		'2' => 'C',
    				  		'3' => 'C'
    				  ));
    				  
    		$value = new Zend_Form_Element_MultiCheckbox('v');
    		$value->setLabel("L...")
    			  ->addMultiOptions(array(
    				  		'0' => 'S'
    				  ));
    		
    				  
    		$reason = new Zend_Form_Element_Text('r');
    		$reason->setLabel("Use this box to give us more details on your reason:");
    				  
    		$submit = new Zend_Form_Element_Submit('Submit');
    		
    		$this->addElements(array(
    			$o,
    			$u,
    			$a,
    			$p,
    			$v,
    			$r
    		));
    	
    		$this->setMethod("post");
    		
    		$this->clearDecorators();
    		$this->setDecorators(array(array('ViewScript', array(
        						'viewScript' => 'path/to/form/reportform.phtml',
        						'class'      => 'form element'
    						)), 'Form'));
    	}
    }
    Last edited by ro88o; 01-29-2010 at 03:06 PM.

  5. #5
    SirAdrian's Avatar
    SirAdrian is offline Member
    Join Date
    Apr 2008
    Posts
    87

    Default

    Try $element->setRequired(true) - that will force it to run all the validation chians for it, and may also add notEmpty validator, which I think also is what you need?

  6. #6
    maciek.231 is offline Member
    Join Date
    Feb 2009
    Location
    Poland
    Posts
    90

    Default

    $element->setAllowEmpty() is probably what you need.
    Validate Empty when other field is true

  7. #7
    ro88o is offline Junior Member
    Join Date
    Jan 2010
    Location
    UK
    Posts
    16

    Default

    Sorry for the slow reply guys I had to work on something else for a few days, thanks alot for your help.
    setAllowEmpty(true) has forced my custom validator to run on the checkboxes as required, however I expected to be able to validate the multicheckbox element as a whole rather than each individual checkbox.
    How do I get access to the other form elements whilst in the validator for a checkbox?

    I echoed $this while in the validator and it just returned:
    Code:
    object(App_Validate_OOC)[84]
      protected '_value' => string 'accepted' (length=8)
      protected '_messageVariables' => 
        array
          empty
      protected '_messageTemplates' => 
        array
          empty
      protected '_messages' => 
        array
          empty
      protected '_obscureValue' => boolean false
      protected '_errors' => 
        array
          empty
      protected '_translator' => null
      protected '_translatorDisabled' => boolean false
      public 'zfBreakChainOnFailure' => boolean false

  8. #8
    maciek.231 is offline Member
    Join Date
    Feb 2009
    Location
    Poland
    Posts
    90

    Default

    How do I get access to the other form elements whilst in the validator for a checkbox?
    AFAIK you don't have access to the elements. You have access to the values (array of form data) if you define the validator's method as
    Code:
    public function isValid($value, $formData) {...}
    instead of the usual
    Code:
    public function isValid($value) {...}
    You can get access to the elements if you pass them to your validator somehow. How to do that is up to you I guess.
    Last edited by maciek.231; 02-05-2010 at 09:38 AM.

  9. #9
    ro88o is offline Junior Member
    Join Date
    Jan 2010
    Location
    UK
    Posts
    16

    Default

    Quote Originally Posted by maciek.231 View Post
    You have access to the values (array of form data) if you define the validator's method as
    Code:
    function isValid($value, $formData) {...}
    Values is fine, as long as I can check them I can do what I want. But if I use the isValid above with the $formData included it throws an exception saying it must be compatible with Zend_Validate_Interface::isValid()?

  10. #10
    maciek.231 is offline Member
    Join Date
    Feb 2009
    Location
    Poland
    Posts
    90

    Default

    Quote Originally Posted by ro88o View Post
    Values is fine, as long as I can check them I can do what I want. But if I use the isValid above with the $formData included it throws an exception saying it must be compatible with Zend_Validate_Interface::isValid()?
    That is strange. Have you put "public function" or just "function"? I see I forgot the keyword "public".

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. Mssql store procedure - problem with multiple result sets
    By michalowskiego in forum Databases
    Replies: 0
    Last Post: 06-15-2010, 01:14 PM
  2. How to decorate checkboxes?
    By Marco Polo in forum Model-View-Controller (MVC)
    Replies: 0
    Last Post: 11-06-2009, 12:38 AM
  3. Align Zend form checkboxes
    By krapspark in forum General Q&A on Zend Framework
    Replies: 2
    Last Post: 04-05-2009, 07:45 AM
  4. best way to add checkboxes in partialloop of table
    By painkilla in forum Model-View-Controller (MVC)
    Replies: 0
    Last Post: 03-24-2009, 01:34 PM
  5. Processing Checkboxes
    By altergothen in forum General Q&A on Zend Framework
    Replies: 3
    Last Post: 12-30-2008, 03:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts