1

Hi guys I'm trying to make FloorType form which has a a hidden field of object (which i s building) and i cannot transform the object entity i have use the transformation code from the cookbook.

http://symfony.com/doc/2.7/cookbook/form/data_transformers.html

Here is the code:

The transformer

namespace George\FloorBundle\Form\DataTransformer;

use  George\ObjectsBundle\Entity\Object;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;

class ObjectToNumberTransformer implements DataTransformerInterface
{
private $manager;

public function __construct(ObjectManager $manager)
{
    $this->manager = $manager;
}

/**
 * Transforms an object (issue) to a string (number).
 *
 * @param  Object|null $issue
 * @return string
 */
public function transform($object)
{
    if (null === $object) {
        return '';
    }

    return $object->getId();
}

/**
 * Transforms a string (number) to an object (issue).
 *
 * @return Object|null
 * @throws TransformationFailedException if object (issue) is not found.
 */
public function reverseTransform($objectNumber)
{
    // no issue number? It's optional, so that's ok
    if (!$objectNumber) {
        return;
    }

    $object= $this->manager
        ->getRepository('ObjectsBundle:Object')
        // query for the issue with this id
        ->find($objectNumber)
    ;

    if (null === $object) {
        // causes a validation error
        // this message is not shown to the user
        // see the invalid_message option
        throw new TransformationFailedException(sprintf(
            'An issue with number "%s" does not exist!',
            $objectNumber
        ));
    }

    return $object;
}
}

Floor Type

namespace George\FloorBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use George\FloorBundle\Form\DataTransformer\ObjectToNumberTransformer;
use Doctrine\Common\Persistence\ObjectManager;

class FloorType extends AbstractType
{
private $manager;

public function __construct(ObjectManager $manager)
{
    $this->manager = $manager;
}
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{

    $builder
        ->add('translations', 'a2lix_translations',array(
            'required_locales' => array('bg','en')
        ))
    ->add('object')->addModelTransformer(new ObjectToNumberTransformer($this->manager));
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'George\FloorBundle\Entity\Floor'
    ));
}

/**
 * @return string
 */
public function getName()
{
    return 'george_floorbundle_floor';
}
}

CreateCreteForm

private function createCreateForm(Floor $entity)
{
    $manager = $this->getDoctrine()->getManager();
    $form = $this->createForm(new FloorType($manager), $entity, array(
        'action' => $this->generateUrl('admin_floor_create'),
        'method' => 'POST',
    ));

    $form->add('submit', 'submit', array('label' => 'Create'));

    return $form;
}

Service

 <services>
    <service id="app.form.type.task" class="George\FloorBundle\Form\FloorType">
        <tag name="form.type" />
        <argument type="service" id="doctrine.orm.entity_manager"></argument>
    </service>
</services>

And the big fat error is :

Catchable Fatal Error: Object of class George\ObjectsBundle\Entity\Object could not be converted to string 

It is interesting that the service did not work i need to put in the create form the manager:

$manager = $this->getDoctrine()->getManager();

So i understand that the transformer fail but i cannot see where... Can someone help me with this transformation?

3 Answers 3

1

Your syntax is off just a bit. $builder->add actually returns a builder object and not the form object that was added. The book shows:

$builder->add(
  $builder->create('description', 'textarea')
    ->addModelTransformer(...)
);

In your case it would be something like:

$builder
  ->add('translations', 'a2lix_translations',array(
        'required_locales' => array('bg','en')
  ))
  ->add(
    $builder->create('object','text')
      ->addModelTransformer(new ObjectToNumberTransformer($this->manager))
  ));

Might have the parens messed up but you get the idea.

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

2 Comments

no it is not this .... now everything is working fine i cannot understand what was this....
It might appear to be working but unless you use the above syntax then the model transformer is being applied incorrectly. You might have trouble if another field is added or if the order changes or there is a lunar eclipse. Trust the documentation.
1

You should use this

$builder
    ->add('translations', 'a2lix_translations',array(
        'required_locales' => array('bg','en')
    ))
->add('object');
$builder->get('object')->addModelTransformer(new ObjectToNumberTransformer($this->manager));

instead of

$builder
        ->add('translations', 'a2lix_translations',array(
            'required_locales' => array('bg','en')
        ))
        ->add('object')->addModelTransformer(new ObjectToNumberTransformer($this->manager));

Hope it helps!!

Comments

0

About the second problem the solution was to use alias in the service:

<services>
    <service id="app.form.type.floor" class="George\FloorBundle\Form\FloorType">
        <tag name="form.type" alias="george_floorbundle_floor" />
        <argument type="service" id="doctrine.orm.entity_manager"></argument>
    </service>
</services>

And where i create the form:

 $form = $this->createForm('george_floorbundle_floor', $entity, array(
    'action' => $this->generateUrl('admin_floor_update', array('id' => $entity->getId())),
    'method' => 'PUT',
   ));

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.