[NFR] Implement method \Phalcon\Forms\Form::__toString()
It doesn't look like there is an easy way to render an instance of \Phalcon\Forms\Form. From this what I can see in the documentation (http://docs.phalconphp.com/en/latest/reference/forms.html) one has to iterate though all elements. Additionally it's required to add the form tag and handle errors.
Forms and highly reusable elements on a website so perhaps it would be worth to wrap all that functionality into one method.
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
Can you provide a sample output form component can generate? I am sure there is no way to render a form in HTML which satisfies everyone: table? floating boxes? what if I want to use Bootstrap or Zurb Foundation? Form component should know about presentation, because it is varies and so it should not render it. It is a View function.
Default output could be based on list and labels but as you said it won't satisfy everybody. The common solution to this problem is using decorators. When every element has own decorator one can enforce any behaviour. It should be also possible to chain decorators so for example a text input can have:
- FormElement - wraps everything in
<li></li> - LabelDecorator - prepends
<label>Element Label</label> - ErrorDecorator - appends an error message if there is any
- TextInputDecorator - render
<input type="text" name="" value="">
Each of the decorators should have a generic configuration which allows to define a tag and attributes. That can be even more granular but I suggest to no overcomplicate this already complicated idea. This is how ZF1 solved this problem. I'm not suggesting to copy-cat it because there might be a better solution.
The benefit for a programmer is you don't have to repeat a code for building forms. When your form structure change it's easy to implement the change because everything is abstracted.
:+1:
:-1:
Solution with decorators forms in ZF1 was unsuccessful from it and later abandoned in favor of view helpers.
-1. I also don't like solution with decorators
@lukaszkujawa so think about somebody using Twitter bootstrap and trying to render a complex form that uses decorators for elements but also additional markup to organize inputs into control groups. How would that person do this?
@roman-kulish I've done that few times (including twitter bootstrap). Once everything is setup then you don't have to think about it any more. I admit that the integration process is not the easiest one.
I don't say decorators are the way to go. This is the first thing which came to my mind as I'm using ZF1. Perhaps there are better patterns then decorator and that's fine so long there is some abstraction. Imagine a big system with lots of forms and the Twitter bootstrap. After a while you realise you have to change some tags. It might be a different error handling, adding a help button next to every input element or changing frontend framework. You will end up changing every single template with the current approach.
Again, this is stuff which can easily be implemented by individual users, no need for it to be part of a (slim & fast) framework.
I have to agree with the guys voting against. __toString() method should be implemented in the custom class. Сan I pick up my vote back? =) -1
I would also vote against. Its easy enough to implement this with an abstract form or, my personal preference, traits. Maybe interested people could contribute different examples of traits to incubator so they might or might not be picked up by Phalcon once support for php 5.3 is dropped one day. Just an idea, but having hardcoded implementation of __toString() isn`t a good idea.
Yes, I also tried to write decorators for forms but I ended with simple views
In Volt I just use
{% include 'partials/form' with ['form': form] %}
I can add my bootstrap form templates to incubator but I don't know if this kind of extension is out of scope for Phalcon Incubator
I'm closing this issue due to the lack of any reaction. I'll open it again if the need arises