How to map entity property in Symfony form - symfony

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

Related

How does Symfony generate the id/name for a form field?

In buildForm() I would like to extract the full id/name of the current form field node. $builder->getName() returns only the name of the current node but I need the full property path, for example:
id="type_employments_0_location"
name="type[employments][0][location]"
Is there any way to generate this while building the form?
I'm working on a custom mandatory field type extension that looks up the "mandatoriness" of each field as the form is built; hence I need the full property path in buildForm() so that I can modify the options array.
From the FormConfigInterface, You should be able to use $builder->getPropertyPath().
It will return a PropertyPathInterface object, just use it as a string to get the real property path as string (i.e. print $builder->getPropertyPath() will give type[employments][0][location]).
Actually, it's pretty easy.
For every field type has many variables assigned.
<label for="{{ form.fieldname.vars.id }}">...</label>
From symfony doc (Form Variables Reference):
variables are common to every field type. Certain field types may have
even more variables and some variables here only really apply to
certain types.
Assuming you have a form variable in your template and you want to
reference the variables on the name field, accessing the variables is
done by using a public vars property on the FormView object.
Form Variables Reference
In a nutshell: The full property path generated by the form framework is not available to buildForm() but is available to buildView() and finishView(). Use those if you need access to the full property path.

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.

FOSUserBundle registration form overridden - validation problems

I've modified my registration form and added a few fields (such as First name, Last name, and Organisation name).
My application will have 2 types of users - "regular" users, that will have to provide their First name and Last name, and also "organisations", that will have to provide their organisation name.
When the form is rendered, the system detects the user's "type" and only shows the form fields relevant to that user (for example: "organisations" don't get the First name and Last name fields).
The registration works fine, but there are no validation messages shown... Even if there is an error, it is not displayed.
How should I handle this?
I think it would be a better idea for you to instanciate a different form for different user level, instead of doing that in templating.
Use this:
parent::buildForm($builder, $options);
where you extend your form.

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.

Exclude Entity fields from updating process in forms Symfony2

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);

Resources