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.
Related
I am trying to build a form and propose a language selection. I want this selection to duplicate when the user wants to add several languages.
My question is : Is it possible to use CollectionType with LanguageType class ?
Thank you.
Yes, absolutely.
Check out one of the examples in the Docs:
$builder->add('emails', CollectionType::class, [
// each entry in the array will be an "email" field
'entry_type' => EmailType::class,
// these options are passed to each "email" type
'entry_options' => [
'attr' => ['class' => 'email-box'],
],
]);
It is totally up to you to specify the underlying form type to be used, whether it is TextType or LanguageType. And you configure the type via the entry_options key.
Hope this helps...
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'm working on a sf2 project using Sonata Admin Bundle.
The project is a Donations website for humanitarian mission.
I have a 'Personne' entity, represent benefactors (donation makers).
My problem is the following:
I have to filter results in the sonata list view using autocompletion.
I want filter results using the 'name' property of the current entity ('Personne').
What I'm expecting :
$datagridMapper
->add('personne', 'doctrine_orm_model_autocomplete',
array('label' => 'AutoComplete'),
null,
array('property' => 'name'))
// error output : " The option `association_mapping` must be set for field: `personne` "
You can see my full admin class and entity on this gist :
https://gist.github.com/chalasr/0658a02b1c04180f5563
I understand this field type is reserved to entity associations (by example I already use it for filter results of my Donation entity by Personne name (other admin class).
My question is :
Is it possible to do what I need ?
If I can't do that using this field type, what is the right way to achieve this task ?
Thank's for your help.
After a lot of tests, it seems this functionality isn't yet provided by Sonata.
So, I had build an homemade autocomplete method in my admin controller and used it as ajax in my overridden CRUD:list.html.twig template.
This method takes field name, other autocomplete fields values, and keyword as parameters and reload results on keyup event.
You can look on this gist:
https://gist.github.com/chalasr/5c27ae64dc596967f18a
If you have an idea/proposition for optimize my code (simple autocomplete field type for $formMapper ?), I'm really interested.
Well it's possible to make a choice field and use integrated select2 to handle autocomplete, I don't know how well it would fare with large tables though.
$datagridMapper
->add('personne','doctrine_orm_callback', array(
'callback' => array($this, 'filterByName'),
'field_type' => 'text',
), 'entity',array(
'class' => 'AppBundle\Entity\Personne',
'choice_label' => 'name'
))
We add 'doctrine_orm_callback' because regular string filter can't handle EntityType Field, so we need to do it ourselves.
public function filterByName($queryBuilder, $alias, $field, $value)
{
if (!$value['value']) {
return;
}
$queryBuilder
->andWhere($alias . '.name' . ' = ' . ':name' )
->setParameter('name' , $value['value']->getName());
return true;
}
1 more thing, select2 won't create autocomplete (search) box if dropdown box has less than 10 choices, because it's set up in Admin.js that way.
select.select2({
width: function(){
// Select2 v3 and v4 BC. If window.Select2 is defined, then the v3 is installed.
// NEXT_MAJOR: Remove Select2 v3 support.
return Admin.get_select2_width(window.Select2 ? this.element : jQuery(this));
},
dropdownAutoWidth: true,
minimumResultsForSearch: 10,
allowClear: allowClearEnabled
});
so you need to override it, if you want that to be less.
Fonctionnality is now bundled with Sonata as simple as:
$filter->add('label', ModelFilter::class, ['field_type'=>ModelAutocompleteType::class,'field_options' => ['property' => 'name']])
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.