Symfony2 validate only on creation - symfony

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.

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

Change the default identifier of the type entity in symfony forms

I have a form which has a field whose type is entity.
And by default the values used to identify each entity are database identifiers.
How do I override that? I'd like to use another column which is unique as well.
This is too easy as of symfony2.7 through choice_value, however I'm working on the old 2.3.
symfony 2.3 & php5.3

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.

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