This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » support » symfony 1.3 and 1.4 » EmbeddedForm validatorSchema in bind method
EmbeddedForm validatorSchema in bind method [message #103261] Thu, 29 July 2010 22:54 Go to next message
simongl  is currently offline simongl
Messages: 20
Registered: July 2008
Location: QC
Junior Member
Trying to figure out how to modify an embedded form validator option in the parent form bind method

PARENTForm
public function configure() {
//[...]
		$this->embedForm('contents', new CHILDCONTAINERForm());
//[..]
}


CHILDCONTAINERForm
public function configure() {
//[..]
	for ($i=1;$i<=3;$i++)
		$this->embedForm($i, new CHILDForm());
//[..]
}


CHILDForm
public function configure() {
//[..]
	$this->setValidators(array(
		'email' => new sfValidatorEmail(
			array('required'=>false)
		)
	));
//[..]
}


Now for some conditions, I have to change 'email' as required on all embedded forms.

PARENTForm
public function bind(array $taintedValues = null, array $taintedFiles = null) {
//[...]
	foreach ($this->getEmbeddedForm('contents')->getEmbeddedForms() as $k=>$form_content) {
		if (isset($taintedValues['contents'][$k]) && $this->someCustomConditionChecks()) {
			$this->getEmbeddedForm('contents')->getEmbeddedForm($k)->validatorSchema['email']->setOption('required',true);
		}
	}


//[..]
}


The problem is, after setting option 'required' to true, the form is post anyway with an empty value for the 'email' field (happens in all embedded forms)
Any idea how to do this?
Re: EmbeddedForm validatorSchema in bind method [message #103266 is a reply to message #103261 ] Fri, 30 July 2010 03:47 Go to previous message
simongl  is currently offline simongl
Messages: 20
Registered: July 2008
Location: QC
Junior Member
OK found the way.
This thread gave me clues http://forum.symfony-project.org/index.php/t/27951/

PARENTForm
public function bind(array $taintedValues = null, array $taintedFiles = null) {
//[...]
	foreach ($this->getEmbeddedForm('contents')->getEmbeddedForms() as $k=>$form_content) {
		if (isset($taintedValues['contents'][$k]) && $this->someCustomConditionChecks()) {
			//OLD $this->getEmbeddedForm('contents')->getEmbeddedForm($k)->validatorSchema['email']->setOption('required',true);
			$vs = $this->validatorSchema['contents'][$k];
			$vs['email']->setOption('required',true);
		}
	}


//[..]
}


Getting used to answer my own forum post.
Previous Topic:How to filter the index view of a module ?
Next Topic:doctrine / createQuery / leftjoin
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software