Getting user input data in FormEvents::SUBMIT - forms

I am looking for a way to get user data in the formEvents::SUBMIT event.
I can get it in formEvents::PRE_SUBMIT using
$builder->addEventListener(FormEvents::PRE_SUBMIT, function() {
//...
$userInput = $event->getData());
//...
});
I can get it in formEvents::POST_SUBMIT using
$builder->addEventListener(FormEvents::POST_SUBMIT, function() {
//...
$userInput = $event->getForm()->getViewData()
//...
});
But in formEvents::SUBMIT I cannot find where, though I read it can be accessed:
Symfony2: dynamic generation of embedded form
I can find other project getting data using the same method as PRE_SUBMIT, for instance here:
https://github.com/Klerik/web-dersen/blob/65ba1bafa574dc2bda6dd2bb738fe0d33a06bf71/src/Catalog/FilmsBundle/Form/EventListener/UploadFileSubscriber.php
or there:
https://github.com/alexandresalome/bros/blob/59c111eb481c2fd672f3646390455ddad65dd800/src/Bros/ServerBundle/Form/Type/BrowserType.php
Though in my situation I test with user input that does not fit the Normalisation - $event->getForm()['field']->getData() sends an object without the user data which have been refused.
Still I want to retrieve user input to:
* change accordingly (testing data validity) my current form (which cannot be done in POST_SUBMIT),
* use my form related object (not available in PRE_SUBMIT)
EDIT
Here are the relevant parts of the ...Type Class :
public function buildForm(FormBuilderInterface $builder, Array $options)
{
$adminUserQuery = $options['admin_user_query'];
$builder
->add('admin_user', 'model', Array( //using Propel as ORM
'label' => FALSE,
'multiple' => FALSE,
'expanded' => FALSE,
'class' => 'App\\CoreBundle\\Model\\AdminUser',
'query' => $adminUserQuery,
'empty_value' => 'form.placeholder.admin_user',
'property' => 'email',
));
//...
$builder->addEventListener(
FormEvents::PRE_SUBMIT, function (FormEvent $formEvent)
{
var_dump($formEvent->getData(), '---'); // prints: array(1) { ["admin_user"]=> string(17) "test#email.test" }
var_dump($formEvent->getData()['admin_user'], '---'); // prints: string(15) "test#email.test"
//...
});
$builder->addEventListener(
FormEvents::SUBMIT, function (FormEvent $formEvent)
{
var_dump($formEvent->getData()); // prints: object(App\CoreBundle\Model\EventUserRole)#1659 {...}
var_dump($formEvent->getData()->getAdminUser()); // prints: NULL
//...
}
);
}
Anyone ?

In the FormEvents::Submit action, you can do this (assuming you are editing a User):
$builder->addEventListener(FormEvents::SUBMIT, function ($event) {
// Your Entity/Document
$user = $event->getData();
// Grab the actual form
$form = $event->getForm();
// ...
});

Related

Symfony form EntityType::class - How use it to edit data in form (3.2.13)

A dropdown selection menu in a Form is available by using a EntityType form type. It is usefull if you add data. But when you try to edit data with the same FormType class, your $request data are overwritten by your EntityType.
How use the same FormType class when editing the data? (eg. editAction in controller) How pass Request $request data to FormType fields as "defaults, or selected" for element EntityType::class in FormBuilder? Is there something in $builder->add() method I can use like if(['choice_value'] !=== null ) xx : yy?
How to get something like html select selected, from Request object and pass it to xxxFormType class and bind to right EntityType::class element there.
<select>
<option value="volvo">Volvo</option>
<option value="vw">VW</option>
<option value="audi" selected>Audi</option>
</select>
I've looked at EntityType Field,
How to Dynamically Modify Forms Using Form Events and lots of StackOverflow posts, but can't find proper solution.
Controller:
public function editProdPackageAction(Request $request, ProdPackage $prodPackage)
{
$form = $this->createForm(ProdPackageFormType::class, $prodPackage);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$prodPackage = $form->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($prodPackage);
$em->flush();
$this->addFlash('success', 'MSG#011E');
return $this->redirectToRoute('admin_package_list');
}
return $this->render(':admin/forms/ProdPackage:edit.html.twig',
[
'ppg' => $form->createView(),
]
);
}
Formtype:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'idCatBig',
EntityType::class,
[
'label' => '* category',
'class' => 'AppBundle\Entity\ProdCategoryBig',
'choice_value' => 'id',
'choice_label' => 'shortName',
'multiple' => false,
'expanded' => false,
]
)
->add(
'dateStart',
DateType::class,
[
'label' => '* some date',
'data' => new \DateTime('now'),
'choice_translation_domain' => true,
]
)
->add(
'dateEnd',
DateType::class,
[
'label' => '* till',
'data' => new \DateTime('now'),
]
)
->add(
'packageName',
TextType::class,
[
'label' => '* package',
'attr' => ['placeholder' => 'default Name'],
'data' => 'your default value',
]
)
This is what I do in my form, I set a "pre set data listener" to check whether or not this is an edit or a create
function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add(
'dateStart',
DateType::class,
[
'label' => '* some date'
//I don't set a data field here because it is an edit
'choice_translation_domain' => true,
]
)
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$data = $event->getData();
$form = $event->getForm();
if (!$data || null === $data->getId()) {
//if the entity does not have an ID it means that this is a new entity not an edit. because all edits have been in the database and have an id
$builder->add(
'dateStart',
DateType::class,
['label' => '* some date',
'data' => new \DateTime('now'), //this is a create so I override my previous setting and set a default data
'choice_translation_domain' => true,]
)
}
});
}
I mainly use this trick to change form fields from required to non required between edits and creates for password fields for example,and sometimes if there is something exceedingly complicated. To change data, honestly, setting default data in constructor is cleaner as Stephan suggests
Problem
You are overriding an object by using the data option.
https://symfony.com/doc/3.2/reference/forms/types/entity.html#data:
( ! ) The data option always overrides the value taken from the domain data (object) when rendering. This means the object value is also overriden when the form edits an already persisted object, causing it to lose its persisted value when the form is submitted.
Solution 1: use the controller
So the solution is pretty simple: don't use data. Instead, set default values in your addProdPackageAction action (or whatever it is called):
public function editProdPackageAction(Request $request)
{
$prodPackage = new ProdPackage();
$prodPackage->setDateEnd(new Datetime('now'));
//example: use Request or other controller methods to modify your entity
if ($this->getUser()->hasRole('ROLE_ADMIN')) {
$prodPackage->setCreatedByAdmin(true);
}
//your form
}
Solution 2: use your entity constructor
Alternatively, you can use your entity constructor method:
class ProdPackage
{
//your attributes
private $dateEnd;
public function __construct()
{
$this->dateEnd = new Datetime('now');
}
}
Found another interesting solution
in formTypeclass at $builder
->add(
'trBegin',
DateTimeType::class,
[
'label' => 'Tourney Begins',
'required' => true,
'date_widget' => 'single_text',
'time_widget' => 'single_text',
'date_format' => 'dd.MM.yyyy',
]
)
and continuing building form add:
$builder->get('trBegin')->addModelTransformer(
new CallbackTransformer(
function ($value) {
if (!$value) {
return new \DateTime('now + 60 minutes');
}
return $value;
},
function ($value) {
return $value;
}
)
);
it sets default date at moment when form is created. This method is very usefull also for EntityType object, where you can pass id of field in form and get selected your current real choice from database (not all list from the begining) very useful when using EntityField also for editing forms
$builder->get('productCategory')
->addModelTransformer(new CallbackTransformer(
function ($id) {
if (!$id) {
return;
}
return $this->em->getRepository('AppBundle:ProductCategory')->find($id);
},
function($category) {
return $category->getId();
}
));

symfony Form with multiple steps

I am trying to create a form with multiple steps in that:
First field displayed should be a phone number type.
Once that is submitted I want to perform a lookup to see if there is a user with that phone number.
If a user is found then I need a second field for verification, so I create a zip code field.
I am currently doing this with one controller and one form type:
SearchType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('phoneNumber', PhoneNumberType::class, array(
'default_region' => 'US',
'format' => PhoneNumberFormat::NATIONAL))
->add("finished", HiddenType::class, array(
"data" => "no"))
->add("submit", SubmitType::class);
$formModifier = function (FormInterface $form) {
$phoneNumber = $form->get('phoneNumber')->getData();
$one = $this->em->getRepository('AppBundle:Phone')->findOneByPhoneNumber($phoneNumber);
if($one){
$form->remove("submit");
$form->remove("finished");
$form->add("zipCode", TextType::class);
$form->add("finished", HiddenType::class, array(
"data" => "yes"));
$form->add("submit", SubmitType::class);
}
};
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($formModifier) {
$formModifier($event->getForm());
}
);
$builder->get('phoneNumber')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) use ($formModifier) {
$formModifier($event->getForm()->getParent());
}
);
}
Controller:
$form = $this->createForm(SearchType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid() && $form->get("finished")->getData() == "Yes") {
//...
}
So, I'm not even sure if I am on the right track. I am currently trying to use the Hidden field "finished" to let me controller know when to actually do the work with the submitted data, however "finished" never gets set to "yes" and I am not sure why.
Aside from that, is there a better way for me to be doing this?
I looked into CraueFormFlowBundle but that required the underlying data to be an object (wouldn't work with Array) and I didn't think I wanted it to be in this case since I am just using the data entered to perform a query.

Correct way to use FormEvents to customise fields in SonataAdmin

I have a Sonata Admin class with some form fields in it, but I'd like to use the FormEvents::PRE_SET_DATA event to dynamically add another form field based on the bound data.
However, I'm running into several problems:
1) The only way I can find to add the form field to the correct 'formGroup' in the admin is by adding the new field twice (once via the formMapper and once via the form itself)... this seems very wrong and I cannot control where in the formGroup it appears.
2) The added element doesn't seem to know that it has a connected Admin (probably because it is added using Form::add()). This means, amongst other things, that it renders differently to the other fields in the form since it triggers the {% if sonata_admin is not defined or not sonata_admin_enabled or not sonata_admin.field_description %} condition in form_admin_fields.html.twig
So, this leads me to believe that I'm doing this all wrong and there must be a better way.
So...
What is the correct way to use a FormEvent to add a field to a form group, ideally in a preferred position within that group, when using SonataAdmin?
Here's some code, FWIW...
protected function configureFormFields(FormMapper $formMapper)
{
$admin = $this;
$formMapper->getFormBuilder()->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($admin, $formMapper) {
$subject = $event->getData();
// do something fancy with $subject
$formOptions = array(/* some cool stuff*/);
// If I don't add the field with the $formMapper then the new field doesn't appear on the rendered form
$formMapper
->with('MyFormGroup')
->add('foo', null, $formOptions)
->end()
;
// If I don't add the field with Form::add() then I get a Twig Exception:
// Key "foo" for array with keys "..." does not exist in my_form_template.html.twig at line xx
$event
->getForm()
->add('foo', null, $formOptions)
;
});
$formMapper
->with('MyFormGroup')
->add('fieldOne')
->add('fieldTwo')
->end()
;
}
The aim is to add the new foo field between fieldOne and fieldTwo in MyFormGroup.
Edit: here's what I came up with with the help of Cassiano's answer
protected function configureFormFields(FormMapper $formMapper)
{
$builder = $formMapper->getFormBuilder();
$ff = $builder->getFormFactory();
$admin = $this;
$formMapper->getFormBuilder()->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($ff, $admin) {
$subject = $event->getData();
// do something fancy with $subject
$formOptions = array(
'auto_initialize' => false,
'class' => 'My\ProjectBundle\Entity\MyEntity',
/* some cool stuff*/
);
$event->getForm()->add($ff->createNamed('foo', 'entity', null, $formOptions));
});
$formMapper
->with('MyFormGroup')
->add('fieldOne')
->add('foo') // adding it here gets the field in the right place, it's then redefined by the event code
->add('fieldTwo')
->end()
;
}
No time here for a long answer, I will paste a piece of code and I don't know if fits exactly in your case, in my it's part of multi dependent selects (country, state, city, neighbor).
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Doctrine\ORM\EntityRepository;
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('myfield');
$builder = $formMapper->getFormBuilder();
$ff = $builder->getFormFactory();
$func = function (FormEvent $e) use ($ff) {
$form = $e->getForm();
if ($form->has('myfield')) {
$form->remove('myfield');
}
$form->add($ff->createNamed('myfield', 'entity', null, array(
'class' => '...',
'attr' => array('class' => 'form-control'),
'auto_initialize' => false,
'query_builder' => function (EntityRepository $repository) use ($pais) {
$qb = $repository->createQueryBuilder('estado');
if ($pais instanceof ...) {
$qb = $qb->where('myfield.other = :other')
->setParameter('other', $other);
} elseif(is_numeric($other)) {
$qb = $qb->where('myfield.other = :other_id')
->setParameter('other_id', $other);
}
return $qb;
}
)));
};
$builder->addEventListener(FormEvents::PRE_SET_DATA, $func);
$builder->addEventListener(FormEvents::PRE_BIND, $func);
}

bind error to embedded form field in symfony2's controller

Using Symfony2.3.4
As the title quite explains it I need to bind an error message to an embedded form field and preferably in the controller. I'm thinking maybe similar to how I do it with single forms:
use Symfony\Component\Form\FormError;
//....
public function createAction(){
//.....
$postData = current($request->request->all());
if ($postData['field_name'] == '') {
$error = new FormError("Write some stuff in here");
$form->get('field_name')->addError($error);
}
//.....
}
or maybe it's got to be done differently, either way I need help,
thank$
I see, that you are trying to display a message when a form field does not contain any value. You can do this easily in your form class, like this:
buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('field_name', 'text', array(
'label' => 'Field label',
'required' => true,
'constraints' => array(
new NotBlank(array('message' => 'Write some stuff in here.'))
),
));
}
If you need to inject some other kind of constraint to your form which is not part of Symfony2 framework, you can create your own validation constraint.
If you want to add some options to your form in controller, it can be done in the method where you create the form by setting your own options:
class YourController {
public function createForm(YourEntity $yourEntity){
$form = $this->createForm(new YourFormType(), $yourFormType, array(
'action' => $this->generateUrl('your_action_name',
array('your_custom_option_key' => 'Your custom option value')),
'method' => 'POST',
));
return $form;
}
// Rest of code omitted.
}
After that you need to add an option in setDefaultOptions(OptionsResolverInterface $resolver) method, in your form class, like this:
public function setDefaultOptions(OptionsResolverInterface $resolver){
$resolver->setDefaults(array(
'your_custom_option_key' => '',
));
}
And then access it in buildForm(FormBuilderInterface $builder, array $options) method, like this:
buildForm(FormBuilderInterface $builder, array $options) {
$options['your_custom_option_key']; // Access content of your option
// The rest of code omitted.
}

Form validation - Require only one field to be filled in

I have the following form class:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('existingfolder', 'entity', array(
'class' => 'ImageBundle:Folder',
'required' => false,
))
->add('folder', 'text', array('required' => false))
->add('file', 'file');
}
How can I set up the validation so that either the existingfolder or folder field must be filled (but not both of them)?
Any advice appreciated.
Thanks.
Use the True or Callback validation assert, here an example to check if the user must give at least one of the folders:
<?php
namespace Acme\BlogBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class Image
{
// ...properties, functions, etc...
/**
* #Assert\True(message = "You must give at least an existing folder or a new folder")
*/
public function isThereOneFieldFilled()
{
return ($this->existingfolder || $this->folder); // If false, display an error !
}
}
Here another example if the user must give ONLY one field but not both:
/**
* #Assert\True(message = "You must give an existing folder or a new folder, not both")
*/
public function isThereOnlyOneFieldFilled()
{
return (!$this->existingfolder && $this->folder || $this->existingfolder && !$this->folder);
}
EDIT:
Callback validation inside a form (I found an example here):
// use ...
use Symfony\Component\Form\CallbackValidator;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormInterface;
// Inside the form:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('existingfolder', 'entity', array(
'class' => 'ImageBundle:Folder',
'required' => false,
))
->add('folder', 'text', array('required' => false))
->add('file', 'file');
// Use the CallbackValidator like a TrueValidator behavior
$builder->addValidator(new CallbackValidator(function(FormInterface $form) {
if (!$form["existingfolder"]->getData() && !$form["folder"]->getData()) {
$form->addError(new FormError('You must give at least an existing folder or a new folder'));
}
}));
}