Symfony "This value should not be blank." while submitting form - php

I'm trying to submit a user form. But the form does not contain all database fields because I want to set the password later (when submitting form).
But now I'm getting the error that the field 'password' should not be blank. Removing the form validation is not working and adding them as HiddenType is also not working.
I'm getting this error after the $form->isValid() check
EDIT:
FormBuilder
$form = $this->createFormBuilder($user)
->add('email', TextType::class, array(
'label' => 'Email adres',
'attr' => array(
'class' => 'input-field'
)
))
->add('first_name', TextType::class, array(
'attr' => array(
'label' => 'Voornaam',
'class' => 'input-field'
)
))
->add('middle_name', TextType::class, array(
'label' => 'Tussenvoegsel (optioneel)',
'required' => false,
'attr' => array(
'class' => 'input-field'
)
))
->add('last_name', TextType::class, array(
'label' => 'Achternaam',
'attr' => array(
'class' => 'input-field'
)
))
->add('date_of_birth', DateType::class, array(
'label' => 'Geboortedatum',
'attr' => array(
'class' => 'input-field'
)
))
->add('save', SubmitType::class, array(
'label' => 'Gebruiker Opslaan',
'attr' => array(
'class' => 'form-submit-btn'
)
))
->getForm();
Submitting the form:
$form->handleRequest($request);
if($form->isSubmitted()){
if($form->isValid()){
dump($user);
exit;
}
}
EDIT:
Doing $user->setPassword('123'); before validation is not working

Can you post your User Entity?
You may get the Error because an argument is not allowed to be blank.
Take a look at any #Assert\NotBlank or nullable = false Annotations.

Can you show your User class? May be implements UserInterface? So, there is this validation.

Related

Validation of AJAX submitted forms in Symfony 5

I'm building an Address form in Symfony 5.1.
It is created in an AddressType Class that has some required fields.
$builder
->add('name', TextType::class, [
'required' => false,
'label' => 'address.name',
'help' => 'address.name_help',
'attr' => [
'placeholder' => 'address.name_ph',
]
])
->add('company', TextType::class, [
'required' => false,
'label' => 'address.company',
'attr' => [
'placeholder' => 'address.company_ph',
]
])
->add('first_line', TextType::class, [
'label' => 'address.first_line',
'attr' => [
'placeholder' => 'address.first_line_ph',
]
])
->add('second_line', TextType::class, [
'required' => false,
'label' => 'address.second_line',
'attr' => [
'placeholder' => 'address.second_line_ph',
]
])
->add('add_info', TextType::class, [
'required' => false,
'label' => 'address.add_info',
'help' => 'address.add_info_help',
'attr' => [
'placeholder' => 'address.add_info_ph',
]
])
->add('postcode', TextType::class, [
'label' => 'address.postcode',
'attr' => [
'placeholder' => 'address.postcode_ph',
]
])
->add('city', TextType::class, [
'label' => 'address.city',
'attr' => [
'placeholder' => 'address.city_ph',
]
])
->add('state', TextType::class, [
'required' => false,
'label' => 'address.state',
'attr' => [
'placeholder' => 'address.state_ph',
]
])
->add('country', CountryType::class, [
'label' => 'address.country',
'preferred_choices' => ['FR'],
'attr' => [
'data-toggle' => 'select',
'placeholder' => 'address.country_ph',
]
])
->add('save',
SubmitType::class,
[
'label' => $options['submit_btn_label'],
]
);
If I submit this form with the submit button, everything works as expected, my form is being processed for validation and if It detects some errors, they are shown on each field.
Here is the function that handle the form :
public function new(Request $request)
{
$user = $this->getUser();
$address = new Address();
$address->setCreatedBy($user);
$form = $this->createForm(AddressType::class, $address);
//handle form
$form->handleRequest($request);
if ($form->isSubmitted()){
//if submit, add hidden fields
$address = $form->getData();
//if valid, process
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($address);
$em->flush();
$this->addFlash(
'success',
'Your address was created.'
);
return $this->redirectToRoute('address_index');
}
}
return $this->render('address/new.html.twig', [
'form' => $form->createView(),
'mode' => 'new',
]);
}
Now, If I submit this form through an AJAX request :
$(document).on('click', '.create-address', function() {
console.log('submitting new address form...');
var $form = $(this).closest('form')
var data = $form.serializeArray();
$.ajax({
url : $form.attr('action'),
type: $form.attr('method'),
data : data
});
});
In this case, my form is processed for validation and It passes ($form->isValid() returns true) even if I don't provide some of the required fields.
This causes the process of persisting the object to occur and so I get a PDOException.
My question is:
Why is my form not handled the same way (especially at the validation step) according how I post the data ?
And how different are those two methods from the point of view of the function that handle the request ?
The required option only adds the required attribute to the markup. This forces the browser to provide a value, but doesn't add any validation server side. You can trigger validation before your ajax call and submit only if the form status is valid.
But it is recommended to add server side validation explicitly by using the constraints option in the form or annotating the target entity. Refer to the documentation for more details.

Symfony: How to create a form with a choice field of another entilty?

I have a form that has to contain a choice field which displays its content from another entity (not the entity used in the form), so this is how I tried to create it:
$user = new user();
$profils=$rep->findAll();
$form2 = $this->createFormBuilder($user,array('csrf_protection' => false))
->add('id', 'text',array('attr'=>array("autocomplete" => "off",'name'=>'login_user','required'=>'required',
'maxlength'=>'255','placeholder'=>'Matricule')))
->add('password', 'password',array('attr'=>array('autocomplete' => 'off','placeholder'=>'Mot de passe','required'=>'required')))
->add('profils', 'choice',[
'choices' => [$profils]],array('attr'=>array('mapped'=>false,'required'=>'required')))
->add('Ajouter', 'submit', array('attr' => array('class' => 'btn btn-primary btn-block rounded_btn', 'id' => 'login_btn',
'style' => "width:6vw;height:5vh;padding:0px 0px; position:relative;left:5vmin;top:1vmin;font-size:2vmin;")))
->getForm();
Pleaaaase help me, as I'm new with symfony, I may be stuck in problems you may consider stupid, I'm sorry but i couldn't find a solution myself.
I fixed it by changing the place of "'mapped'=>false" and it worked:
$form2 = $this->createFormBuilder($user,array('csrf_protection' => false))
->add('id', 'text',array('attr'=>array("autocomplete" => "off",'name'=>'login_user','required'=>'required',
'maxlength'=>'255','placeholder'=>'Matricule')))
->add('password', 'password',array('attr'=>array('autocomplete' => 'off','placeholder'=>'Mot de passe','required'=>'required')))
->add('profils', 'choice'
,array( 'choices' => array('Profils' => $profArr),'mapped'=>false),
array('attr'=>array('required'=>'required')))
->add('Ajouter', 'submit', array('attr' => array('class' => 'btn btn-primary btn-block rounded_btn', 'id' => 'login_btn',
'style' => "width:6vw;height:5vh;padding:0px 0px; position:relative;left:5vmin;top:1vmin;font-size:2vmin;")))
->getForm();
One way of doing it is this :
In the repository of profil entity create a method eg that return an array of profils e.g
class ProfilRepository extends EntityRepository
{
public function myFindAll()
{
$queryBuilder = $this->createQueryBuilder('p');
$entites = $queryBuilder->getQuery()->getArrayResult();
return $entites;
}
}
In the controller as you are doing now,
$user = new user();
// I'm asuming that Profil is an entity
$profils = $this
->getDoctrine()
->getManager()
// this should return an array
->getRepository('SdzBlogBundle:Profil')->myFindAll()
;
$form2 = $this->createFormBuilder($user, array('csrf_protection' => false))
->add('id', 'text', array(
'attr' => array(
'autocomplete' => 'off',
'name' => 'login_user',
'required' => 'required',
'maxlength' => '255',
'placeholder' => 'Matricule'
)
))
->add('password', 'password', array(
'attr' => array(
'autocomplete' => 'off',
'placeholder' => 'Mot de passe',
'required' => 'required'
)
))
->add('profils', 'choice', array(
'choices' => $profils,
array(
'attr' => array(
'mapped' => false,
'required' => 'required'
)
)
))
->add('Ajouter', 'submit', array(
'attr' => array(
'class' => 'btn btn-primary btn-block rounded_btn',
'id' => 'login_btn',
'style' => "width:6vw;height:5vh;padding:0px 0px; position:relative;left:5vmin;top:1vmin;font-size:2vmin;"
)
))
->getForm()
;
That's all for now, Good luck.

Symfony form validate An invalid form control with name='' is not focusable

I have for with fields type entity, drop down choice and required true but when submit form have error in console
An invalid form control with name='inbound_invoice_row[costObject]' is not focusable.
new:1 An invalid form control with name='inbound_invoice_row[accountingAccount]' is not focusable.
new:1 An invalid form control with name='inbound_invoice_row[user]' is not focusable.
Another field validate fine, like vat or price but for accountingAccount user costObject have this error in console
why not understand
my form
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('location', EntityType::class, [
'class' => Location::class,
'empty_value' => 'select_default_value',
'query_builder' => self::getLocations(),
'required' => false,
'label' => 'locations',
'translation_domain' => 'invoicing'
])
->add('costObject', EntityType::class, [
'class' => CostObject::class,
'empty_value' => 'select_default_value',
'choices' => self::getCostObjectHierarchy(),
'required' => true,
'label' => 'cost_object',
'translation_domain' => 'invoicing'
])
->add('accountingAccount', EntityType::class, [
'class' => AccountingAccount::class,
'empty_value' => 'select_default_value',
'query_builder' => self::getAccountingAccount(),
'required' => true,
'label' => 'accounting_account',
'translation_domain' => 'invoicing'
])
->add('user', EntityType::class, [
'class' => User::class,
'empty_value' => 'select_default_value',
'choices' => self::getR(),
'required' => true,
'label' => 'employee',
'translation_domain' => 'invoicing'
])
->add('description', TextType::class, [
'label' => 'description',
'required' => false,
'translation_domain' => 'invoicing'
])
->add('vat', ChoiceType::class, [
'choices' => $this->vatClasses,
'required' => true,
'label' => 'vat',
'translation_domain' => 'common'
])
->add('price', TextType::class, [
'label' => 'price',
'required' => true,
'translation_domain' => 'invoicing'
]);
}
/**
* #param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'EconomyBundle\Entity\InboundInvoiceRow',
'locations' => [],
'employees' => [],
'accounts' => [],
'vat' => [],
'cost' => [],
'ajax' => true,
'csrf_protection' => true
));
}
public function getName()
{
return 'inbound_invoice_row';
}
create form in action
$form = $this->createForm(
$this->get('economy.form.type.in_bound_invoice_row'),
$inboundInvoiceRow,
[
'validation_groups' => [InboundInvoiceRow::GROUP_POST],
'cascade_validation' => true,
'action' => $this->generateUrl('inbound_invoices_row_create', ['id' => $inboundInvoice->getId()]),
'method' => 'POST',
]
);
$form->add('submit', 'submit', array('label' => 'save', 'translation_domain' => 'invoicing'));
You probably have some js library that is used when rendering those fields (e.g. Select2 or Chosen). When there's some HTML validation error (e.g. the field is required but there is no value) on a field, but it's not visible - it might have display property set to none - then the browser is unable to attach error message to that field. This is what most likely triggers your error.
Simplest solution is to set 'required' => false in form type options and rely on backend validation (e.g. using Symfony Validation component) rather than on basic HTML validation.

Symfony 3 - modify form after getForm

I have a form builder set to create an user info form:
$form = $this->createFormBuilder($userSettings)
->add('newName', TextType::class, array(
'required' => false,
'label' => "Change UserName",
'attr' => array(
'placeholder' => $userData[0]->getUsername()
)
))
->add('newMail', TextType::class, array(
'required' => false,
'label' => "Change Email",
'attr' => array(
'placeholder' => $userData[0]->getEmail()
)
))
->add('newPassword', PasswordType::class, array(
'required' => false,
'label' => "Change Password"
))
->add('currentPassword', PasswordType::class, array(
'label' => "Current Password"
))
->add('save', SubmitType::class, array('label' => 'Update Information!'))
->getForm();
Now after form is submitted I have a handler which would check if the mail or username are not already taken
// Check if name or mail not occupied
if($user_manager->findUserByEmail($mail)){
}
elseif($user_manager->findUserByUsername($name)){
}
My goal is to, on mail/username occupied to have the form be printed again, only this time, occupied values would have a placeholder saying "THIS VALUE IS OCCUPIED"
Now I have tried to add another "add" into the if statement like
$form->add('newMail', TextType::class, array(
'required' => false,
'label' => "Change Email",
'attr' => array(
'placeholder' => "EMAIL TAKEN"
)
));
Unfortunately this returns me an error saying
You cannot add children to a submitted form
Now here is my question:
Is there a good and proper way to do it?
B-Side:
Could I trigger browser default validation like ie. In chrome if an input is required and you fail to put stuff there, the box would turn red and you would get an error message.
I was wondering if I could do the same here, so for this project so in that case I would get a red input box with alert "Email taken" or something like that
maybe something like this?
$form = $this->createFormBuilder($userSettings)
->add('newName', TextType::class, array(
'required' => false,
'label' => "Change UserName",
'attr' => array(
'placeholder' => $userData[0]->getUsername()
)
))
->add('newMail', TextType::class, array(
'required' => false,
'label' => "Change Email",
'attr' => array(
'placeholder' => $userData[0]->getEmail()
)
))
->add('newPassword', PasswordType::class, array(
'required' => false,
'label' => "Change Password"
))
->add('currentPassword', PasswordType::class, array(
'label' => "Current Password"
))
->add('save', SubmitType::class, array('label' => 'Update Information!'));
if(isset($mail) && $user_manager->findUserByEmail($mail)){
$form->remove('newMail')
->add('newMail', TextType::class, array(
'required' => false,
'label' => "Change Email",
'attr' => array(
'placeholder' => 'EMAIL TAKEN'
)
));
}
elseif(isset($name) && $user_manager->findUserByUsername($name)){
// just duplicate the above
}
$form->getForm();

how to create dynamic form in symfony2

I have a formtype, ContactoType, this form contain the next fields:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nombre','text', array(
'attr' => array(
'placeholder' => 'contacto.nombre'
)
))
->add('departamento', 'entity', array(
'label' => "Departamentos",
'class' => 'ProductosBundle:Departamento',
'property' => 'nombre'
))
->add('fechaEvento', 'birthday',array(
'input' => 'datetime',
'widget' => 'single_text',
'format' => 'dd-MM-yyyy',
'attr' => array(
'placeholder' => 'DD-MM-YYYY',
'class' => 'form-control')))
->add('promocion','text', array(
'attr' => array(
'placeholder' => 'contacto.promocion'
)
))
->add('apodo','text', array(
'attr' => array(
'placeholder' => 'contacto.apodo'
)
))
->add('file','file', array(
'attr' => array(
'placeholder' => 'contacto.fichero'
)
))
;
}
The Departamento entity has a field named "requiresadditional" if this is true, and promotion nickname will be displayed, if false they are hidden.
Do not know how you could get the field value "requiresadditional"...
As it should do this?Thank!
You should test your Departamento entity in a FormEvent. Read the documentation about FormEvents here.
Usually i add all the field to my symfony form and I display / hide the field by using simple javascript event.

Categories