Get all fields names from a Symfony's Form - php

For a project I need to check if all form's fields are present in a PUT request.
Simple data validation with the NotNull / NotBlank constraints is not appropriate because the fields in the request can be set with NULL or blank values but they have to be present.
My idea is to take all names from a Form's field and check if those fields are present in the request array.
To do the trick I need to get names of those fields, there's an array in the Form class named orderedKeys which contains exactly what I want, but the variable is set to private.
Is there any other way to get access to those keys ?

You can get all the child forms of a form by doing
$form->all();
Then you can recover the name of each field by doing
$child->getName();

Related

In Yii2 dirtyAttributes() get only string fields? Need to take number fields and relation fields

I get dirty attributes in after save method. but it only gives fields which are in string type. Not gets fields are in Number type and foreign key fields.

Symfony 3 - Form builder: Storing select field in database with doctrine

I'm new to Symfony and I would like your help in deciding the best way to design a form and store into database using Doctrine. Here is my task:
I have a form created with the FormBuilder containing various types of input fields. I have created entity which connects my database to form and it works both ways, both to fetch data and to put data into database.
However, I want to add a Select multiple field to a form which would be populated from a table from the database. And when I select the choices from that select element, I would like to save them into a database in either csv of id's or some simple serialized object or array.
So far, what I have managed to do is to add that Select field as EntityType and have placed ORM Annotation in entity for that element: Type("object"), which populates the Select element flawlessly. From the $_REQUEST variable I can see that it returns the id(s) of selected options. But, what I get when I submit the form is a very large serialized object in the field which is supposed to store selected element.
I suppose that it is a normal behavior (to store the entire object (an entire row of data)), but is there some cleaner, leaner way of storing only the id's that I would be able to later on use to populate the same select field, also using doctrine (since I don't have a need for other data in that field except for their IDs)?
Thank you in advance.

Two fields mapping in symfony2

Is it possible to collect and map two fields from form to one property?
Details:
I have an array field in my entity:
#ORM\Column(name="custom", type="simple_array")
where admin can specify (multi choice) custom options for the product - for example:
$product->setCustom( array('customText', 'customNumber') );
So the user should have two fields in his order form - text and number. Then I want to save them:
$order->setCustomOptions(array(
'customText' => 'Foo',
'customNumber' => '100',
));
In order entity there is just array field type instead of simple_array.
User can't add new options so the collection field type isn't a good choice in my opinion.
I have also tried to use data transformer but I think it can be applied only to one field.
Tell me if it's not clear. I don't need a complete solution but a hint what to choose.
Finally, I created two additional fields in my Entity (like DonCallisto suggested) without mapping to Database and rendered them in the form. Then instead of using DataTransforem, I used a setter with #ORM\PrePersist and #ORM\PreUpdate to set custom values together.

Symfony Set Drop Down Value Based Upon ID

I am still getting my hands dirty with Symfony, so I am a bit ignorant on how to do something that should be pretty simple.
I have a form that I am creating in the controller and passing to the view:
$form = $this->createForm(new PurchaseOrderType($account), $purchaseOrder);
The form displays exactly how I need it to, no problems at all! I am trying to now make the form more dynamic so that it can auto select a drop down list based upon an "id" variable that I am passing into the form. The id equals 23 by the way.
So, I have a drop down of suppliers and one of the options value is 23. How do I automatically select this option? Sorry for my ignorance :)
Thanks!
Without the code of your form type, I can make only suggestion.
If I get it correctly, inside the purchase order entity, there is an other entity mapped, and that one is represented with an id.
The object, $purchaseOrder has to have the other entity, then in the form type, when you set up the drop down field, you have to specify - with the correct name it shouldn't be a problem - the foreign id.
But you need of course data for the drop down field what can be the result of a SELECT * query.

Embedding a collection of forms Symfony2 forms with adding and deleting allowed

In Symfony2, if I embed a collection of forms pointing at a many to one relationship in Doctrine and allow adding and deletion, if I delete a record from the beginning, add one at the end, and edit some in the middle how does the system know which records to update with which data?
There is nothing in the tutorial that passes the primary key of the embedded data around. Under certain circumstances, my records are getting needlessly deleted and added again rather than edited in place (even if there are no changes to the particular record). This breaks the fields on the records that are not included on the form, setting them to their default values from the DB model.
Is there a way to pass the primary key in the form and have it used to perform updates when the data comes back?
If you want to index the collection (by the entity id) for all querys, you can simply use the indexBy annotation in your entity class.
/**
* #ORM\OneToMany(targetEntity="EntityClass", mappedBy="EntityVariable", indexBy="id")
*/
private $collection;
Based on the Akkumulator's answer and comment and some experimentation, I did this:
Create new fields (using Javascript as described in the documentation) with __name__ replaced not by a number but by a string: new_ followed by an forever increasing number that has nothing to do with the list (e.g. new_1, new_2, new_3...)
I don't have to push the primary keys into the forms and I don't need indexBy either - that's good, because indexBy felt like it was too far removed from the form, ending in having the Action at a distance anti-pattern.
Why this works:
PHP arrays aren't like those in other languages. They're always dictionaries, so you can add string keys to them even if they only have numeric keys to start with.
Because the Symfony collection is mapped by field name, new fields will not match existing data and deleted fields will not be matched to existing data (and thus be removed from the set)
One way to pass primary id is to use INDEX BY.
For example, say I have an entity called Customer and a Customer has several Emails. In my Customer repository class, I can specify my collection to be indexed by Email's primary id.
$qb->select('c, e')
->leftJoin('c.emails', 'e', null, null, 'e.id')
->where('c.id = :id');
By doing so, the generated name of the input tag would be
customer[emails][e.id][fieldName]
Upon submitting the form, Symfony will bind the request values according to the input names.

Categories