Symfony2 select option other - php

I'm building a form in Symfony2, in a itemType.
Currently, users can type whatever they want in the "typePresentation" field.
I want to let them select 3-4 options (most usual options), and when they select "others", a text field appear and they can type whatever they want.
I think that I can use some jQuery to show a text field when a particular option is selected, but I want to know how to generate 2 fields with the same name, or how to save 1 of 2 fields on the same field of entity.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('numero', 'hidden')
->add('etat', 'choice', array(
'choices' => array('v' => 'Vert', 'o' => 'Orange', 'r' => 'Rouge'),
'preferred_choices' => array('v'),
'required' => true,
'expanded' => false,
'multiple' => false))
->add('typePresentation', 'text', array('attr' => array('placeholder' => 'Type de la présentation')))
->add('datePresentation', 'date', array('empty_data' => new \DateTime(),'label' => 'Date de présentation', 'years'=>range(date('Y')-2, date('Y')+5)))
->add('iteration', 'textarea', array('attr' => array('placeholder' => "Nom de l'itération")))
->add('contenu', 'textarea', array('attr' => array('placeholder' => "Contenu de l'Item")))
->add('enregistrer', 'submit');
;
}

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 embedded ChoiceType form updating every entity instance

I have an EditAnnouncementType form which is embedded in a CollectionType form. The embedded form has two ChoiceType forms, one which properly updates the mapped Announcement entity. The other one however, will attempt to update all instances of the entity in my database, resulting in this error.
Type error: Argument 1 passed to AppBundle\Entity\Announcement::setType() must be of the type integer, null given, called in /Users/dperezpe/dev/grand-central/673/grand-central/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 528
EditAnnouncementType.php The type form is the error one.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('edit', SubmitType::class,
array
(
'label' => 'Save changes',
'attr' => ['class' => 'btn btn-primary']
))
->add('type', ChoiceType::class,
[
'choices' =>
[
'info_type' => 1,
'star_type' => 2,
'alert_type' => 3,
'lightbulb_type' => 4,
'event_type' => 5,
'statement_type' => 6,
'cat_type' => 7,
'hands_type' => 8
],
'expanded' => true,
'multiple' => false,
'required' => true,
'label_attr' => array(
'class' => 'sr-only'
),
])
->add('audience', ChoiceType::class,
[
'choices' =>
[
'Students: anybody with a student level field populated' => 'students',
'Employees: anybody with an employee ID number' => 'employees'
],
'expanded' => true,
'required' => true,
'multiple' => true
])
The CollectionType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('announcements', CollectionType::class,
[
'entry_type' => EditAnnouncementType::class,
'entry_options' => ['label' => false],
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => AnnouncementManager::class
]);
}
I suspect it is related to the difference in the HTML name that was rendered to this, where the type inputs are missing an empty []
<input type="radio"
id="announcement_edit_collection_announcements_221_type_0"
name="announcement_edit_collection[announcements][221][type]"
required="required" value="1">
<input type="checkbox" id="announcement_edit_collection_announcements_221_audience_0"
name="announcement_edit_collection[announcements][221][audience][]
"value="students">

Symfony 3 - Best practice for concating form-blocks

I'm trying to learn how to build forms in symfony 3.
Following some tutorials I have built a PersonType
class PersonType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('gender', ChoiceType::class, array('label' => 'Anrede', 'choices' => array('Herr' => 'Herr', 'Frau' => 'Frau'), 'attr' => array('class' => 'form-control')))
->add('title', TextType::class, array('label' => 'Titel', 'attr' => array('class' => 'form-control')))
->add('firstname', TextType::class, array('label' => 'Vorname', 'attr' => array('class' => 'form-control')))
->add('lastname', TextType::class, array('label' => 'Nachname', 'attr' => array('class' => 'form-control')))
->add('birthdate', DateType::class, array('label' => 'Geburtsdatum', 'attr' => array('class' => 'form-control')))
->add('street', TextType::class, array('label' => 'Straße', 'attr' => array('class' => 'form-control')))
->add('streetnumber', TextType::class, array('label' => 'Hausnummer', 'attr' => array('class' => 'form-control')))
->add('zip', TextType::class, array('label' => 'PLZ', 'attr' => array('class' => 'form-control')))
->add('city', TextType::class, array('label' => 'Stadt', 'attr' => array('class' => 'form-control')))
->add('email', TextType::class, array('label' => 'E-Mail', 'attr' => array('class' => 'form-control')));
}
public function getName() {
return 'person';
}
}
And some other types.
In the controller I have
$person = new Person();
$form = $this->createForm(PersonType::class, $person);
My question now is, how do I now concat the PersonType to some other Types to get one Form out of it? And how do I then set the submit-button?
You cannot concatenate but you can include a subset of fields in several forms.
Here you have a nice example in the Symfony documentation :
http://symfony.com/doc/current/cookbook/form/inherit_data_option.html
Recap :
Create a form with your subfields, with the option 'inherit_data' => true.
Use it in another form as field.
First of all, please note how you add fields to PersonType form, because it will be exactly the same.
->add('email', TextType::class, array('label' => 'E-Mail', 'attr' => array('class' => 'form-control')));
What you do here is adding a subform TextType. It actually contains single field, but it's still a form.
The same way you can add PersonType to any other form. That would be something like:
->add('person', PersonType::class, array(/* some options if needed*/);
And how do I then set the submit-button?
As mentioned in Best Practices for Symfony Forms, I would suggest to add them in template, not to the form object.

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.

Display a field depending on a checkbox

I have a form with a checkbox field and I want basically when this checkbox is checked, display a field below.
I do not know what is the best practice in symfony to do that...
Form builder :
public function buildForm(FormBuilderInterface $builder, array $options)
{
->add('protected', 'checkbox', array(
'label' => 'Protected by a password ?',
'required' => false,
'mapped' => false
))
->add('password', 'password', array(
'label' => 'Mot de passe',
'required' => false
))
}
So if the field 'protected' is checked, I want to display 'password' field.
I would do this with jquery.
Hide the field with css ( i would suggest to do this with a class instead)
->add('password', 'password', array(
'label' => 'Mot de passe',
'required' => false,
'attr' => array('style' => 'display:none;')
))
and then in jquery:
$('#checkbox_id').click(function() {
$('.password_id')[this.checked ? "show" : "hide"]();
});
I hope this helped!

Categories