Symfony - Disable required field if the checkbox is checked - symfony

I have a form on Symfony.
My form is composed of a text input and a checkbox.
If the user checks the box, the input text is disabled (I use javascript).
However, if the user doesn't check the box, he has to fill in the input text. If he ticks the box, he does not have to do it.
Here is my form :
$formBuilder
->add('text', TextType::class,array(
'required' => true,
'constraints' => array(
new NotBlank()
)))
->add('box', CheckboxType::class, array(
'mapped' => false,
'label' => 'Box'
))
;
I'm looking for a way to disable text input validation if the box is checked.
How to ignore input text validation if the checkbox is checked?
Do you know how to do this?
Thanks!

You need a custom contraint in this case. You should mark field as non-required and show asterisk (*) for required using javascript. For the backend, you need to create a custom constraint. See symfony documentation here: https://symfony.com/doc/current/validation/custom_constraint.html

Related

Reinitialize autocomplete after form submission

I have a custom autocomplete input that is not bound to any entities:
$builder
->add('input', TextType::class, [
'autocomplete' => true,
'autocomplete_url' => 'https://path-to-autocomplete',
'tom_select_options' => [
'create' => false,
'preload' => true,
'maxItems' => 1,
'delimiter' => '/',
],
])
;
The input correctly requests the autocomplete URL, fetches results, renders the correct item label, and sends the correct item value with the form.
The problem arises after submitting the form at step #6.
Empty form is rendered.
We select an item with ID 15 and label Foo.
Input is rendered correctly.
Form is submitted.
Value of 15 is sent to the server with the form.
Now we have to re-render a form again with an initial value of the input of 15.
At this point, the input value is rendered as 15 instead of Foo. That makes perfect sense. The input just doesn't know how to get a label for an item with ID 15.
Question: how do I provide the input with data about the item label?
I expected the it to have something like reverse_autocomplete_url that would be called after input initialization to get items by their IDs but I don't think there is such an option.
Considering you used symfony form.
You used $form->isValid() etc..
Persisted and flush your object.
Then instead of re-rendering the twig, call redirectToRoute function to the current route.
If you provide more detail about your usecase i can give you a better answer.
You have to initialize the form field with options array:
'tom_select_options' => [
'options' => [
[
'value' => 15,
'text' => 'Foo',
],
],
],
value and text keys are customizable via valueField and labelField properties.

Render a field multiple times in Twig and Symfony

I need two inputs so that the user can choose from
Controller
$etud = new Etudiant();
$form=$this->createFormBuilder($etud)
->add('filierechoisit',EntityType::class,array('class'=>'inscriptionBundle\Entity\filieres', 'choice_label'=>'libelle_filiere'))
->add('filierechoisit',EntityType::class,array('class'=>'inscriptionBundle\Entity\filieres', 'choice_label'=>'libelle_filiere'))->getForm();
if ($form->isValid()) {
// ... maybe do some form processing, like saving the Task and Tag objects
}
return $this->render('inscriptionBundle:Default:authentification.html.twig', array(
'modif' => $form->createView(),
));
How can I do it?
I'm almost sure you want a ChoiceType/EntityType field with multiple and expanded options as true.
It should be something like this:
$form->add('filierechoisit', EntityType::class, array(
# query choices from this entity
'class' => 'inscriptionBundle\Entity\filieres',
# use the filieres.libelle_filiere property as the visible option string
'choice_label' => 'libelle_filiere',
# used to render a select box, check boxes or radios
'multiple' => true,
'expanded' => true,
));
You are mixing the form processing and form rendering. If you want user to choose which way he enters data - you do not want to process this data in two different ways until these are two independent fields.
You should just have one
->add('filierechoisit',EntityType::class,array('class'=>'inscriptionBundle\Entity\filieres', 'choice_label'=>'libelle_filiere'))
call to add field processing and left all rendering for the front-end side. You could use some JS, or API there, or in simple case, just override Twig template for that field
http://symfony.com/doc/current/cookbook/form/form_customization.html
You could render your own widget for your form here, allowing user to do some html stuff to change input.
Currently, making two add with identical names call just makes the second one override the first.

Get out an error message if noone of checkboxes selected

I have set a checkboxes form that works perfect, i have this if statement which also works perfect but what i really want is when the user hasn't selectected any checkbox from the list, when he pushes the save button to get out a drupal error that will says "Oh!you do not select nothing"... How i can do this thing?
if (!$selected) {
drupal_set_message(t('You have to select at least one option from the list.'), 'warning');
}
You can set the message manually from your code:
https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/drupal_set_message/7
Where you have that your if logic add drupal_set_message() call.
Do you use Form API?
If yes try the #required property
$form['test'] = array(
'#type' => 'checkboxes',
'#options' => $myOptions,
'#required' => TRUE,
'#title' => t('Test'),
)

Sonata admin, editable field with choice

I'm using sonata admin and there is an option 'editable' => true for edit directly inline datas on the list view.
If my field is a text, it's ok, i can click, edit the text and save directly on the table.
But i don't want an input type="text" when i click on the field, but a list, i'm trying something like :
->add('etat', null, array('editable' => true), 'choice', array(
'choices' => array(
'Brut' => 'Brut',
'NRP' => 'NRP',
)
))
But no effetc.. is this possible ?
Since Sonata Admin Bundle 2.2 choice accepts the "editable" parameter in the list view. You use it like that:
$listMapper->add('etat', 'choice', [
'choices'=>['Brut'=>'Brut', 'NRP' => 'NRP',],
'editable'=>true,
]);
Doc: https://sonata-project.org/bundles/admin/2-2/doc/reference/field_types.html
It's possible for scalar values only. Hear some doc
http://sonata-project.org/bundles/admin/master/doc/reference/field_types.html
Well it's not possible at the moment and won't be possible as I can guess in the near future. Your own realization should be written.

Symfony form read only field

How should I render read-only fields using Symfony form component?
This is how I am trying to do that to no avail:
Symfony 2
$builder
->add('descripcion', 'text', array(
'read_only' =>'true'
));
}
Symfony 3
$builder
->add('descripcion', TextType::class, array(
'read_only' => 'true'
));
}
Provided answers all end up with this exception on Symfony 3:
Uncaught PHP Exception Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException: "The option "read_only" does not exist.
The right way to do this is to take advantage of attr property on the field:
->add('descripcion', TextareaType::class, array(
'attr' => array(
'readonly' => true,
),
));
If you are after way to have a field with data not posted to the server during form submission, you should use disabled like:
->add('field', TextareaType::class, array(
'disabled' => true,
));
on your form builder object.
I believe the only secure method to present a form field as readonly and also prevent your form from accepting a new value in a request is the following.
$builder->add(
'description',
TextType::class,
['disabled' => true]
);
The other suggestion of using either ['attr' => ['readonly' => true]] or ['attr' => ['disabled' => true]] will leave you vulnerable to forged requests.
Both the latter options will set either readonly or disabled attributes on the field, but your Form will still accept a new value for this field if included in the request.
Only the first option above will both disable the form field and also prevent your Form from accepting a new value for the field in the request.
I have tested this with Symfony Form 3.4. I do not know if 4 behaves the same.
You have declared your read only attribute to a string, it needs to be a boolean.
remove the quotes around true
like this:
->add('descripcion','text',array('read_only' => true))
true, without the quotes.
Symfony 4 allows using only "disabled" option in form field. But it is something different that "readonly".
Disabled - user can't edit field and its value IS NOT passed during form submision.
Readonly - user can't edit field but its value IS passed during form submision.
Only solution I found for "readonly" is:
->add('fieldname', TextType::class, [
'label' => false,
'attr'=> [ 'readonly' => true ]
])
read_only is deprecated since Symfony 2.8. So please use readonly instead. And provide boolean value for this attribute
->add('','text',array('readonly' => true))
Update: since Symfony 3.0, the readonly value should be set in the attr option.
http://symfony.com/doc/2.8/reference/forms/types/form.html#read-only
The disabled option can also be used instead.
Would recommend using the disabled option because any submitted value will be ignored as per the docs: https://symfony.com/doc/current/reference/forms/types/text.html#disabled
$builder->add('descripcion', TextType::class, [
'disabled' => 'true',
]);
readonly and not read_only. You shoud make this option in attr like this:
->add('', TextType::class, array('attr'=> array('readonly' => true)))
Let me add something that the other answers didn't help to manage with. The treatment as field but "tweak" to disable edition may work in many cases. However, it's at least difficult to render in some formats that fully prevent edition (i.e., render as a label).
How to solve this? What I did is to define the field as HiddenType, and in the template, render using {{ form.vars.value.myfield }} or {{ item.myfield }} taking "item" as the entity object, enclosed in whatever you can think of, as any other HTML element.
Other solution could be:
->add('value', TextType::class, ['disabled' => true]):
Taken from: http://symfony.com/doc/current/reference/forms/types/text.html#disabled
Only 'disabled' option does not cause an error
$builder
->add('descripcion', TextType::class, array(
'disabled' => 'true'
));
}
If familia and proveedor are relations to other entity i think they shouldn't be text type. Try nullify their types or change to entity type and check if it worked.

Resources