Zend Framework Forum

Go Back   Zend Framework Forum > Zend Framework Components > Core Infrastructure

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-28-2010, 04:46 PM
Junior Member
 
Join Date: Jan 2010
Location: UK
Posts: 15
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-28-2010, 06:59 PM
SirAdrian's Avatar
Member
 
Join Date: Apr 2008
Posts: 83
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-29-2010, 03:37 PM
Junior Member
 
Join Date: Jan 2010
Location: UK
Posts: 15
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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-29-2010, 04:03 PM
Junior Member
 
Join Date: Jan 2010
Location: UK
Posts: 15
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 04:06 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-29-2010, 07:14 PM
SirAdrian's Avatar
Member
 
Join Date: Apr 2008
Posts: 83
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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 01-30-2010, 11:50 AM
Member
 
Join Date: Feb 2009
Location: Poland
Posts: 83
Default

$element->setAllowEmpty() is probably what you need.
Validate Empty when other field is true
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-04-2010, 12:09 PM
Junior Member
 
Join Date: Jan 2010
Location: UK
Posts: 15
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-04-2010, 04:03 PM
Member
 
Join Date: Feb 2009
Location: Poland
Posts: 83
Default

Quote:
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 10:38 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 02-04-2010, 04:13 PM
Junior Member
 
Join Date: Jan 2010
Location: UK
Posts: 15
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()?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 02-05-2010, 10:37 AM
Member
 
Join Date: Feb 2009
Location: Poland
Posts: 83
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".
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT. The time now is 08:58 AM.


Designed by: Miner Skinz Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0