access underlying entity in symfony2 form builder - forms

I'm looking for a way to access the data class entity in a symfony2 form builder class.
The reason I need this is because the text on the submit button should change depending on a value of this entity (a value the user cannot change in the form).
so basically I want to do:
if ($this->entity->getVariable() == xxx) {
// do something
} else {
// do something else
}
inside the form builder class

praxmatig pointed me in the right direction, the solution is even easier:
the underlying entity is automatically available as an option named "data", so you can do:
public function buildForm(FormBuilderInterface $builder, array $options) {
// whatever
if (isset($options['data'])) {
switch ($options['data']->getSomeVariable()) {
// whatever
}
}
// whatever
}

If you create the form from controller, you can pass anything you want as the options
// AcmeType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$entity = $options['entity'];
}
// AcmeController.php
$form = $this->createForm(new AcmeType(), $entity, array('entity' => $entity));
Or a better but harder way to do this is using the form event

Related

Symfony2 Entity Form Type gets data

I have 2 entities: Audio and Destination
In Audio:
/**
* #ORM\OneToOne(targetEntity="HearWeGo\HearWeGoBundle\Entity\Destination", inversedBy="audio")
* #Assert\NotBlank(message="This field must be filled")
*
*/
private $destination;
I created a Form Type name AddAudioType used to upload an audio to database
<?php
namespace HearWeGo\HearWeGoBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use HearWeGo\HearWeGoBundle\Entity\Audio;
class AddAudioType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name','text')
->add('content','file')
->add('destination','entity',array('class'=>'HearWeGoHearWeGoBundle:Destination','property'=>'name'))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array('data_class'=>"HearWeGo\\HearWeGoBundle\\Entity\\Audio"));
}
public function getName()
{
return 'add_audio';
}
}
?>
In Controller
/**
* #Route("/admin/add/audio",name="add_audio")
*/
public function addAudioAction(Request $request)
{
if (!$this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')){
return new Response('Please login');
}
$this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'Unable to access this page!');
$audio=new Audio();
$form=$this->createForm(new AddAudioType(),$audio,array(
'method'=>'POST',
'action'=>$this->generateUrl('add_audio')
));
$form->add('submit','submit');
if ($request->getMethod()=='POST')
{
$form->handleRequest($request);
if ($form->isValid())
{
$destination=$this->getDoctrine()->getRepository('HearWeGoHearWeGoBundle:Destination')
->findByName($form->get('destination')->getData()->getName());
$audio->setDestination($destination);
$name=$_FILES['add_audio']['name']['content'];
$tmp_name=$_FILES['add_audio']['tmp_name']['content'];
if (isset($name))
{
if (!empty($name))
{
$location=$_SERVER['DOCUMENT_ROOT']."/bundles/hearwegohearwego/uploads/";
move_uploaded_file($tmp_name,$location.$name);
$audio->setContent($location.$name);
$em=$this->getDoctrine()->getEntityManager();
$em->persist($audio);
$em->flush();
return new Response('Audio '.$audio->getName().' has been created!');
}
}
}
}
return $this->render('#HearWeGoHearWeGo/manage/addAudio.html.twig',array('form'=>$form->createView()));
}
In AddAudioType, I declared so that it gets all records from Destination entity table and allows user to choose one of them, then persist it to database
Now there's something another I have to handle: Because relationship between Audio and Destination is one-to-one, user is not allowed to choose a Destination which already appeared in Audio table. Now in AddAudioType, I don't want to get all records from Destination table, but only some that hasn't appeared in Audio table yet. How should I do it?
When you do in your form builder
->add('destination', 'entity', array(
'class'=>'HearWeGoHearWeGoBundle:Destination',
'property'=>'name'
));
you're saying that you want all of possible Destination entities
If you want to filter them, you have two possibilities
First one (recommended)
Write your own method to exclude already "associated" Destinations into DestionationRepository. If you don't know what is a repository or you don't know how to write one, please refer to this document. Method implementation is left to you as an exercise (No, really, I don't know all entities so I cannot make any guess)
Once you've done this, you have to pass DestinationRepository to your form, as an option (required I suppose [see setRequired() method below]), so, something like this (I'll omit uninteresting code)
//AddAudioType
<?php
[...]
public function buildForm(FormBuilderInterface $builder, array $options)
{
$destination_repo = $options['dr'];
$builder->[...]
->add('destination','entity',array(
'class'=>'HearWeGoHearWeGoBundle:Destination',
'choices'=> $destination_repo->yourCustomRepoFunctionName(),
'property'=>'name'));
}
$resolver->setRequired(array(
'dr',
));
Now that you have setted all for your form, you need to pass DestinationRepository to your form. How do you that?
It's quite simple indeed
//In controller you're instatiating your form
[...]
public function addAudioAction()
{
[...]
$destination_repo = $this->getDoctrine()
->getManager()
->getRepository('HearWeGoHearWeGoBundle:Destination');
$form=$this->createForm(new AddAudioType(), $audio, array(
'method' => 'POST',
'action' => $this->generateUrl('add_audio'),
'dr' => $destination_repo,
));
}
It's going to work like a charm as long as you write a good "filter" method (ie.: you exlude with NOT IN clause all Destinations that got the key into other table)
Second one
You simply write your method into the form
//AddAudioType
use Doctrine\ORM\EntityRepository;
<?php
[...]
public function buildForm(FormBuilderInterface $builder, array $options)
{
$destination_repo = $options['dr'];
$builder->[...]
->add('destination','entity',array(
'class'=>'HearWeGoHearWeGoBundle:Destination',
'choices'=> function(EntityRepository $repository) use ($someParametersIfNeeded) {
return $repository->createQueryBuilder('d')
->[...];},
'property'=>'name'));
}
In this second case, createQueryBuilder is also not implemented and left to you. One thing you need to remember: choices will need a query builder, so don't call ->getQuery() and ->getResult()
Why fist one?
Custom function should always stay within repos. So you are writing some code into the place that has to be (see points below to know way)
Because code, that way, is reusable (DRY principle)
Because you can test code more easily
Custom repo function
public function findDestinationWithoutAudio() {
$query= "SELECT d
FROM HearWeGoHearWeGoBundle:Destination d
WHERE d NOT IN (SELECT IDENTITY(a.destination)
FROM HearWeGoHearWeGoBundle:Audio a)"
;
return $this->getEntityManager()->createQuery($query)->getResult();
}
If you want to know why you should use IDENTITY() function and not foreign key directly: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#dql-functions

Detect in Symfony2 TypeExtension if a type is a prototype root

Form type extension problem
I'm writing a FormExtension that apply to all form type (experimenting with angularjs... another story :), but I need to detect at build time if a FormType is used as prototype root.
class MyFormTypeExtension extends AbstractTypeExtension
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
if (true /* === $iAmPrototypeRoot*/) {
$builder->setAttribute('i_need', 'magic');
}
}
}
My problem is that I need to set some attributes that I need later, but setAttribute is (or seems to be) available only at build time.
So far...
I've checked the source code for CollectionType and found this:
class CollectionType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
if ($options['allow_add'] && $options['prototype']) {
$prototype = $builder->create($options['prototype_name'], $options['type'], array_replace(array(
'label' => $options['prototype_name'].'label__',
), $options['options']));
$builder->setAttribute('prototype', $prototype->getForm());
}
// [...]
}
}
So I know that I can override this template and add a "fake" option is_prototype to the options, but this is a bit hackish since I'm overriding (not extending) the default CollectionType. Example:
class CollectionType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
if ($options['allow_add'] && $options['prototype']) {
$prototype = $builder->create($options['prototype_name'], $options['type'], array_replace(array(
'label' => $options['prototype_name'].'label__',
), $options['options'], array(
'is_prototype' => true
)));
$builder->setAttribute('prototype', $prototype->getForm());
}
// [...]
}
}
Which solution?
Up 'til now, the best solution I see is to add the 'is_prototype' to the collection option option (no pun :); but this should persist if the user override the option (maybe a Normalizer?)
After having struggled for a while I found the solution and it is easy (if you know it):
private function isPrototypeRoot($options)
{
return $options['block_name'] === 'entry';
}
Hope this will save some headache.
TL;DR
block_name is the option that allow you to override the block name used to render the form type. By digging into CollectionType I found this:
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$optionsNormalizer = function (Options $options, $value) {
$value['block_name'] = 'entry';
return $value;
};
// [...]
$resolver->setNormalizers(array(
'options' => $optionsNormalizer,
));
}
Normalization happens right after option resolution (even lazy resolution), and once executed the options are set immutable. Since block_name is forced to be equal to entry, and this is not found anywhere into symfony sources, I can safely assume that is a "prototype"

Symfony 2 : bind request to form object containing objects

I am trying to do something that I am not sure it is possible to do.
Here is my form object :
class DeclarationForm {
private $string1;
private $paramObject;
}
Here is the Param Object :
class Param {
private $id;
private $name;
}
I wanted the form to display a select for the 'ParamObject' field in the creation phase
public function buildForm(FormBuilderInterface $builder, array $options) {
$phase = intval($this->options['phase']);
if($phase === 0) {
$params_qualities = $this->options['params_qualities'] // this is an array of Param Objects;
$qualities = new ObjectChoiceList($params_qualities, 'name', array(), null, 'id');
$builder->add('paramObject', 'choice', array(
'required' => true,
'choice_list' => $qualities
));
}
...
}
it works fine and of course, when I submit the form only the id of the selected option is put in the request.
The problem is when I use $form->handleRequest($request); in the controller, it tries to put a string (the id value) in a Param Object of my DeclarationForm.
Is it even possible to get the label of the selected option in the request to populate the Param Object when handleRequest tries to bind the request to the object ?
How to do that ?
Thank you
It is possible, but not with just one Form. In Symfony, each individual Form has a single backing data object (if at all), and each HTML field corresponds to a single member of that object (apart from special cases like dates and entities). However, one of the Field Types you can use is Form, representing a child Form with its own data object.
In your case, that means doing something like this:
class DecorationFormType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$phase = intval($this->options['phase']);
if($phase === 0) {
//Add child form for param; pass options (with dropdown info) in
//By default fieldname needs to match object member
$builder->add('paramObject', new ParamType(), ['options' => $options] );
}
...
}
}
class ParamType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$params_qualities = $this->options['params_qualities'] // this is an array of Param Objects;
$qualities = new ObjectChoiceList($params_qualities, 'name', array(), null, 'id');
//By default fieldname needs to match object member
$builder->add('id', 'choice', array(
'required' => true,
'choice_list' => $qualities
));
...
}
}
Then in the Controller
public parameterAction() {
//Get param options somehow and stick in $options
$paramForm = $this->createForm(new DecorationFormType(), $decorationForm, $options);
}
So basically you create your main Form as normal, and in that Form one of the Fields it adds is the subform. The name of that Field needs to match a public property or getter on the DeclarationForm class so that it can find the data object (you can override this in the options). You set data_class on each Form appropriately.
Apologies if that's not quite right, I've not tested it and I'm more used to using Collections (which is where you have potentially several of a given subform, depending on the data).
Generally the advice is to use Form Events to manipulate what Fields get added to Forms (your Param one only gets added if phase==0 for example), but I don't think it matters if you're only going to use the Form once.
I wouldn't normally have used the Form Options to pass dropdown info in to the Form, but that might just be me, not sure what best practice is - on the one hand you'd be mixing your own stuff up with a whole load of fixed Symfony keys, but on the other it's a handy place to put it! I've used members of my data objects for that kind of thing in the past.
As for your question about getting the label back from the HTML form - you can't do that as is, because as you've seen the only thing the Request contains is the ID. I can imagine solving this several ways:
Use the labels as the dropdown keys (if they're unique)
Remap IDs to labels in the Param object somehow, give it the list of options so when you go getName() or whatever it can magically give you what you want
Just accept that you get an ID back and look up what it means when you use it!

Set data for Embed Form in Symfony2

I'm trying to set data for contact_phone and contact_postal_address type in an embed form:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('phone', 'contact_phone')
->add('postal', 'contact_postal_address');
}
But as I know, when creating the form, I can set only 1 object for the form like this
$form = $this->createForm('address', $address); //address is the embed form
How can I do to set data for each child type in address type?
Create a form-specific model that you use for this form, for example:
class ModelForForm
{
public $phone;
public $postal;
}
You can add any assertions here as well if you're using annotations, such as Valid().
Then you can populate this new model with your data:
$model = new ModelForForm();
$model->postal = $address;
// ...
$form = $this->createForm('address', $model);
Make sure you update your form type's data_class option to be the name of the class you create above.

Validating form (cross-field) without entity using methods (assert true/false) in symfony2

I have a registration form that isn't tied to any entity. I figured out how to specify the validators (validation_constraint option). It's okay if I use simple validators.
But what if I want to make a cross-field validation, like comparing the "password" and "repeat password" fields?
If it was an entity then it would be as simple as adding an "#Assert/True" annotation to a custom method, but here I am clueless, can't find anything in the docs or the net.
In the buildForm function of the form you can use the CallbackValidator :
use Symfony\Component\Form as Form;
//...
public function buildForm(FormBuilder $builder, array $options)
{
// build your form...
$builder->addValidator(new Form\CallbackValidator(function(Form\FormInterface $form)
{
if ($form["password"]->getData() != $form["repeat_password"]->getData())
{
$form->addError(new Form\FormError('passwords must be the same'));
}
});
//...
}
In symfony 2.1+ you should add an event listender for POST_BIND event:
$builder->addEventListener(FormEvents::POST_BIND, function(FormEvent $event)
{
$form = $event->getForm();
if($form['password']->getData() !== $form['password_confirm']->getData())
{
$form['password_confirm']->addError(new FormError('error.form.passwords-not-match'));
}
});