I have a "teachers" field in a form.
I would like to be able to select a blank value.
But in the symfony doc, I read that I have to set the multiple to false and the required to false, to add an empty_value and an empty_data. I understand the required. But why is this necessary to set the multiple to false ?
How can I add an empty_value with a multiple => true field ?
Here is my builder :
$builder->add('teachers', 'entity', [
'class' => 'Ent\UserBundle\Entity\User',
'multiple' => true,
'query_builder' => function(\Doctrine\ORM\EntityRepository $er) use ($options) {
$qb = $er->createQueryBuilder('u');
$qb->where($qb->expr()->eq('u.group', '\'ROLE_TEACHER\''));
return $qb;
}
]);
You cant specify at the same time multiple and empty_value. Because multiple mean that you have got checkboxes at form or select with multiple choices. In the first case, you can uncheck all checkboxes and recieve empty array, in second you can do the same with ctrl button.
symfony doc
Related
I've made a form that submits the parent organisations of an individual user. The database table the entity is linked to contains multiple entries for the organisation(as there are multiple users associated with each organisation) and I only want to return one instance of the organisations name rather than 20 duplicates.
I have heard there are functions such as distinct() or findOneOrNull but I am not sure how to implement them.
Here is the code from the form:
->add(
'userParent',
EntityType::class,
[
'class' => UserParent::class,
'choice_label' => function ($parents) {
return $parents->getParent()->getName();
}
]
)
You can specify a QueryBuilder for an EntityType :
->add('userParent',
EntityType::class,
[
'class' => UserParent::class,
'choice_label' => function ($parents) {
return $parents->getParent()->getName();
},
'query_builder' => function(UserParentRepository $r) {
return $r->getUniqueCompanies();
},
]
);
Then you'll have to add getUniqueCompanies() in the UserParentRepository class, that's where a DISTINCT will help.
You should use the query builder:
https://symfony.com/doc/current/reference/forms/types/entity.html#ref-form-entity-query-builder
With that you can create an own query which entities you want to show. But one hind! If you group the entries from the table, means you will link only to one specific row from the doppelgängers... that sound a bit strange.
I am adding fields to a project based on a project based on Symfony 2 and Sonata. I am trying to follow the instructions from this answer. In one of my admin classes, I have inserted the following code:
$default = 'Germany';
if (!$this->getUser()->hasRole(User::CONTENT_SUPPLIER)) {
$formMapper
->tab('Distribution')
->with('Distribution')
->add(
'module',
null,
[
'empty_data' => $default,
]
)
->add(
'distributions',
'distribution_list',
[
'label' => false,
'required' => 'false',
'disabled' => true
]
)
->add('plannedDistributions')
->end()
->end()
;
}
... and while I expect to see a reference to the "Germany" object by default in my form, I instead see an empty field. Should I be passing in an object rather than a string? Is what I'm trying to do even possible? What am I doing incorrectly here?
I think you missed a crucial bit in the documentation regarding empty_data:
This option determines what value the field will return when the submitted value is empty (or missing). It does not set an initial value if none is provided when the form is rendered in a view.
This means it helps you handling form submission with blank fields.
That means empty_data will populate your model with the data when the form was submitted without a default value.
I'm not familiar with the $formMapper used in your snippet, but in a typical Symfony-Controller you could create your form like this:
$form = $this->createForm(MyForm::class, $initialData);
In this case $initialData contains a property Distribution with the value Germany. Alternatively you could try providing the value in your frontend.
To set default data use option 'data'.
Sample:
//Use block
use Symfony\Component\Form\Extension\Core\Type\TextType;
//...
$formMapper
->add('module', TextType::class,[
'data' => 'Gearmany',
]);
I would like to add a collection of input text with same name (i.e. name="blabla[]") filed to admin block with add/delete buttons.
I'm using collection form field type but can't see add/delete buttons
public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
{
$formMapper->add('settings', 'sonata_type_immutable_array', array(
'keys' => array(
array('title', 'collection',
array('type' => 'text' ,
'required' => true,
'allow_add' => true,
'data' => array('First' => 'One')
)
)
)
));
}
I get below result without add/delete buttons!
Any idea how to get it working ?
I think you should use sonata_type_collection or sonata_type_native_collection instead of collection.
Here is an extract of the field doc :
14.1.7. SONATA_TYPE_NATIVE_COLLECTION (PREVIOUSLY COLLECTION)
This bundle handle the native Symfony collection form type by adding:
an add button if you set the allow_add option to true. a delete button
if you set the allow_delete option to true.
In my symfony2 project I created a new FormType which is called "ChoiceAndOrTextType" which is a list of choices (checkboxes) but the user can also check the "others" option and write his answer into a textfield.
The code is like this one here in the answer of bschussek:
Use a conditional statement when creating a form
$builder
->add('choice', 'choice', array(
'choices' => $options['choices'] + array('Other' => 'Other'),
'required' => false,
))
->add('text', 'text', array(
'required' => false,
))
->addModelTransformer(new ValueToChoiceOrTextTransformer($options['choices']))
;
Now i want to validate this correctly, so when the user checks "Others" then the textfield needs to be filled out, if "Others" isn't checked it can be blank. (Kind of dependent validation).
How do I do this?
You need to use two validation constraints, for example in validation.yml yaml file
I have a formbuilder where I am adding some values from an entity:
$builder->add('affiliation', 'entity', array(
'class' => 'SciForumVersion2Bundle:UserAffiliation',
'multiple' => true,
'expanded' => true,
'query_builder' => function(EntityRepository $er) use ($author,$user) {
return $er->createQueryBuilder('ua')
->where("ua.user_id = {$user->getId()}")
->andWhere("ua.affiliation_id not in ( select pa.affiliation_id FROM SciForumVersion2Bundle:PersonAffiliation pa where pa.person_id = {$author->getPersonId()} )");
},
'required' => true,
));
In my controller, I would like to check if there is something in my form. If there is something, I will display one view, if there is nothing, I will display another view.
Is this possible and if so, how?
Thank you.
Simply try it:
$data = $form->getData()
function getData() documentation Book
If you want to get the current data (just after rendering the form) in your form type you can use the builder supplied in each form type by standard.
It works exactly as a normal form response, so you can use:
$builder->getData();
and use if clauses to add different fields depending on what you want to generate.