0

I've the following Model in Symfony2 due to the fact that the relationship has an extra property(Boolean value) and as such must be represented as an entity too. enter image description here I'm now working on the form to mark the completion of Trainings for the various Candidates (Parents) having attended.
I'm using a nested form as follow:

class TrainingCompletionType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
            ->add('titre', 'text', array('disabled' => 'true'))
            ->add('Intervenant', 'collection', array(
                                    'type' => new TrainingCandidatesType(),'label'=>'Candidat(s)'
                                    ,'options'  => array('label'=>' '))
            );
        }

Here is the TrainingCandidatesType form

class TrainingCandidatesType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('parents','entity',array('class'=>'ParentsBundle:Parents'
                                            ,'label'=>' ','disabled'=>true))
            ->add('completed')
        ;
    }

This works (the submission of parents checked a completed), however it causes the twig to render each parent as a drop-down list having each and every parents as a value as seen below enter image description here. I've disabled the drop-down since the user is not made to select from the list. The candidates name is not meant to be selectable.

Is there a way to display each of the parent name as a label or disable text input in Symfony without referring to a data transformer or Event subscriber?
I've gone through related questions on the Stack site but didn't find one quite similar to this issue.

Is there a better approach to this that i'm willing to try ? or am i approaching it from a worng angle?

1 Answer 1

1

Since you only need a boolean value there is an excellent option using expanded and multiple.

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('parents','entity',array(
                  'class'=>'ParentsBundle:Parents',
                  'label'=>'Label', 
                  'expanded'=>true,
                  'multiple'=>true
             ))
        ;
    }

But in that way you are going to need the data-transformer. You can use this bundle too, wich transform the data for you, but is a little more than you need.

Possibly the best option is set also mapped=>false, and with the selected collection of entities handle the Training_Candidates instances creation in the controller. The selected collection will be created with true and the rest need to be loaded from database and created with false. Also note that in this way you don't need the nested form, you can use it in on single form.

Hope this can help you.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your feedback! I think the 2nd option is much easier to understand and implement. Will give it a try!

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.