I have two Entities, called Bookings and Rooms. The Rooms are embedded as a collection in to the Booking Form. Everything works fine.
I then installed SimpleThings/EntityAuditBundle to version changes to the records. This all works fine and as expected except for one thing.
Say I have two Rooms defined in a Booking. I edit the Booking, but made zero changes to the form, and save it. Strange - the room_audit table now has a record saying I changed its values to one of the Room records via the embedded form collection etc. But I made no changes before saving.
Then via trial and error, I determine that if I remove the price form field from the RoomType embedded collection, which is a currency form type, then this additional audit record does not appear. If I change the field type of price to numeric then it appears again on saving. If I then change the field type to text then this additional record stops appearing.
SUMMARY
It seems that having a numeric field type such as a currency or a number in a embedded form collection is forcing the preUpdate lifecycle callback to be triggered even when there is nothing to update as no changes have been made. Change the field type to text and the preUpdate handler is not called unless there are values altered in the form.
Does anyone know why this behaviour seems to be based on using numeric form field types?
I'm using Symfony2.5.2 standard edition on MySQL with PHP 5.5.9
Thanks
I had a very similar issue when processing a form collection of entities that contained a text field. If I loaded the form to edit - but did not make any changes - it still schedule updates for all of the entities in the collection.
I discovered that in my Doctrine2 annotations - when defining the entity - the property was declared as an integer - not a varchar. When I changed the form field to integer - the form handler no longer attempted to update all of the entities in the collection.
So - perhaps you should look at your Doctrine2 annotations and make sure that the form field type matches your database schema.
Related
I have a truck table and a postman table. Both tables have a one to one relationship. One truck has one postman, one postman has one truck.
Inside postman/create.blade.php file, there is an HTML form containing truck_number, postman_number, postman_name, and date_of_operation to be filled by the user.
the truck_number is presented in a dropdown list format, and the list get automatically updated everytime a truck_number is created from the truck table. inside the truck table, each of these truck_number has its own date_of_operation.
Therefore for the date_of_operation field inside postman/create.blade.php file, I want the value to get updated automatically within that same form when the user selects a particular truck_number associated with it.
Is it possible to do this in laravel without involving any form of ajax or json? How can I implement it correctly?.
You cannot get the value until you have sent a request to server.You can use livewire, A fullstack framework associated with laravel. You can easily do the stuffs you are willing to do. Here is Documentation:
https://laravel-livewire.com/docs/2.x/quickstart
What I want to do
I'm trying to build a form in Symfony 2.8 that requests the user's choice between many types of credentials and add an input box to the form it's similar to this one but I use a select option field instead of buttons.
What I did
I was able to create this form with HTML and JavaScript.The user can select more than one credential type only once except the OTV Option that can be selected without limits.
image here
What's my problem
I need a way to store user's data in a session and get them in my controller.
Is it possible to pass submitted data from HTML form to a Symfony controller and if not how can I build a similar form with Symfony's formBuilder ?
CollectionType is probably what you are looking for.
For your purposes, you can think of it as adding rows to the end of a two-column list. The Left Column has credential_type with Name, Email etc in a <select>; and the Right Column allows for your text values.
You'll have two FormTypes: one for the list, and another for the list item. In the list item FormType, you should be able to set a CallbackConstraint to validate e.g. that email values are valid upon form submission.
All user changes are saved at the same time, only when the form as valid.
I think you can do that. There is CollectionType in Symfony form. You can read about it here and how to implement it here
P.S. The main problem in this case is to generate specific names for dynamically created fields to provide a normal handling data in controller.
I'm developing an application using Symfony2 and Elasticsearch. To communicate the ORM-way I use the Ongr ElasticsearchBundle (https://github.com/ongr-io/ElasticsearchBundle).
In my project I have a page which displays data in a table. The user should be able to filter this data. So I made a form (without entities) containing a formcollection. A FormType can be added (several times) to this collection using Javascript. This FormType contains a textfield and a dropdown. The dropdown is filled with all possible fields for the particular Elasticsearch document (comparable to a Doctrine entity). So, the user chooses a field from the dropdown and this field should match the value in the textfield.
This actually works, but I try to find a way to validate this. The value is filled in in a textfield, so the form is always valid. But some fields should be integers or dates.
I'm building a simple form having a one-to-many relation from an User entity :
User 1..* actions
So basically, in my user form, i have to set the actions property to the collection form type.
I don't want the client to be able to add multiple actions within the form, but only one when editing the user.
The problem is that Symfony 2 form expect to get all the actions from the form on submit, so existing actions are removed from database on flush and only the new one remain.
Any idea ?
If your entity User has an array of Actions, and you create a form type UserType with data_class => User, Symfony expects you to add to that form a collection field for actions, because that's what the entity has.
That's the way Symfony works. Personally I don't like this tight coupling between entities in the model and forms, but it works pretty well for simple apps.
If you want to have a form type that does not match any entity, things won't be so simple, but you'll have more control of things.
You have some options (as far as I know, maybe there're other):
You can move to a task-based UI, by creating another class that represents the data you need to perform an action - in your case you want to edit an user, so you can create a EditUserCommand, which contains 1 and only 1 Action, as long as other fields, most of them probably exactly the same as your User class.
Now you can map your form to this EditUserCommand class, which will contain only 1 action as you want. Obviously later you need to use this command class to get the data you need to edit the user.
Use the empty_data option with a closure that receives the submitted FormInterface, so that you can get the submitted data from there and use it as you want to update the user.
In both options you're breaking the coupling between your User class and your EditUserType form, which means you need to do some more work, but imo it has many benefits.
You can find further reading on this subject here.
I have tried a lot and have not yet able to find solution to this problem. i am not sure if thats possible or not.
I have one table called Student then table called Assigment and Solutions
Now there are questions Stored in Assignment Table which are common to all students. But different student will submit different solutions.
On the form i want to have label as Question text and then text box as solution to that question.
IN the beginning i have the empty answers in database.
I am confused how can i use symfony to build this form
Becasue all solutions will be submiited togetehr with one submit button
Class AssignmentFormType{
$builder-> add('answer','collection' ....)
This is working fine but only thing here i want is to display that QuestionText as label for that Answer and i am not able to find that for 1 week
What you are trying to do is to dynamically create a form element based on information from your entity (i.e. setting label = $entity->getQuestion)
Two approaches:
A. Dynamic forms
http://symfony.com/doc/master/cookbook/form/dynamic_form_generation.html
Basically involves setting up a listener then using it to create your form element with access to each individual entity. Bit of a pain but it works.
B. Inside of the form template
Since all you want to do is to change the label then it might be easier to just set it inside of your form template. Something like:
{{ form_label(form.assignment, form.assignment.vars.value.question) }}
The .data should give you the actual assignment entity from which you can pull the question.