Symfony : a form field does not want to be null dispite the constraints - forms

In my entity Transaction, the field credit is nullable. field credit in entity Transaction
In my transactionType,I wrote for the chield credit, the option required at false.
the chield credit in transactionType
But when I submit the form, a message asks me to fill in the field.
I tried changing the attribute in the entity, and the constraint in the form but it didn't work.

Related

Could not get element id from s58c147845272f_products_2_suppliers_0_contacts Failing part: contacts sonata_admin symfony3

The Setup
I am working on a project using Symfony 3 and SonataAdminBundle 3.1.
I am using the nested form method of sonata admin where I have 4 entities:
Category, Product, Supplier, and Contact. They all have one-to-many relation with each other respectively.
I am using sonata_type_collection to put products in category form, and using the same to put suppliers in product form, and using the same to put contacts in the supplier form. The contact form has a sonata_type_model_list field for zipcodes.
The Problem
I am facing the following error when I click the add new contact button on the supplier form from within the category > product form.
Could not get element id from
s58c147845272f_products_2_suppliers_0_contacts Failing part: contacts
The error does not show up when I save the form step by step - like i add a product to the category form then save the form, then add a supplier to product and then save the form, and when i add the contact - the contact form is loaded and the above mentioned exception is not thrown. Also When I go directly to Supplier form and add the contact there, the exception is not thrown.
It is only when the supplier is not saved from within the category form that the above exception is thrown.
any help is highly appreciated.
Check if any of your properties have underscore in the name, like my_file. Try to change it into myFile and also accordingly change code in your Admin class:
$formMapper->add('my_file', 'file'); => $formMapper->add('myFile', 'file');.
Update:
I think I know what it is specifically - you probably have reference to your parent in your children. If that's the case, then add in your Admin classes for entities that have reference to the parents:
$formMapper->add('[parent_reference_attribute]', 'sonata_type_model_hidden');
Replacing [parent_reference_attribute] with name of your field referencing the parent.
Then exception should be gone, at least it was in my case.

Symfony: get new ID in a form

I have an object displaying within a form, a hidden field being related to its PK (field id).
When I create a new object, the field has a null value. Submitting the form, the object is inserted into the DB and it now has an ID, but the field in the page still has a null value.
If a reload the page, now the ID is indeed set in the hidden field.
In my opinion, this is due to the form processing of Symfony: when a create an object, it creates a form, with this form valid the object is saved but the form still uses the data before it was saved.
The question is: how to get the auto-incremented key in the form up-to-date? Shouldn't the form only have a reference to the object? Can't the value be updated?
make sur that you call $entityManager->flush() method after insert and that you bind your form whene you have same data in your request object
$form->submit($request->request->get($form->getName()));
You should have an Entity assigned to your Form by FormFactory. Then Symfony will fill that Entity with submitted values. What's left is only persist the Entity and flush to database.
You can find steb-by-step form submission in Symfony Cookbook

Foreign entity in form into different kind of input

I have two entities: product and category (Symfony 2.3).
I want to create a form in which an user can choose a product by first selecting the category. A user selects the category by clicking on image, then I want to set the image's value into a hidden input, but I don't see how can I change a foreign entity choice list to a hidden input (http://symfony.com/doc/current/reference/forms/types/entity.html).
How can I accomplish this? (how to change form input to hidden)
If I set cascade validation to true, will it for example check if a category really exist. (To prevent putting products with non-existing category from malicious users) ?
Part 1
To do this you need to use a data transformer to do two things:
transform an entity into an identifier that is either a string or integer so a form can render it as a hidden field.
transform the string or integer identifier into the entity when the form is submitted so that the parent entity can be saved with the correct relationship
The symfony docs I linked to above (here too) actually walk though an entire example of using a data transformer with a form.
As a shameless plug (because I believe it is helpful) I have written a little tutorial on using a data transformer for a hidden field with an entity id: http://lrotherfield.com/blog/symfony2-forms-entity-as-hidden-field/
Part 2
If you are using the data transformer then you don't need to worry about malicious users. The data transformer will fail because it will not be able to reverse transform the category from the fake id. In my tutorial the transformer will throw a Symfony\Component\Form\Exception\TransformationFailedException exception.
You can also write a validator (potentially using a call back) if you wanted that checks that the submitted category is real if you want an error to show in the form. Doctrine wont allow you to persist a fake category relationship as the foreign key constraint will fail.

web2py Validation

I would like to know validation is a must, on fields which are not present on the form but are available in the table. Does marking them as NULL in the define_table make them validated only when they are present in the form?
The form validators apply only to forms, so will not affect fields that are not present in the form. I'm not sure what you mean by marking a field as NULL, but if you are referring to Field(..., notnull=True), that executes the SQL NOT NULL statement when the database table is first created (assuming DAL migrations are enabled). That option is enforced by the database itself whenever a record is inserted or updated (via a form or any other method). If a notnull field is left empty, it will result in an operational error from the database.

symfony2: how to validate a property which is not mapped to the entity ('property_path' => false)

I'm having a registration form, which contains input fields, almost every field mapped to a property of the User class. But there are some fields which are not mapped. Validation is done via annotations in the User class.
My question is: how do I validate those fields which are not mapped?