Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 953443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:03:07+00:00 2026-05-16T00:03:07+00:00

I have a symfony 1.4 form with 2 embedded forms. The parent form has

  • 0

I have a symfony 1.4 form with 2 embedded forms.

The parent form has a drop down which determines which of the embedded forms you fill in (hidden/shown on the frontend using JavaScript).

The problem is, when I submit the form, the validation and save is being run on both of the embedded forms, which I obviously don’t want.

What is the best way to alter the parent form so that it only validates and saves the relevant embedded form based on the selection made in the parent form?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:03:08+00:00Added an answer on May 16, 2026 at 12:03 am

    Note: Please see jeremy’s answer as mine is based on his.

    Thank you for your answer jeremy. Your code had a few issues, so I thought I’d post my implemented solution explaining what I did differently.

    1. Override doBind()

    The override of doBind() had an issue where an uncaught sfValidatorError would be thrown if the parent value didn’t return clean from the validator. I wrapped it in a try/catch to suppress this.

    I also altered it to work with multiple embedded forms, not just the two I specified.

    protected $selectedTemplate;
    
    public function getTemplateToEmbeddedFormKeyMap()
    {
        // An array of template values to embedded forms
        return array(
            'template1' => 'templateform1',
            'template2' => 'templateform2',
            'template3' => 'templateform3',
            'templateN' => 'templateformN'
        );
    }
    
    protected function doBind(array $values)
    {
        // Clean the "template" value
        try
        {
            $this->selectedTemplate = $this->validatorSchema['template']->clean(array_key_exists('template', $values) ? $values['template'] : NULL);
        }
        catch(sfValidatorError $e) {}
    
        // For each template embedded form
        foreach($this->getTemplateToEmbeddedFormKeyMap() as $template => $form_key)
        {
            // If there is no selected template or the embedded form is not for the selected template
            if ($this->selectedTemplate == NULL || $this->selectedTemplate != $template)
            {
                // Don't validate it
                $this->validatorSchema[$form_key] = new sfValidatorPass();
            }
        }
    
        // Parent
        parent::doBind($values);
    }
    

    2. NEW STEP Override updateObjectEmbeddedForms()

    Because I’ve disabled validation on some or all of my embedded forms, we now have some uncleaned data in the $values array. I don’t want this data being passed to my model objects within the embedded forms, so I’ve overridden updateObjectEmbeddedForms() to remove any data related to an embedded form that isn’t validated.

    public function updateObjectEmbeddedForms($values, $forms = null)
    {
        // For each template embedded form
        foreach($this->getTemplateToEmbeddedFormKeyMap() as $template => $form_key)
        {
            // If there is no selected template or the embedded form is not for the selected template
            if ($this->selectedTemplate == NULL || $this->selectedTemplate != $template)
            {
                // Remove the data
                unset($values[$form_key]);
            }
        }
    
        // Parent
        parent::updateObjectEmbeddedForms($values, $forms);
    }
    

    3. Override saveEmbeddedForms()

    And finally, I didn’t like I had to copy and paste the entire base saveEmbeddedForms() method and then alter it, so I refactored it to remove the embedded forms I don’t want to save before passing them to the parent.

    public function saveEmbeddedForms($con = null, $forms = null)
    {
        // Get the embedded forms
        if ($forms === NULL)
        {
            $forms = $this->getEmbeddedForms();
        }
    
        // For each template embedded form
        foreach($this->getTemplateToEmbeddedFormKeyMap() as $template => $form_key)
        {
            // If there is no selected template or the embedded form is not for the selected template
            if ($this->selectedTemplate == NULL || $this->selectedTemplate != $template)
            {
                // Remove the form so it isn't saved
                unset($forms[$form_key]);
            }
        }
    
        // Parent
        parent::saveEmbeddedForms($con, $forms);
    }
    

    Thanks again for the answer jeremy, it got me to this which works for my use case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to create a custom formatter in Symfony 1.4. I have embedded form
I have a symfony form which have created_at and updated_at field for date. But
I have a form created with Symfony forms. and in the template i have
A quick question. I have a symfony form. When I save the form, I
I'm trying to use auto-generated forms in symfony, but I have troubles finding good
I am using Symfony 1.2.9, and I have a form that contains two date
A question about symfony form validators. For example I have an url field. If
Have just started using Visual Studio Professional's built-in unit testing features, which as I
im testing symfony form validation. the problem is very simple. no matter what i
I have been using symfony for a few weeks now and I am using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.