Symfony4 - Validate fields based on values of other fields - forms

I am building a very large form with multisteps and some conditional fields and cannot find a good solution for my problem.
For example if field A is "Foo" then field B is shown via Javascript and must be validated via Symfony, otherwise the field must be empty. Is there a Way to do this?
PS:
Is https://github.com/craue/CraueFormFlowBundle a good way to go for complex multistep forms and should I use groups and entities or build it without a class? Whats the best practice?
Thanks!

My solution:
I think you should use Symfony Form Events. In events you can do such custom stuff like adding errors, check values in all form, remove and add validators/fields dynamically.
More about form events flow you can read here
Here Add error to Symfony 2 form element you have question howto add error to field from event listener(In symfony 4 it is done similarly)
My opinion about craue/CraueFormFlowBundle:
I use CraueFormFlowBundle in my projects to create complicated multistep forms. It's useful Bundle and saves a lot of time. I created a lot of forms based on CraueFormFlowBundle + Doctrine entities in combination with validation groups + symfony form events - powerful and flexible solution which I can recommend

Related

Is the symfony2 entity field a good solution for many related objects?

Is it an nice solution to use the symphony entity form type for relations (for example tags - manytomany).
I think of a big collection of tag - objects! Is it not to much overhead to load all the tags of the database for a choice - list?
And if this is true, how could I solve this issue better?
Greetings Michael!
You should not care about micro optimizations in this state. But of course, you should know Doctrine. If you have an entitity with a one to many or many to many relation and you know, you need it the releations, e.g. your tags as getTags, later in your application, you should do a join in the query builder. Otherwise the relation is lazy loaded on the first call to getTags
tags - manytomany is a normal solution, but of course it is an overhead to load all the tags of the database for a choicelist (if there is a large collection), you don't need to load all of them at once, use some jquery plugin to handle it instead (may be ajaxbased Select2 plugin...)

Angular form data / model data duplication

Starting out with angular, Ive developed an app which builds a form with directives dynamically chosen from the field type in the data from the server. All the fields are bound to data within $scope using ng-model. The data is loaded and saved to the server using $http.
Today I've looked at Angular forms, and by wrapping all this in a form element, and giving names to each input, I automatically have validation etc, which is very nice.
But now I notice there are two sets of data maintained - my bound data from my model in $scope, and the data separately maintained by the Angular form directive.
Looking at the Angular docs for eg form or input, the example also binds to $scope (ng-model="text"), but form data is separate (myForm.input.*).
Is this apparent duplication best practice? Or is there a way to unify my model with the data maintained by the Angular form, under one structure?
The form and ng-model (using ngmodelcontroller) directives provide a mechanism to support form validation and 2 way data binding on input elements.
What the form provides as data is current state of the form and its element, which beyond the standard model that you bind using ng-model. Duplication of code is bad, but here data in the form is being tracked by the form directive so it is fine.

Symfony2: Best practise for large forms

I going to set up some large forms with up to 100 fields.
Now my question is if there is some best practise about handling such forms.
Especially:
Entity relations
Form object it self
Does somebody have experience about such tasks?
Should I put all together in one entity, one form, structured with jQuery in subforms or should I group the attributes to arrays as much as possible?
Regards,
Bodo
Whereas official documentation show many examples of forms directly linked to entities, I think this is not the best approach. In rare case it's good, when your form has exactly the same fields as your entity.
Your form must represent what you get in the browser, so only html inputs/textearea/select. I usually create a formData class, like an entity, that holds validation constraints and has the same structure as the form.
You have to initialize this formData object with your persisted datas before loading the form, and after submission, you update your entities with your formData object...
I am creating some complex forms that are built from multiple entities. I create a form class for each entity and then create a composite form class that holds the various combinations of forms I need (which is in someways similar to PéCé's suggestion).
If I don't need all of the fields, that is fine because I can control which fields are rendered in the twig template. If I need fields that aren't in the form class, I can add them to the template and process the form data as appropriate. For all of this effort I get some built in validation and flexibility to reuse forms from multiple entities and bundles.

Saving complex form data in zend framework using doctrine 2

Doctrine 2 integration into ZF seems to make simple things very hard and time consuming(At least for me).
I cant just give a submitted form array to doctrine to automatically map key/value pairs to doctrine entities and it gets very complicated if i have a many-to-many entity and submitted form has nested array.
In symfony, submitted form keys/values are easily and AUTOMATICALLY mapped and saved to doctrine tables. I don't know how to do that in ZF especially if I have "Many to Many" and/or "Many to One" doctrine entities and I have **nested form elements which need multi-level iteration.
I don't want to Set every entity explicitly and create every entity object manually**.
Pain would be alot less if i used ZF`s native database architecture.
I have done some coding and now its done half-automatically but is not very useful.
I think the best solution is to use PHP's Reflection API to inject/retrieve values from your entities (using smart get/set detection as well). I started a little library called ObjectSerializer to help with the process but never finished. That said, if you look at the logic contained in these two classes you might get a good idea where to start.
You should program this sort of logic into your models.
I usually add this sort of logic into my form, for example
class Application_Form_SomeModel extends Zend_Form
{
// the usual stuff
public function populateModel(\Entity\Model $model)
{
$model->setSomething($this->getValue('something'));
$subForm = $this->getSubForm('name');
$relatedModel = new \Entity\RelatedModel;
$relatedModel->setSomething($subForm->getValue('something'));
$model->getRelatedModels()->add($relatedModel);
// assuming the related model collection has cascade persist
// otherwise you'll need to pass in the entity manager to persist
// the new model
}
}
I do it this way due to the fact the form's elements are really only known to the form. This allows you to encapsulate everything to do with the form and its element names within the form without having to assume anything else knows this information.

Automatically Populate Objects from HTTPRequest

I have started developing my own web framework which has been coming along quite nicely. It simplifies and cators for all my specific needs. I have tried many frameworks like struts, struts 2, tapestry, spring and the list goes on. There are 2 big problems I find in all of these frameworks.
The ability to work with specific business logic/rules.
I am a bit of a perfectionist and the entire layout of these frameworks force you to scatter you validation, DAO logic and your form actions. I like to keep things grouped together and not do validation in my Hibernate Objects, Action classes and a separate XML files which causes a load unnecessary processing.
Ok back to my question. Does any one know of possible solutions to populating Objects from a HTTPRequest? I started looking into introspection and reflection, but I would like to see if there might be a better solution for something like this. Example: Object Student has 3 fields eg String Name, Integer age and Calendar DOB. What are the options in populating this object or any other Object from a HTTPrequest?
You could use a similar system to the way objects are sent in json by adding a __type__field that gets sent with each object. That way you know which object to create and introspect.