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

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

Related

Get request data (GET) in a Symfony (5) formType without using data_class

I'm building a formType to filter products on a collection page. You can set multiple select boxes which makes other auto filled or unnecessary. I want to be able to manipulate the formType based on the data like when using a data_class object. I'm not using data_class because the search isn't a persisted object which is saved to the database. I'm using a GET form.
For example 2 select boxes:
category
productType
When setting a category makes some of the productTypes unnecessary. So i want to not show it.
To do so in the formType I need the data of the request (GET) but I can't find a way to do so.
To retrieve data from the form, you can use $form->getData().
As you're in a GET context, I suspect you can take advantage from FormEvents (take a closer look to POST_SET_DATA event) and get rid of values you don't need.
One other thing I would like to point out, is that you still can use some kind of object that's not persisted to DB, like DTO or whatever.
Forms and entities are not related anyhow, neither in the usage nor in the intentions.

Symfony2 Mapping Model to Entity

I am currently trying to sort out my user registration in Smyfony2, loosely following their documentation here:
http://symfony.com/doc/current/cookbook/doctrine/registration_form.html
Sadly, their example is a bit simplistic.
My User Entity has a number of fields which only get added to during registration, and can't be changed there after, so I separated those out from my UserType into my RegistrationType.
The problem now, is that Symfony can't find any of the fields, requested for the form, which live within the User Entity, because it is looking for them in the Registration model. How do I get the Registration model to point to User Entity?
In the documenation example, they avoid all this as the "terms and conditions" checkbox doesn't get added to the database.
e.g. they use this:
$builder->add('user', new UserType());
but as I mentioned, that only has the fields I want the user to edit after registration.
I tried the data_class, but it complained about Form\Model\Registration wasn't of type Entity\User.
These seems like a really common issue when you are trying to embed bits of forms for a single entity, yet it doesn't cover it in the documentation.
And no, I don't want to use FoSUserBundle.
Actually, it's possible and really easy to have several form types for the same model class. You can have the RegistrationType with lots of fields and then the UserType with only some of those fields. Both use the same User model.

Dynamic validation using jQuery

I need your help. I need to create a validation framework for complex form which will be generated dynamically.
For instance, user selects some Template he wants to change, then he receives a list of fields to be filled with data. Number of fields can reach up to 150.
There will be few types of validation rules
Regular expresions
Is field mandatory
data type - string, numeric or alphanumeric (can be validated with RegExp)
All the rules for each field will be stored in the database.
The complexity is that each field may have it's own regular expression, therefore validation error messages are also different. I have attached image describing how the control will look:
I'm thinking on using JQuery framework and assign each control to be validated using the attribute, but not sure if this task could be done using the JQuery.
Will appreciate any advices on this!
Thank you very much!

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

MVC 3 validate dynamic form fields. ASP.NET

I'm working on a very dynamic site build at the moment. What I'm trying to do is creating something like a survey that can be created dynamically from a control panel.
In the control panel you add input fields (these are saved in a database), what the user then see is a form that I generate from the database. So if I add 3 input fields to the database the survey will contain 3 fields. If I add 20 fields the survey will have 20 fields.
Now my problem is that I want to validate these fields, and I would like to be able to hook me in with the standard validation flow. I can't create a Model with validation rules since the number of fields and their names are dynamic the only thing I know is what kind of data that is expected in every field (this rule is found in the database).
In an ordinary case I would get the automatic highlighted fields that are not valid and so on thanks to the built in validation flow with ValidationResult and so on.
So the question now is can I somehow simulate parts of the validation and then hook me back in with the validation result, and if not valid, the form prints the error messages and fill the fields with the data that was given?
Regards
Tobias
What I would do is create some kind of expando model, my own ModelMetadataProvider and might also need my own ModelValidator for that model.
Then, you can easily create validation using the Html.EditorFor and other Html helpers, as they use the metadata to create validation.
BTW, you might also need to create a model binder :)
meta data:
http://mgolchin.net/posts/21/dive-deep-into-mvc-modelmetadata-and-modelmetadataprovider
http://weblogs.asp.net/seanmcalinden/archive/2010/06/11/custom-asp-net-mvc-2-modelmetadataprovider-for-using-custom-view-model-attributes.aspx
http://bradwilson.typepad.com/blog/2010/01/why-you-dont-need-modelmetadataattributes.html
validator:
http://dotnetslackers.com/articles/aspnet/Customizing-ASP-NET-MVC-2-Metadata-and-Validation.aspx#s2-validation
http://dotnetslackers.com/articles/aspnet/Experience-ASP-NET-MVC-3-Beta-the-New-Dependency-Injection-Support-Part2.aspx#s10-new-support-for-validator-provider
model binder:
http://www.singingeels.com/Articles/Model_Binders_in_ASPNET_MVC.aspx
This might bo overkill... But these are the extensibility points that you can use.

Resources