0

I have an older project which still uses Symfony 2. In it there is a form for editing a client profile. In the controller we have this:

$form = $this->createForm(new ClientProfile($remindTimes), $client);
$form->handleRequest($request);

And in the ClientProfile class we have

class ClientProfile extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('client_name', 'text', array('label' => 'Full name'))
            ->add('client_address', 'text', array('label' => 'Address', 'required' => false))
            ->add('client_city', 'text', array('label' => 'City', 'required' => false))
            ->add('client_post_code', 'text', array('label' => 'Postal index', 'required' => false))
            ->add('client_email', 'email', array('label' => 'E-mail', 'required' => false));
    }
}

... and some other fields, but you get the gist. Then there's also a Twig view which renders the HTML. Standard stuff, as far as I can tell.

Now for my requirement. The client object has two special properties. Let's call them FroobleEnabled and FroobleType. If Frooble is disabled, then the type value has no meaning (can be set to 0). In the UI I want a dropdown with the values:

Disabled
Type 1
Type 2
Type 3

If the user selects Disabled, then FroobleEnabled gets set to false and FroobleType gets set to 0. Otherwise FroobleEnabled gets set to true and FroobleType to 1, 2 or 3 respectively.

How do I do this? The thing which makes this special is that it's not a 1:1 mapping anymore. There are two fields in the model object but just one UI control. I think I could achieve this with the a DataMapper, but I also don't want to manually map all the other fields (though I can, if there's no other option). I also cannot find any decent documentation about DataMapper or any other Symfony Forms features that could help me.

13
  • Could you use javascript to show or hide FroobleType based on the value of FroobleEnabled? The controller can change the value of FroobleType to 0 if the record 's FroobleType is changed to Disabled. Commented Jun 4, 2019 at 22:53
  • @geoB - I could, but I wanted to keep the ui simple. Commented Jun 5, 2019 at 8:27
  • An alternative is a single field with four possible choices. The controller can determine the proper values of the two properties. Doing this depends on the ability of users to make appropriate choices. Of course this is true for a two field solution as well. Commented Jun 5, 2019 at 12:23
  • @geoB - I'd quite much like to write some code that sets the two properties depending on the one input field. But where do I put this code? Commented Jun 5, 2019 at 12:51
  • First choice would be the controller. Commented Jun 5, 2019 at 13:00

1 Answer 1

1

One way to accomplish this is to create a field, let's say, frooble.

  • In the form class create a frooble Choice field with mapped => false and values 0, 1, 2, 3. Set its choices to strings appropriate to the application.
  • In the controller, after form submission & validation, include code something like:

    ...
    $frooble = $form->get('frooble')->getData();
    if (0 === $frooble) {
        $client->setFroobleEnabled(false);
        $client->setFroobleType(0);
    } else {
        $client->setFroobleEnabled(true);
        $client->setFroobleType($frooble);
    }
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.