0

I currently have a working form in Symfony where I have a list of companies with checkboxes next to each company name. This is so you can check off which company is assigned to each user. The checkbox currently shows the accountID but it would also be helpful to have the entity field 'name' as well. Can you build a property with two entity fields? Here is my form in my controller:

 ->add('companies', 'entity', array(
            'label'         => 'Company',
            'class'         => 'Default\Bundle\Entity\Customer',
            'property' =>   'accountId', //This puts the company id next to the check box
            'multiple'      => true,
            'expanded'      => true,
            'query_builder' => function ($repository)
                {
                    return $repository->createQueryBuilder('c')->orderBy('c.accountId', 'ASC');
                },))
        ->add('Save', 'submit')
        ->getForm();

This is what I am trying to do:

 ->add('companies', 'entity', array(
       'label'         => 'Company',
       'class'         => 'Default\Bundle\Entity\Customer',
       'property' =>   'accountId' + 'name', // I want to combine my entity fields here
       'multiple'      => true,
       'expanded'      => true,
       'query_builder' => function ($repository)

here is the entity just for reference

class Customer
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @Assert\NotBlank(message="Please enter a Customer ID.")
     * @Assert\Length(max="32")
     * @ORM\Column(type="string", length=32)
     * @var string
     */
    protected $accountId;

    /**
     * @Assert\NotBlank(message="Please enter a company name.")
     * @Assert\Length(max="60")
     * @ORM\Column(type="string", length=60)
     * @var string
     */
    protected $name;

And one last time... I want to go from this:

Original

To this:

New

2 Answers 2

2

Create a simple getter and use that as the property, eg:

public function getNamePlusAccountId()
{
    return $this->name." (".$this->accountId.")";
}

and use 'property' => 'namePlusAccountId' in your form.

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

1 Comment

Awesome! Didn't know I could reference functions when building the form. I assumed it had to be a variable of the class.
1

If you only need to change the label but would like to keep the form field value then http://symfony.com/doc/current/reference/forms/types/entity.html#choice-label probably what are you looking for

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.