im developing a simple user profile editor for my app. The values that user can edit are, e-mail, password and phone numbers.
Obviouslly, i have the User entity, that have an Phone entity collection. Also, i have the UserProfileType and the PhoneType.
The buildForm method of userProfileType, include the phone numbers:
$builder
->add('telefonos','collection', array(
'type' => new TelefonoType(),
'required' => false,
'allow_add' => true,
'allow_delete' => true
))
And the PhoneType buildForm is:
$builder
->add('valor','text',array(
'required' => false
))
->add('descripcion','text', array(
'required' => false
))
->add('user')
;
The phone numbers are displayed correctly in Twig template...but the user field is displayed as combobox and allow the user "change the user value". I wish that the user value will be implicit. How can do this ?.
I am newest with Symfony forms. Please, help me. Thanks.
As you didn't tell Symfony what type should be your user field, it's trying to guess what is the most appropriate type (you can look here for some details). So, in your Telephone entity, I suppose that the user attribute is declared as a User (another entity of yours).
As a consequence, Symfony create a form field with entity type (doc here).
If you want an "implicit user set", this is quite impossible, you have to remove the field addition in you form builder, and call setUser manualy on your entity(ies) in your controller action or a service.
You can get the current logged in user from the security token, or directly in a controller action : $this->getUser()
Related
I am trying to add a checkbox in my form in Symfony.
The aim of that checkbox is to eventually remove uploaded image if checked.
So it shows up only when a file is uploaded. I also want it to cancel upload before form entity is persisted.
But I am not there yet, the code that is supposed to do that is currently commented out.
This checkbox corresponds to a boolean property in my File class so that it allows to access its value in controller. At the beginning that property didn't exist and so I was unable to access this value from entity or form /getData.
My problem is having the field in my formtype messes up with persistence.
All properties acquired from the form are considered null by doctrine. Even though a file was selected, renamed with uniqid etc . When time has come to flush, the fields are considered null. Removing DeleteFile from form solved the issue. I have tried setting it with property mapped = false, to no avail.
Here is code from form:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file', 'file', array(
'label' => false
))
->add('deleteFile','checkbox', array(
'label' => 'Delete File',
'attr' => array('class' => 'delete_file'),
'mapped' => false,
))
;
}
Thank you for your input. I am also open to alternative solutions to that "interface" if a better solution can spare me from having to deal with this issue I am interested.
I have a form that contains a collection field-type.
However I don't know how to add/remove fields to/from the collection.
$builder
// ...
->add('covers', 'collection', array(
'required' => false,
'type' => new BookCoverType(),
'allow_add' => true,
))
;
The rendered form looks like this:
How can I add a new cover using the collection form field?
Symfony does not provide the add/remove JavaScript methods/buttonss or any session-based solution without JavaScript out of the box.
It renders a data-prototype attribute that can be used as described in the documentation chapter How to Embed a Collection of Forms -> Allowing "new" tags with the "prototype".
Some bundles provide this functionality though. Those are primarily the bootstrap bundles:
braincrafted/bootstrap-bundle
mopa/bootstrap-bundle
...
Just dive into their code - i.e. braincrafted/bootstrapbundle's bc-bootstrap-collection.js.
I would like to create a custom form field. For example, select field of cities in the world.
I read this article. In this article, the data are loaded using parameter file config.ynl. I, however, I'd like to upload this data from my database.
http://symfony.com/doc/current/cookbook/form/create_custom_field_type.html
Can somebody tell me how to do this or send the link to the example
In this case, better is to use an Entity Field instead of a Choice Field. It allows you to request your database to load your entities as follow,
$builder->add('MyField', 'entity', array(
'class' => 'MyBundle:MyEntity',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('e');
},
'property' => 'something',
));
The property option define the entity property you want to use.
You can then customize your field using the "multiple" and "exanded" options to behave the way you want. Read the documentation
I have an order and a client entity.
I am wondering if it's possible with the actual Symfony2 form system to create an order form which will allow to:
Select several clients from a dropdown (mix of collection and entity form type)
And to create new clients on the fly (the default way for the collection type) if not in the dropdown list.
I've seen some way to do it by creating multiple forms in the same page, but this is not the way I would like to achieve it.
Are there any better ways to do this?
I had a similar problem which may lead to your resolution:
I have a Category and Item relationship (Many-to-One) and I wanted to either select an existing item or create a new item.
In my Form class:
$builder->add('item', 'entity', array(
'label' => 'Item',
'class' => 'ExampleItemBundle:Item',
));
$builder->add('itemNew', new EmbedItemForm(), array(
'required' => FALSE,
'mapped' => FALSE,
'property_path' => 'item',
));
$builder->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event) {
$data = $event->getData();
$form = $event->getForm();
if (!empty($data['itemNew']['name'])) {
$form->remove('item');
$form->add('itemNew', new EmbedItemForm(), array(
'required' => TRUE,
'mapped' => TRUE,
'property_path' => 'item',
));
}
});
You can map two fields in a form to the same property using the property_path option. Then, using form events, use the submitted data to make a decision and modify the form so that only one of the fields has a mapped option that is true.
If I have understood, you want to create and store new clients in a Form "on fly", at the moment. I think that you have to do that using JavaScript and set an additional action in your controller.
JS -> Capture the event to add new client to you database (i.e. "Add new" button click event)
JS -> Inside this event, call via AJAX to your controller with the values of new client. (Using FOSJsRoutingBundle is easy to do)
Symfony2 -> Inside your new action, store the new client in your database.
JS -> OnSuccess event, in your AJAX call, add the new Client to your DropDownBox
(ddb.append(new element tag)
Just doing that you have your new client stored in the database and added to your dropdownbox
For my part i had the same kind of problem and i resolved it by creating 2 attribute in my formType;
For example, for you it would be:
customer->entity
new_customer-> collection
In your order entity file you will have to add 3 methods (getter, setter, and remover) getter and remover don't do anything but setter should call the setCustomer(c)
I'm not sure if it is the best way but it's the only way I figure it out!
The collection Form type allows to add and delete on the fly with allow_add and allow_delete attribute.
More informations by following these 2 links :
Official collection form field type reference
Cookbook about add and delete on the fly with collection type
If you don't like to get supplementary forms on the same page, you can integrate them in dialog boxes... But you definitely need a form to create new items...
I added custom fields as described in magento add custom input field to customer account form in admin
But I want a select list, not only a text input one. I don't know which kind of parameter I have to set and how to tell the list of possible values.
Please help :)
Thanks,
Plantex
Where you might do something like:
$setup->addAttribute('customer', 'custom_attribute', array(
'type' => 'text',
'label' => 'Customer Custom Attribute',
));
Use these values instead:
$setup->addAttribute('customer', 'custom_attribute', array(
'type' => 'int',
'label' => 'Customer Custom Attribute',
'input' => 'select',
'source' => 'eav/entity_attribute_source_boolean',
));
The type is int because you will typically be storing the index of the value chosen, not the value itself. The input is select so the admin renderer knows which control to use. The source shown here is a common example, it provides an array of "Yes" and "No" values with numeric indexes.
There are many source models already in the Magento code that you can use and you can create your own too, look at any existing one to see how it returns an array. If you make your own and if it uses text indexes instead of numeric then the type will have to be changed back to text.
Try adding this at your module setup file
'value' => array('notate_to_zero'=>array(0=>'Bleu',0=>'Rouge',0=>'Vert',0=>'Violet',0=>'Noir',0=>'Orange'))
),
or look at this --> http://inchoo.net/ecommerce/magento/how-to-create-custom-attribute-source-type/