I'm looking for something to make this kind of input in Symfony:
I don't know which kind of input and how to deal with the Symfony controller.
$form = $this->createFormBuilder($newLot)
->add('lotType',null,['required' => false])
->add('lotGenre',null,['required' => false])
->add('ville',null)
->add('quartier',null)
->add('prix',null,['required' => false])
->add('prix', null,['required' => false])
->add('superficie', RangeType::class,['required' => false])
->add('createdAt', RangeType::class,['required' => false])
->add('gestionnaire',null)
->add('lotStatut',null)
->getForm();
Here is a part of my controller but not sure it's useful.
If you want to represent a start date linked to an end date, I'd advise you to create two DateType fields, like this:
->add('startDate', DateType::class, [some_parameters])
->add('endDate', DateType::class, [some_parameters])
Then, you design your two fields in your template, by adding the "à" cell between them.
The RangeType is not appropriate to render dates, it renders this kind of input.
Related
I have an older project which still uses Symfony 2. In it there is a form for editing a client profile. In the controller we have this:
$form = $this->createForm(new ClientProfile($remindTimes), $client);
$form->handleRequest($request);
And in the ClientProfile class we have
class ClientProfile extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('client_name', 'text', array('label' => 'Full name'))
->add('client_address', 'text', array('label' => 'Address', 'required' => false))
->add('client_city', 'text', array('label' => 'City', 'required' => false))
->add('client_post_code', 'text', array('label' => 'Postal index', 'required' => false))
->add('client_email', 'email', array('label' => 'E-mail', 'required' => false));
}
}
... and some other fields, but you get the gist. Then there's also a Twig view which renders the HTML. Standard stuff, as far as I can tell.
Now for my requirement. The client object has two special properties. Let's call them FroobleEnabled and FroobleType. If Frooble is disabled, then the type value has no meaning (can be set to 0). In the UI I want a dropdown with the values:
Disabled
Type 1
Type 2
Type 3
If the user selects Disabled, then FroobleEnabled gets set to false and FroobleType gets set to 0. Otherwise FroobleEnabled gets set to true and FroobleType to 1, 2 or 3 respectively.
How do I do this? The thing which makes this special is that it's not a 1:1 mapping anymore. There are two fields in the model object but just one UI control. I think I could achieve this with the a DataMapper, but I also don't want to manually map all the other fields (though I can, if there's no other option). I also cannot find any decent documentation about DataMapper or any other Symfony Forms features that could help me.
One way to accomplish this is to create a field, let's say, frooble.
In the form class create a frooble Choice field with mapped => false and values 0, 1, 2, 3. Set its choices to strings appropriate to the application.
In the controller, after form submission & validation, include code something like:
...
$frooble = $form->get('frooble')->getData();
if (0 === $frooble) {
$client->setFroobleEnabled(false);
$client->setFroobleType(0);
} else {
$client->setFroobleEnabled(true);
$client->setFroobleType($frooble);
}
I'm trying to create multiple HiddenType fields in with Form Builder using CollectionType, to group these fields when I'm dealing with them in my controller. But I want each field to have different attributes. Is it possible to use entry_options to set different options for each entry?
I'm using a javascript drag and drop plugin to drop elements in a container, when these elements are dropped a JS code updates these hidden fields that, I use to capture the information in my controller. So is mandatory that these fields are grouped somehow so I can deal with them easily in my controller.
An example:
$builder->add(self::FIELD_MAPPED_COLUMNS, CollectionType::class, [
'entry_type' => HiddenType::class,
'data' => ['field1', 'field2', 'field3'],
'label' => false,
'entry_options' => [
'attr' => [
'data-my-field' => '?????'
]
]
])->add('submit', SubmitType::class);
I want to set a different data-my-field for each HiddenType so I can use it in Javascript. Is it possible?
I just faced the same problem. Here is my workaround.
First add a template in the entry_options:
$builder->add(self::FIELD_MAPPED_COLUMNS, CollectionType::class, [
'entry_type' => HiddenType::class,
'data' => ['field1', 'field2', 'field3'],
'label' => false,
'entry_options' => [
'attr' => [
'data-my-field' => '%templateForMyFieldByFieldData%'
]
]
])->add('submit', SubmitType::class);
Then render each field of the collection individually in a Twig loop and replace the template by the required value:
...
{{ (form_widget(field) | replace('%templateForMyFieldByFieldData%', valueToInsert)) | raw }}
...
Hopefully it will be usefull to someone !
I want to make a select dropdown in my form that contains 2 entities.
$form = $this->createFormBuilder(new VisitEvent())
->add('date', 'text')
->add('where', 'entity', array(
'class' => 'PGMainBundle:Field',
'property' => 'name',
)
)
I'd like to use two entities at once in the dropdown. What I've got here is just one of them the second one would be Simulator. I'd like to know what approach I should take.
merge the two entities into an array and pass it to a choice field instead?
Or is there a better way to use it with the entity field?
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'm trying to add the selected attribute to my <option> tags
{% for product in form.products %}
{# of course this should only done inside a if,
but for testing purpose, it's okay #}
{{ form_widget(product, { 'attr': {'selected': 'selected'} }) }}
{% endfor %}
However this does not work. Even exactly the same copy&paste from the symfony2 docs here does not work: http://symfony.com/doc/current/book/forms.html#rendering-each-field-by-hand
I'm adding the form element inside a FormType à la:
public function buildForm(FormBuilderInterface $builder, array $options) {
parent::buildForm($builder, $options);
$builder
->add('products', 'entity', array('attr' => array('class' => 'choseable input-xlarge'),
'property_path' => false, 'label' => 'form.products.title', 'class' => 'Test\Bundle\Entity\Product', 'choices' => $products, 'multiple' => true, 'empty_value' => 'form.products.placeholder'));
}
All variables ($products) are ok in the example above.
Is there a problem?
I'm using Symfony 2.1.9-dev.
Can you update the question with form creation code? I'm pretty sure that Symfony is able to make item selected only if it's present in your model. But since you field is marked with property_path => false I don't think you can override this behavior...
An idea:
Fetch all the data used in your entity field type and pass it to form object (either via constructor or options array)
Add field as you're currently doing it
Invoke setData right afterwards to set field data (using data fetched in step #1)
Btw, I am not big fan of setData but situation like this just calls for it :)
Hope that this helps...
There are several issues with your code. I reformatted the code below for better clarity.
public function buildForm(FormBuilderInterface $builder, array $options) {
parent::buildForm($builder, $options);
$builder->add('products', 'entity', array(
'attr' => array('class' => 'choseable input-xlarge'),
'property_path' => false,
'label' => 'form.products.title',
'class' => 'Test\Bundle\Entity\Product',
'choices' => $products,
'multiple' => true,
'empty_value' => 'form.products.placeholder'
));
}
Don't use class inheritance for form types (except of course extending AbstractType). Use getParent() for extending other types instead.
You can't set attributes of <option> tags manually. You would have to render the tags by hand.
For marking choices as selected, you need to pass default data to the field. Usually getProducts() would be called here to get the defaults, but you disabled that by setting property_path to false. So you should set the defaults manually by using the data option. Note that the defaults need to be an array/a collection, since multiple is true.
You are manually passing choices, which is a bad practice. If you just remove this option, the entity field can itself query the choices from the DB and optimize the needed statements for better performance.
Format your code. Really. :P