DateTime - Symfony FormBuilder passing value - php

I am geting a specific DateTime value from database that I want to pass as optional value in form rendering (doesnt't work) but also a option that can be changed (that option is working).
I am getting the date but I am having trouble passing it trough my form builder.
My (part of) code:
$lastDate = $transactions[count($accounts) - 1]->getDate();
$theDate = $lastDate->format('Y-m-d H:i:s');
$form = $this->createFormBuilder()
->add('issuingDate', DateType::class, array(
'data' => $theDate,
'label' => false,
'attr' => array(
'date' => (new \DateTime())->format('Y-m-d H:i:s'),
)))
->add('submit', SubmitType::class, array('label' => 'Save', 'attr' => [
'class' => 'btn btn-bg btn-primary'
]))
->getForm();
Everything else seems to be working fine.

$lastDate = $transactions[count($accounts) - 1]->getDate();
$form = $this->createFormBuilder()
->add('issuingDate', DateType::class, array(
'data' => $lastDate,
'data_class' => \DateTime::class,
'label' => false,
))
->add('submit', SubmitType::class, array('label' => 'Save', 'attr' => [
'class' => 'btn btn-bg btn-primary'
]))
->getForm();
This should be enough to display a form with the date prefilled with $lastDate object's value.

Related

How to show field value in sonata edit form?

There is edit form. One field should not be editable. I have tried to set options disabled=true, attr => ['readonly' => true], they make it uneditable, but when submitting form, it gets submitted, sets null to that field and then I get error when getting that field value because it cannot be null.
So I want to make that field not even exist as field but show its value in edit form. Is there a way to do it? Or also if you know how to get rid of error when submitting form with disabled field, that would work too.
public function configureFormFields(FormMapper $form)
{
if ($this->subject->getId() === null) {
$form
->add('name', 'text', ['required' => true])
->add('codeMod', 'text', ['required' => true])
->add('position', 'text', ['required' => false])
->add('projectMod', EntityType::class, ['class' => ProjectEntity::class])
->add('active', 'checkbox', ['required' => false])
->add('first', 'checkbox', ['required' => false])
->add('last', 'checkbox', ['required' => false])
->add('denialReasons', 'text', ['required' => false])
;
} else {
$form
->add('name', 'text', ['required' => true])
->add('position', 'text', ['required' => false])
// ->add('project', TextType::class, ['label' => 'form.label_project_mod', 'attr' => [/*'readonly' => true,*/ 'disabled' => true]])
->add('project', EntityType::class,
['label' => 'form.label_project_mod', 'class' => ProjectEntity::class, 'attr' => ['readonly' => true, 'disabled' => true],
// 'template' => 'ClaimClaimBundle:ClaimStatusAdmin:show_project.html.twig'
]
)
// ->add('projectMod', TextType::class, ['label' => 'form.label_project_mod', 'attr' => [/*'readonly' => true,*/ 'disabled' => true]])
->add('active', 'checkbox', ['required' => false])
->add('first', 'checkbox', ['required' => false])
->add('last', 'checkbox', ['required' => false])
->add('denialReasons', 'text', ['required' => false])
;
}
}
Currently I get error:
Type error: Return value of Qms\ClaimComponent\Status\ManagedModel\StatusManaged::getProject() must implement interface Qms\CoreComponent\Domain\Project\ManagedModel\ProjectManagedInterface, null returned
That is because field value is set to null if I have disabled field.
One way could be rewrite edit.html.twig, now sonatas default template is used. But I did not find quick way, if I override, the styling is off. For one field looks bit too much.
If you don't mind that field's value when submitting you can unmap it by setting
'mapped' => false
in its attributes.
Example:
->add('name', 'text', ['required' => true, 'mapped' => false])

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

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.

Symfony - checkbox form

I have trouble with syntax, my form does not show a checkbox in a right way. Any ideas?
$form = $this->createFormBuilder()
->add('generate', CheckboxType::class, array('label' => 'Generate', 'attr' => ['class'=>'form-control']))
->add('save', SubmitType::class, array('label' => 'Send', 'attr' => [
'class' => 'btn btn-primary action-save'
]))
->getForm();

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.

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