0

I have form with field type entity on this field I have query_builder which return query and 'property' => 'name',. And my question what need to do in data transformer for change select name, need complicated with several filed, example - name_address_office. Using Symfony 2.8 I need dataTransformer approach

my form

class OutBoundInvoiceRowType extends AbstractType
{
/**
 * @var array
 */
private $vatClasses;

/**
 * @var Container
 */
private $container;

/**
 * @var EntityManager
 */
private $em;

/**
 * OutBoundInvoiceRowType constructor.
 * @param Container $container
 * @param $vatClasses
 */
public function __construct(
    Container $container,    
    $vatClasses
) {
    $this->container = $container;
    $this->vatClasses = $vatClasses;
    $this->em = $this->container->get('doctrine.orm.entity_manager');
}

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('location', 'entity', array(
            'class' => Location::class,
            'property' => 'name',
            'empty_value' => 'Choice Location',
            'query_builder' => self::getLocations(),
            'required' => false
        ))          
        ->add('vat', ChoiceType::class, [
            'choices' => ?,
            'required' => true,
        ])
    $builder->get('vat')
        ->addModelTransformer(new VatTransformer($this->container));
}

and my VatTransformer:

class VatTransformer implements DataTransformerInterface
{
    /**
     * @var Container
     */
    private $container;

    /**
     * @var EntityManager
     */
    private $em;

    /**
     * LocationTransformer constructor.
     * @param Container $container
     */
    public function __construct(Container $container)
    {
        $this->container = $container;
        $this->em = $this->container->get('doctrine.orm.entity_manager');
    }

    /**
     *
     * @param  Location|null $issue
     * @return string
     */
    public function transform($issue)
    {
        if (null === $issue) {
            return '';
        }
    }
}

in function transform $issue have null and when return '' nothing change in form, still have 'property' => 'name', on choice, What need to do in data transform name ?

this now I have

enter image description here

and this what I need

enter image description here

need name of several parts

UPDATE

Ok. I have choice field vat and I need build data in choice for vat field like - from some entity field, example entity Location (id, name)

How this realized with dataTransformer ?

5
  • A data-transformer is probably not the right solution here. What is it that you actually want to achieve? And what symfony version are you using. Also check the choice_label and choice_value options. Commented Feb 7, 2017 at 13:32
  • Symfony 2.8 I need dataTransformer approach Commented Feb 7, 2017 at 13:44
  • Currently there is nothing in your question that would suggest that you need a data-transformer. Simply using choice_label with a callback would produce the same result. So please add why you think you need a data-transformer. For now this looks like you simply want to use it, because you don't know how to solve your actual problem. Commented Feb 7, 2017 at 13:48
  • Ok. I have choice field vat and I need build data in choice for vat field like - from some entity field, example entity Location (id, name) How this realized with dataTransformer ? Commented Feb 7, 2017 at 13:58
  • 1
    So you want to populate the values for vat depending on what the user selected in location? If so, there is a tutorial for exactly that: symfony.com/doc/current/form/… Commented Feb 7, 2017 at 14:11

1 Answer 1

1

Implement a __toString() method into your entity, which will return the desired name. Read more on the docs.

Then you remove the property from the QueryBuilder, this will cause the automatic usage of the to_string() method of the class.

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

1 Comment

ok, thnks, but I need approach with dataTransform, because in this transformer I want add some logic

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.