How to make Symfony2 form optional field not null on save - php

I have an image field which is optional. When you upload image, it will save the filename on the database (using events via doctrine ).
The problem is when you edit an already uploaded form and don't add an image, it makes the image field to null.
Is there a way to check / remove the field value setting to null if no image is uploaded?
The Entity, Form code is as below :
class Product
{
/**
* #ORM\Column(type="string", nullable=true)
*
* #Assert\Image
*/
private $image;
}
Form
class ProductType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('image', FileType::class, [
'required' => !$options['update'],
]);
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$form = $event->getForm();
$product = $event->getData();
if (null == $form->get('image')->getData()) {
// $form->remove('image');
}
});
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Product'
));
}
public function getBlockPrefix()
{
return 'appbundle_product';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setRequired([
'update',
]);
}
}
// In controller
$editForm = $this->createForm(
'AppBundle\Form\ProductType',
$product,
[
'update' => true
]
);

You need the event PRE_SUBMIT, try this:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('image', FileType::class, [
'required' => !$options['update'],
]);
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
$form = $event->getForm();
$product = $event->getData();
if(empty($product['image'])){
//No new image for existing field was given. Make field disabled so doctrine will not update with empty
$config = $event->getForm()->get('image')->getConfig();
$options = $config->getOptions();
$event->getForm()->add(
'image',
$config->getType()->getName(),
array_replace(
$options,
['disabled' => true]
)
);
}
});
}

Symfony Form has submit() method, which takes two arguments, $submittedData and $clearMissing
/**
* Submits data to the form, transforms and validates it.
*
* #param null|string|array $submittedData The submitted data
* #param bool $clearMissing Whether to set fields to NULL
* when they are missing in the
* submitted data.
*
* #return FormInterface The form instance
*
* #throws Exception\AlreadySubmittedException If the form has already been submitted.
*/
public function submit($submittedData, $clearMissing = true);
$clearMissing parameter is by the default set to true.
If you, in your controller do this: $form->submit($submittedData, false);, your image will not be set to null.
https://symfony.com/doc/current/form/direct_submit.html#calling-form-submit-manually
Similar question: Symfony2 - How to stop Form->handleRequest from nulling fields that don't exist in post data

Related

Symfony 6 - Fail to inject data into a dynamic formtype on POST_SUBMIT

I have a problem with a nested form. I can't get the values I want to pass.
Here is a simple example to reproduce my problem, I would like to pre-fill a form about a user according to the selected house in my form.
Here are the files, if you want to test. I would like to inject the values of roger and billy the good way but my user fields are always empty
The models
class Test
{
/**
* #var string|null
*/
private $house;
/**
* #var TestUser|null
*/
private $user;
// Getters & Setters of course...
}
class TestUser
{
/**
* #var string|null
*/
private $name;
/**
* #var int|null
*/
private $age;
// Getters & Setters again...
}
The main form
class TestType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('house', ChoiceType::class, [
'choices' => [
'first' => 1,
'second' => 2,
],
]
);
$builder->get('house')->addEventListener(FormEvents::POST_SUBMIT, [$this, 'addUser']);
}
function addUser(FormEvent $event)
{
$form = $event->getForm()->getParent();
$house = $event->getForm()->getData();
if (!$house) {
return;
}
// here is the part where I choose the user I want to use
// for the rest of the example (which does not work)
$testUser = $house === 1
? (new TestUser())->setName('roger')->setAge(65)
: (new TestUser())->setName('billy')->setAge(22);
$builder = $form->getConfig()->getFormFactory()->createNamedBuilder('user', TestUserType::class, $testUser, [
'auto_initialize' => false,
]);
$form->add($builder->getForm());
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Test::class,
]);
}
}
The user form type
class TestUserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('name', TextType::class, ['label' => 'username'])
->add('age', IntegerType::class, ['label' => 'age']);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => TestUser::class,
]);
}
}
The controller
public function testForm(Request $request): RedirectResponse|Response
{
// ...
$test = new Test();
$form = $this->createForm(TestType::class, $test);
$form->handleRequest($request);
// ...
}
The view
{{ form_start(formtest) }}
<p>
<button class="btn btn-primary" type="submit">test go</button>
</p>
{{ form_end(formtest) }}
all help is welcome
Is setter actually returning $this?
When ->setAge(65) is executed.
Because it's not clear, it's not in your code you provided here.
you need to link the $user $age $house and $name to the input fields you have.
The reason why you always get an empty output is do to the face that non of the variables refer to any data source.

Symfony doesn't validate form

I'm writing an API. And I need to validate the form. Other forms validate correctly, but this one does not validate at all.
This is my form type class:
public function buildForm(FormBUilderInterface $builder, array $options){
$builder
->add('passwordConfirmation', RepeatedType::class, [
'required' => true,
'invalid_message' => 'Passwords must match!',
'type' => PasswordType::class
]);
}
/**
* #param OptionsResolver $resolver
*/
public function configureOption(OptionsResolver $resolver){
$resolver->setDefaults(array(
'data_class' => RepeatPassword::class,
'csrf_protection' => false
))
}
public function getName() {
return 'repeatPassword';
}
My entity:
class RepeatedPassword{
/**
* #var string
* #Assert\Length(
* min = 8,
* minMessage = "Your password must be at least {{ limit }} characters long")
* )
* #Assert\NotBlank()
*/
private $passwordConfirmation;
/**
* #return mixed
*/
public function getPasswordConfirmation() {
return $this->passwordConfirmation;
}
/**
* #param mixed $passwordConfirmation
*/
public function setPasswordConfirmation($passwordConfirmation): void{
$this->passwordConfirmation = $passwordConfirmation;
}
}
Method where I try validate:
public function resetPassword(Request $request): View{
$form = $this->createForm(RepeatPasswordType::class);
$form->handleRequest($request);
if ($form->isValid()) {
$this->userService->setPassword($this->getUser(), $form->getData()->getPasswordConfirmation());
return View::create([], Response::HTTP_OK);
}
return View::create($form->getErrors(), Response::HTTP_BAD_REQUEST);
}
My config.yml file:
validation: { enabled: true, enable_annotations: true }
serializer: { enable_annotations: true }
Data I send and server response with status 400:
I've seen an issue before where $form->handleRequest was behaving differently to $form->submit. I was able to debug more effectively by manually calling submit.
$form = $this->createForm(RepeatPasswordType::class);
if ($request->getMethod() === 'post') {
$form->submit($request->request->get($form->getName()));
if (!$form->isValid()) {
// etc
}
}
Maybe it helps:
add following to your form
'constraints' => [
new NotNull(),
new NotBlank(),
],
All I can see for now is that you have an extra "," at the end of your minMessage Assert. Have you tried removing it ?

Update Many to Many relationship

I am working on project with symfony 3 and doctrine .
I have a many to many relationship between Pack and Produit :
Pack Entity :
class Pack
{
/**
* #var ArrayCollection | Produit[]
* #ORM\ManyToMany(targetEntity="AppBundle\Entity\Produit", inversedBy="packs")
* #ORM\JoinTable(name="link_pack")
*/
private $produits;
public function __construct()
{
$this->produits = new ArrayCollection();
}
/**
* #return Produit[]|ArrayCollection
*/
public function getProduits()
{
return $this->produits;
}
public function addProduit(Produit $produit)
{
if ($this->produits->contains($produit)) {
return;
}
$this->produits[] = $produit;
}
public function removeProduit(Produit $produit)
{
if (! $this->produits->contains($produit)) {
return;
}
return $this->produits->removeElement($produit);
}
}
Produit Entity :
class Produit
{
/**
* #var ArrayCollection | Pack[]
* #ORM\ManyToMany(targetEntity="AppBundle\Entity\Pack", mappedBy="produits")
*/
private $packs;
public function __construct()
{
$this->packs = new ArrayCollection();
}
/**
* #return Pack[]|ArrayCollection
*/
public function getPacks()
{
return $this->packs;
}
public function addPack(Pack $pack)
{
if ($this->packs->contains($pack)) {
return;
}
$pack->addProduit($this);
$this->packs[] = $pack;
}
}
I want to assign products to a pack , so I have a form that contains products in a select field. (the pack and products are created before).
The form type :
class PackAffectProduitType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('produits', EntityType::class, array(
'class' => Produit::class,
'choice_label' => 'libelle',
'multiple' => true,
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Pack::class,
));
}
}
The controller :
public function affectProduitsAction(Pack $pack, Request $request)
{
$form = $this->createForm(PackAffectProduitType::class, $pack);
$form->handleRequest($request);
dump($pack);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
dump($pack);
$em->flush();
// ...
}
//...
}
The problem is when I select the products and submit, It assigns the selected products to the pack but the products assigned before are deleted. I want them to still assigned, So how to solve this problem ?
If you want your view to add OR remove values, if you manually generate your field view structure, you need not to forget to fill already set values (e.g. the products previously added to the Pack).
For example by adding the selected attribute to the choices of your select. Doing so will pre-fill your field with current values therefore removing at submit only those which have been deliberately unselected.
If you don't want your view to display nor know anything about already set Produit entities on this pack and just blindly perform adds, you can add mapped => false to your field options and handle manually the addition of your products to your pack in your controller.
This will only allow you to add though, since it does not have knowledge of already set values, and you would have to make another case/action to perform deletion. Above way is better in my opinion.
In this last case, your controller would look like :
public function affectProduitsAction(Pack $pack, Request $request)
{
$form = $this->createForm(PackAffectProduitType::class, $pack);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
if(is_array($form->get('produits')->getData()){
foreach($form->get('produits')->getData() as $produit){
$pack->addProduit($produit);
}
}
$em->flush();
// ...
}
//...
}
And your FormType :
class PackAffectProduitType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('produits', EntityType::class, array(
'class' => Produit::class,
'mapped' => false,
'choice_label' => 'libelle',
'multiple' => true,
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Pack::class,
));
}
}
Try to add 'by_reference' => false option to your form, it will force to use setters and getters from your entity

Using some dynamic forms related to the same class

Symfony 2.3
I'm embedding some Forms to be able to change each user property related to permissions. I've created an UserAdminType which is displayed for each user in the same page:
<?php
namespace Msalsas\UserAdminBundle\Form;
use Msalsas\UserBundle\Entity\User;
use Msalsas\UserBundle\Entity\ExtendedUser;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
class UserAdminType extends AbstractType
{
private $user;
public function __construct(User $user)
{
$this->user = $user;
$this->extendedUser = new ExtendedUser($this->user);
}
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$extendedUser = $this->extendedUser;
$builder
->add('extendedRole',
'choice', array('choices' => array(
$extendedUser::ROLE_1 => "Role 1",
$extendedUser::ROLE_2 => "Role 2",
$extendedUser::ROLE_3 => "Role 3",
),
'label' => $this->user->getUsername()
))
->add('Change roles for '.$this->user->getUsername(), 'submit')
;
$builder->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) {
$form = $event->getForm();
$data = $event->getData();
if( ! $form->getClickedButton('Change roles for '.$this->user->getUsername()) )
{
// Here I should avoid submitting the form
}
}
);
}
/**
* #param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Msalsas\UserBundle\Entity\ExtendedUser',
'empty_data' => function (FormInterface $form) {
return null;
}
));
}
/**
* #return string
*/
public function getName()
{
return 'extendedUserRoleForm';
}
}
The problem is that when I submit one of those forms, all other forms are also submitted, returning an error, because the extendedUser uses a constructor to initialize the object with the User as parameter:
Catchable Fatal Error: Argument 1 passed to Msalsas\UserBundle\Entity\ExtendedUser::__construct() must be an instance of Msalsas\UserBundle\Entity\User, none given, called in .../vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/Type/FormType.php on line 140 and defined
I've also tried to set the empty_data with a new default ExtendedUser:
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Msalsas\UserBundle\Entity\ExtendedUser',
'empty_data' => function (FormInterface $form) {
return $this->extendedUser;
}
));
}
Now, when submitting the form, the new entity is persisted, but the other forms are still submitted, and returning an error: This form should not contain extra fields. This seems to be due to the duplicated property name (extendedRole).
How could I avoid the other forms to be submitted?
I've found out the solution here.
Each Form must have different name. So I've added a $name property, and assigned it in the constructor:
private $user;
private $name = 'default_name_';
private $extendedUser;
public function __construct(User $user, $formName)
{
$this->user = $user;
$this->extendedUser = new ExtendedUser($this->user);
$this->name = $this->name.$formName;
}
//... (No event required)
/**
* #return string
*/
public function getName()
{
return $this->name;
}
The $formName parameter is relative to the current user. In this way, only the "clicked" form is submitted. Wish it helps.

Symfony2 FormType entity field type

I have three entity. Profile, Car and Trip. When user(Profile) create Trip, he can chose the Car(only his own) and assign it to the Trip. I know the field must be the entity type. But I dont know how I can set to chose list a cars of current user(Profile). Any ideas? Thanks.
Filter the cars by user, I think this example is what you need:
$builder->add('car', 'entity', array(
'class' => '/path/to/entity/Car',
'property' => 'title',
'empty_value' => 'Choose a car',
'query_builder' => function(EntityRepository $em) use ($userId) {
return $em->createQueryBuilder('c')
->join('c.user', 'u')
->where('u.id = :userId')
->setParameter('userId', $userId);
}
)
)
You can add $userId as one form option:
$form = $this->createForm(new MyFormType(), $object, array( 'userId' => $userId ));
And inside your form retrieve it:
/**
* #param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'userId' => function (Options $options, $value) {
return $options['userId'];
}
));
}
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
if($options['userId']){
$userId = $options['userId'];
}
}
How I mentioned. other solution, even personally I don't prefer it:
$form = $this->createForm(new MyFormType($userId), $object);
And in your form, store it in a protected variable to be used later in your query:
/**
* Class MyFormType
*/
class MyFormType extends AbstractType
{
protected $userId;
/**
* #param $userId
*/
public function __construct($userId) {
$this->userId = $userId;
}
}
Take a look at this Symfony cookbook entry:
http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#how-to-dynamically-generate-forms-based-on-user-data

Categories