symfony2 Forms with 2 distinct objects - forms

I would like to do a form with two distincts objets that are not related to one another.
Is it possible?
Do I have to do 2 forms on the same page and a with javascript sumit them all together when the user click on a Javascript Submit Button? Or is possible to manage it just with one form with Symfony. If yes any tips about how to proceed it?

I'm not sure if this solutions is best one, but it's working on one of my project, where I had similar problem.
You need to create FormType for each of Entity and correctly define data_class in setDefaultOptions method. In our example these are EventType and UserType.
For this to work, you need create class which represent both of entities. I call then Form Models.
// Acme\DemoBundle\Form\Model\Custom.php
class Custom
{
public $event;
public $user;
}
And now just create last FormType which glue this entities together in one form.
// Acme\DemoBundle\Form\CustomType.php
class CustomType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('Event', new EventType());
$builder->add('User', new UserType());
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\DemoBundle\Form\Model\Custom'
));
}
...
}

Related

Check if a specific validation_group is valid in Symfony Form?

I'm building a complex symfony form which is a bit long and contains other embedded forms. Thus the form is displayed in the UI in separate tabs to make it more readable and convenient for the user.
Because the form is long and separated in the UI there is a chance you've missed something while populating it or you just inserted something incorrect. That's when the validation would kick in and stop the form from being saved. The validation itself is configured and works flawlessly.
My problem here is I have a gigantic form, separated in tabs, which has an error somewhere and I need to browse each one of the tabs to see exactly what's wrong. I was thinking to make that specific tab, containing fields with errors, in another color so it could stand out and save you the time of wondering what's wrong and where it is located.
From what I could see, I have two options:
Check all fields per tab, manually, using something like:
{% if not form.children.FIELD_NAME.vars.valid %}
which would take forever to complete and I would do only if it's the only possible way.
Try using validation_groups => array('Default', 'my_tab_name') and logically group the fields for each tab.
I'm really hoping to use the second method, but I can't seem to figure out how to check if the validation group i.e. my_tab_1 contains any errors. I'm aware I can do something like this:
$validator = $this->get('validator');
$my_tab_1 = $validator->validate($entity, null, array('my_tab_1'));
$my_tab_2 = $validator->validate($entity, null, array('my_tab_2'));
$my_tab_3 = $validator->validate($entity, null, array('my_tab_3'));
// so on
But the form is already being validated with $form->validate() and using this approach would trigger N more unnecessary validations.
So the question here is how to check if a specific validation group is valid from a twig template? If that's not possible, can one get it from the Controller and pass it as a variable without doing yet another validation?
I don't think I need to post the FormTypes because they're long, nested and might only confuse you. However, this is an oversimplified version of the parent form:
class CompanyType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('address')
->add('representedBy')
->add('category')
->add('phone')
->add('member', new MemberType())
->add('contacts', new ContactType())
->add('notes', new NoteType())
// and a couple more embedded form types.
;
}
/**
* #param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'App\FooBundle\Entity\Company',
'cascade_validation' => true
));
}
/**
* #return string
*/
public function getName()
{
return 'app_company';
}
}
If anybody has a better idea or solution, I would really appreciate it.
First you can use tabs in two different ways:
a) With javascript. All the content of the tabs are loaded once and can be found in the source of the page. All tab-content is hidden except one.
b) With links and PHP. In this case every tab is another webpage with another URL.
(hopefully you understand the difference)
I always use the second method for my advanced forms. Thus for each page i only add a part of all the formfields in the formtype. For each page i use one validation group too. This is already enough to EDIT existing entities.
But a problem is a new Entity. You might want to avoid partly filled entities in your database, thus you need to validate and then store every 'step' in the session and after the user has finished last step (and validation was okay) you might want to store all the form-fields in one time into the database.
This method is used by the craueformflowbundle.
To get a part of your formfields simply use a switch in your formType or create a formType for each step.
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class CompanyType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
switch ($options['flow_step']) {
case 1:
$builder
->add('company')
->add('origin')
;
break;
case 2:
$builder
->add('contactPerson', NULL, array('label' => 'Volledige naam'))
->add('email', 'email', array('label' => 'Email'))
->add('telephone', NULL, array('label' => 'Telefoonnummer'))
;
break;
}
}
/**
* #param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Company',
'flow_step' => 1
));
}
/**
* #return string
*/
public function getName()
{
return 'appbundle_company';
}
}

Symfony form collection - can we check validation on a row by row basic, not on the whole form?

I'm trying to validate some quantity values to make sure I'm not saving them unless we have the stock to allocate to it.
The issue is that I'm using form collections and this means that if we have a delivery of 100 items to make and a user creates 2 rows on the dispatch form of 100 quantity each, then the validation will let both items get created instead of what I want, which is to allow the first to be saved but then error-ing on the second row.
It seems that validation is triggered when the form->isValid() is called but it seems that it should be possible to process row by row before the form->isValid() is called, I just cannot seem to figure it out.
If I cannot validate row by row is there any other ways to smoothly handle this kind of issue?
For know, I see two options here: First one, is to use the special Callback constaint to validate the form values, here is an example:
class MyEntityType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
....
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver
->setDefaults([
'data_class' => 'AcmeDemoBundle:MyEntity',
'constraints' => [
new Callback(
['callback' => [$this, 'validateForm']]
)
]
]);
}
public function validateForm(MyEntity $data, ExecutionContextInterface $context)
{
// Some validation logic here
// e.g.:
if ($data->getSomeData() < 4) {
$context
->buildViolation('ERROR MESSAGE HERE.')
->atPath('someData')
->addViolation()
;
}
}
...
}
The second one, is to create custom validation constraint as described here. I won't provide an example here as it can be too much coding.. But, you should notice that, you must set cascade_validation option to true in parent form's default options, if you're validating collection items with custom constraint..

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

Symfony2 forms confirm password without repeated field type

Registration forms usually feature a confirmation field for passwords. We can achieve this in Symfony2 by using a repeated field type.
However, say that you are building you're registration form as follows:
class RegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('user', new UserType());
$builder->add(
'terms',
'checkbox',
array('property_path' => 'termsAccepted')
);
$builder->add('Register', 'submit');
}
...
FWIW, this is actually taken from a Symfony tutorial.
The problem here is that we add a UserType that already contains a password field.
One solution is to use a repeated field type in UserType. However, I am wondering if there is a way to achieve this without modifying UserType?
I thought about adding a field in the Registration class:
/**
* #Assert\EqualTo($this->user.getPassword())
*/
protected $confirmPassword;
but the getPassword() method actually returns the hashed password so I am unsure whether I am on the right track with this..
How would you do it?
Not sure if I understood completely, but I'll give it a shot.
The fact that getPassword() returns hash value only tells you that you need to go in reverse: hash the plain text value and then compare it. But that is a bit weird/invalid to do within the entity, since you do not have access to container nor to any service.
I suggest the following approach:
class RegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
// the rest of your registration form, what you already have
$encoderFactory = $options['encoder_factory'];
$builder->addEventListener(FormEvents::SUBMIT, function(FormEvent $event) use ($encoderFactory) {
// Instance of Registration
$data = $event->getData();
$encoder = $encoderFactory->getEncoder($data->getUser());
// Second argument should be the salt or null. Do you intend to use it?
$hash = $encoder->encodePassword($data->getConfirmPassword(), .... );
// Overwrite the palin text value with hash
$data->setConfirmPassword($hash);
});
}
The key points:
You will need to pass an encoder_factory to your registration form.
Later, after the setConfirmPassword() call has completed, validation will run and run positive if passwords match.
So, is this what you were trying to do, at all? :)
P.S. Pardon the potential errors, I am typing this right out of my head into the Notepad++...
I have solved a similar issue by applying the comparison with property plainPassword for those using FOS_user.
Taken from a Symfony tutorial.
<code>
/**
* #Assert\EqualTo(propertyPath="plainPassword")
*/
protected $confirmPassword;
</code>

Pass an array of values as choices in choice field type symfony 2 form

I'd appreciate some help with building a choice-type symfony 2 form. I have tried searching for answers and followed several leads but could not find exactly what I'm looking for.
The QuestionType class below is instantiated with four values (which are intended to serve as the choices in the form). However, when the form is rendered, the choices do not reflected the values that have been passed into the class. Any idea what went wrong? Thanks in advance!
class QuestionType extends AbstractType
{
private $foptionone;
private $foptiontwo;
private $foptionthree;
private $foptionfour;
public function buildForm(FormBuilderInterface $builder, array $options)
{
$optionone=$this->foptionone;
$optiontwo=$this->foptiontwo;
$optionthree=$this->foptionthree;
$optionfour=$this->foptionfour;
$builder->add('sanswer','choice',array(
'expanded'=>true,
'choices'=>array(
'a'=>$optionone,
'b'=>$optiontwo,
'c'=>$optionthree,
'd'=>$optionfour)
)
);
}
public function _construct($qoptionone,$qoptiontwo,$qoptionthree,$qoptionfour)
{
$this->foptionone=$qoptionone;
$this->foptiontwo=$qoptiontwo;
$this->foptionthree=$qoptionthree;
$this->foptionfour=$qoptionfour;
}
public function getName()
{
return 'questiontype';
}
Constructors have two underscores, not one:
public function __construct(...)
Your _construct() method is never getting called at all.