Exclude Entity fields from updating process in forms Symfony2 - symfony

I'm in a situation where the edit form of an Entity is a little bit different than the create form. I don't show some fields, because I don't want them to be editable.
But when I save this form, all not included fields are set to null and are saved, but I want to exclude those fields from the whole updating process.
How can I achieve this?

There are several options:
Create the basic form type for editing and extend it to add additional fields for creating.
Keep just one form type but add some fields conditionally — that is, only when the entity is new. You can get your entity in the form type as $options['data'] and check if its ID is not null or whatever.
Use form events.

Another option is to submit form instead of handleRequest and pass the second parameter $clearMissing to false:
$editForm->submit($request->request->get('form_name'), false);

Related

How to map entity property in Symfony form

I have an entity - 'house' I want to edit in a symfony form. One of the properties of the entity is another entity 'owner' mapped in a manytoone relationship.
When I create the House entity I know who owns it and that canlt be changed so I need to pass the owner in the form, but not in a way that can be edited.
If I wasn't using Symfony I would just pass it as an 'OwnerID' hidden field. In Symfony I know I can pass it as an entitity type but that is a choice field, visible and allows the owner to be changed.
So how should I pass it in symfony?
Simply adding it to the form as 'Owner' gives me an array to string conversion error.
To avoid the array to string conversion error you should make a __toString() function in order to print the OwnerId value.
Once there, you can add the disable property to the owner field as explain in this doc
in order to do that you need to set default values or insert some logic on submit actions.
Hope it works!
You can use the option :
"disabled" => true
Documentation:
If you don't want a user to modify the value of a field, you can set the disabled option to true. Any submitted value will be ignored.
Official documentation

Symfony2 validate only on creation

I have some entity and want to validate one field of this entity only on create object.
Example: I have Post entity and title has to have at least 10 characters when I create Post, but when I update it it can have less characters.
How can I do it with Symfony2? It's possible with default validator and default forms?
You can add the Length constraint for the title attribute to a dedicated validation group that will only be validated when the entity is not updated.

How to get informations from two different form in Symfony2 and save them?

Actually, I have a form with an input to persist an entity. I want to create another form somewhere else with more information and I want to save everything from the submit which is in the first form. I can't include the second inputs in the same form, because this is not the same view. What's the best way to resolve this issue?
Thanks in advance
This sounds difficult.
Have I understood this right:
You have on entity with some required information and some nullable, additional information.
Now there are two forms one which creates the entity with the required information and you than want to update the form with the additionals.
Is this correct?
I would try two things:
Default
You create one FormType with all the fields and have two views which only render the required fields (for this don't use form_rest).
Than do the form handling like descriped in the docs.
It could be that this doesn't work with the validation.
Use FormModels
Under Form/Model/FormModelClass you have the two classes which only have the attributes the one form requires. The Form component interacts with them and maps all attributes to this models. When the form is submitted successfullly and valid you can manuelly map the attributes back to your orginal entity and persist it

How to deal with the choice "other" in EntityChoiceList in Symfony2 forms?

I've got a model as described below :
I've also got a form to create a new product with a field entity building a dropdown list containing all the Brands.
Now I want to add a value "Other" in this list in order to allow the user to specify the Brand manually in another text field.
The question is: is there a clean way to manage this case (eg. adding the value "Other" in the list, which is not an entity and get the form validation to work) with Symfony2 forms?
You can do it in two ways,
You can subscribe for FormEvents::BIND_CLIENT_DATA form event. In the event method you can create new Brand object from the text, save it and set the id to the form by calling $event->setData($data). See this cookbook entry.
OR
You can append a data transformer. In its reverseTransform method you can create+save the object and return its id. See this cookbook entry.

Texbox custom representation for Symfony2 entity form type?

My entity Person has a one to many relation with Tag entity. When creating a new person i'd like to show a textbox for assigning tag names separated by commas. User can then input an existent tag name or a new tag name (in this case, a new Tag entity is created and persisted).
Unfortunately entity field type has only checkbox and select representation. How can implement my own textbox representation?
You need to create a custom form type along with a custom data transformer.
This article is a pretty good guide on creating your own form type.
Also, check out Symfony's own source for the EntityType to get an idea of what's going on.
One way would be creating a custom data transformer.

Resources