php - How do you validate/save an embedded form based on a value in the parent form in symfony? -


i have symfony 1.4 form 2 embedded forms.

the parent form has drop down determines of embedded forms fill in (hidden/shown on frontend using javascript).

the problem is, when submit form, validation , save being run on both of embedded forms, don't want.

what best way alter parent form validates , saves relevant embedded form based on selection made in parent form?

thanks.

note: please see jeremy's answer mine based on his.

thank answer jeremy. code had few issues, thought i'd post implemented solution explaining did differently.

1. override dobind()

the override of dobind() had issue uncaught sfvalidatorerror thrown if parent value didn't return clean validator. wrapped in try/catch suppress this.

i altered work multiple embedded forms, not 2 specified.

protected $selectedtemplate;  public function gettemplatetoembeddedformkeymap() {     // array of template values embedded forms     return array(         'template1' => 'templateform1',         'template2' => 'templateform2',         'template3' => 'templateform3',         'templaten' => 'templateformn'     ); }  protected function dobind(array $values) {     // clean "template" value     try     {         $this->selectedtemplate = $this->validatorschema['template']->clean(array_key_exists('template', $values) ? $values['template'] : null);     }     catch(sfvalidatorerror $e) {}      // each template embedded form     foreach($this->gettemplatetoembeddedformkeymap() $template => $form_key)     {         // if there no selected template or embedded form not selected template         if ($this->selectedtemplate == null || $this->selectedtemplate != $template)         {             // don't validate             $this->validatorschema[$form_key] = new sfvalidatorpass();         }     }      // parent     parent::dobind($values); } 

2. new step override updateobjectembeddedforms()

because i've disabled validation on or of embedded forms, have uncleaned data in $values array. don't want data being passed model objects within embedded forms, i've overridden updateobjectembeddedforms() remove data related embedded form isn't validated.

public function updateobjectembeddedforms($values, $forms = null) {     // each template embedded form     foreach($this->gettemplatetoembeddedformkeymap() $template => $form_key)     {         // if there no selected template or embedded form not selected template         if ($this->selectedtemplate == null || $this->selectedtemplate != $template)         {             // remove data             unset($values[$form_key]);         }     }      // parent     parent::updateobjectembeddedforms($values, $forms); } 

3. override saveembeddedforms()

and finally, didn't had copy , paste entire base saveembeddedforms() method , alter it, refactored remove embedded forms don't want save before passing them parent.

public function saveembeddedforms($con = null, $forms = null) {     // embedded forms     if ($forms === null)     {         $forms = $this->getembeddedforms();     }      // each template embedded form     foreach($this->gettemplatetoembeddedformkeymap() $template => $form_key)     {         // if there no selected template or embedded form not selected template         if ($this->selectedtemplate == null || $this->selectedtemplate != $template)         {             // remove form isn't saved             unset($forms[$form_key]);         }     }      // parent     parent::saveembeddedforms($con, $forms); } 

thanks again answer jeremy, got me works use case.


Comments

Popular posts from this blog

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

c++ - How do I get a multi line tooltip in MFC -

unit testing - How to mock PreferenceManager in Android? -