Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony form generation - why?

I've been pondering this for awhile now - I never fully grasped why you'd want to generate all your forms programmatically, unless they were going to be fully dynamic. But from my experience, most are static.

Now, getting to the main question at hand - with Symfony, it generates all your forms for you based on the table that you associate a module to (when building it). My question is, why would you want these forms generated? For the most part these are static forms, which should be easy to edit in the template.

The main issue for me, is if you have a team of back-end and front-end developers, and maybe some designers, for example. And the designers or front-end developers (who may or may not have much or any PHP experience) want to change the form around (for aesthetic purposes) in the template directory, which houses all the views. Well, they can't really, because it's all generated by a form class which was built specifically for that form. So, now they need to go back to the back-end developers and ask them to change things for them?

I may be missing the point with the form generation, but the way I see it - if it's static, there is no need to generate it programmatically, but if it's totally dynamic, then yes, it's alright.

Any views on this?

like image 757
xil3 Avatar asked Feb 03 '11 09:02

xil3


1 Answers

Because for the most part, the field types used are pretty similar to the schema columns, assuming you designed your schema correct in the first place ;-) eg varchar(200) translates to a single-line text input, a MySQL longtext translates to a textarea and so on. Quick and simple to then move to rendering, after maybe a couple of tweaks. The form classes generated also give you a place to pop all your form widget validators for form validation.

You can of course use the validator classes wherever you want, but the forms framework encapsulates this nicely I feel.

Rendering the form though is a separate issue - as you mentioned, you have a view for this. The lazy approach in Symfony is to simply do <?php echo $form; ?> but it's preferable to render the individual fields themselves eg: <?php echo $form["fieldname"]->render(); ?> and so on. If your views are structured like this, then your front-end developers should be able to re-order the fields as they want.

Edit: Adding classes and other attributes during rendering:

<?php
  echo $form["fieldname"]->render(
    array("class" => "myNewClass", "title" => "My Title", ...)
  );
?>

Edit 2 Essentially what I'm trying to convey is that you will always be able to find a corner case for where the forms framework in Symfony isn't appropriate. You'll be able to find that for probably every component in every framework ;-) But for the majority of the time, it does the job fine and with relative ease. If you find yourself continually fighting against it, then I would suggest that either you're coming at it from the wrong angle or you've just found all those corner cases mentioned above :-)

like image 180
richsage Avatar answered Sep 20 '22 10:09

richsage