I am new to Symfony, and Im wondering whether there is a build in way to show a not editable input field within a form.(not hidden, not editable, so it just appears in a box)
I am using echo_field and doctrine model.
Just set the read_only attribute to true when you make the form.
$builder->add('dueDate', 'text', array('read_only' => true));
http://symfony.com/doc/master/reference/forms/types/text.html#read-only
Related
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
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.
I have an embedded form (for Address) which has its own validations for various properties. I embed this form in a parent form (for Person), and I have a checkbox on the parent form that says something like "Person has an address?"
When the checkbox is left unchecked, I want to disable all the validation for the embedded Address form. Or, better yet, if I can just remove the embedded form from being submitted completely that would be OK too.
I looked at using validation groups, but the use case doesn't match my own.
OK, figured this out. When adding the AddressType embedded form in my form builder, I just pass in the option for validation groups like so:
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$form = $event->getForm();
$form->add('address', new AddressType(), array(
'label' => 'Address',
'validation_groups' => function (FormInterface $form) {
if ($form->getParent()->get('toggleAddress')->getData() === false) {
return array();
}
return array('Default');
}
));
});
Within the validation group function, a check is made to see if the toggle to enable Address is off. If so, return a blank array, with removes all validation groups, including the "Default" one.
You try to fix your issue with validation group which will not cover your use case (it can but it will be tricky because en empty Address object will be linked to your Person object).
Basically, you embed your Address form everytime whereas it should only be embed when the checkbox is checked. IMHO, you should rely on dynamic form as explained here.
With this solution, you will need extra JS code in order to update you form when you click the checkbox in order to update the whole form accordingly. Then, there will be no issue about validation because the Address object will only be created when the form is embed.
Additionally (just for information), you can add/edit validation groups according to the submitted data as explained here.
Hope my answer is helpfull!
Perhaps someone can help me. I've just installed the Grid Field Extensions Module for Silverstripe (https://github.com/ajshort/silverstripe-gridfieldextensions) because I need inline editing/adding. It works, but simple TextFields are shown as textareas and not as a simple textfield.
Can someone tell me how to change that?
The module attempts to automatically work out what field would be best for your variable. It will create a DropdownField for an Enum variable, TextareaField for a Text varialbe and so on.
If you don't want to manually set the field types for each variable that you want to be editable inline you need to change your variables a little.
TextareaField is the field set for Text variables.
TextField is the field set for Varchar variables.
For any variables that you want to be a TextField instead of a TextareaField change it's type from Text to Varchar(255) (or however large a character limit you need).
Otherwise you can manually set the fields using
setDisplayFields as described in the documentation.
$grid->getConfig()->getComponentByType('GridFieldEditableColumns')->setDisplayFields(array(
'FirstField' => function($record, $column, $grid) {
return new TextField($column);
},
'SecondField' => function($record, $column, $grid) {
return new TextField($column);
},
// ... etc for each field you want to be editable
));
I have a form containing a checkbox and a "value field". The value field could be anything, a text box, a compound field, a collection - anything.
The form could look like this, for example:
field_1_label enabled [x]
value [________]
field_2_label enabled [x]
value sub_field_1 [________]
sub_field_2 [________]
field_3_label enabled [x]
value [________]
When the "enabled" field contains true, everything works fine already. When the "enabled" field contains false, I would like to disable validation on the value field and it's child fields.
So when "enabled" is un-checked, I will effectively ignore the field. I will still display it in the form, but I won't store the data and I certainly don't want it validated.
Does anybody have suggestions for how I might do this? Specifically, I'm having problems getting the validation system to ignore the value field and any potential child fields.
In Symfony 2.3 you can use false in validation_groups to have no constraints applied:
https://symfony.com/doc/current/form/button_based_validation.html
So for example, on the field containing the checkbox and value field:
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver
->setDefaults([
'validation_groups' => function(FormInterface $form) {
// If the form is disabled, don't use any constraints
if ($form->get('enabled_checkbox')->getData() == false) {
return false;
}
// Otherwise, use the default validation group
return 'Default';
}
]);
}
Just remove the child fields prior to validation if the parent's checkbox is set to false.
Read more in the cookbook article How to Dynamically Modify Forms Using Form Events.
Subscribe to form events FormEvents::POST_SET_DATA and remove the field in your subscriber.
The section Adding an Event Subscriber to a Fom class covers this topic.
You can aswell introduce different validation groups for your form.
Just apply another validation group ( not containing the chield fields ) if the parent's checkbox is set to false.