cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[NFR] Implement method \Phalcon\Forms\Form::__toString()

Open lukaszkujawa opened this issue 13 years ago • 12 comments

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.

lukaszkujawa avatar May 08 '13 15:05 lukaszkujawa

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.

roman-kulish avatar May 10 '13 01:05 roman-kulish

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.

lukaszkujawa avatar May 12 '13 12:05 lukaszkujawa

:+1:

iforp avatar May 13 '13 08:05 iforp

:-1:

Solution with decorators forms in ZF1 was unsuccessful from it and later abandoned in favor of view helpers.

nikolasha avatar May 14 '13 14:05 nikolasha

-1. I also don't like solution with decorators

igorgolovanov avatar May 21 '13 11:05 igorgolovanov

@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 avatar May 21 '13 11:05 roman-kulish

@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.

lukaszkujawa avatar May 21 '13 13:05 lukaszkujawa

Again, this is stuff which can easily be implemented by individual users, no need for it to be part of a (slim & fast) framework.

philippgerard avatar Jun 21 '13 14:06 philippgerard

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

iforp avatar Jun 21 '13 18:06 iforp

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.

pgasiorowski avatar Jan 12 '14 21:01 pgasiorowski

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

nexik avatar Jan 12 '14 22:01 nexik

I'm closing this issue due to the lack of any reaction. I'll open it again if the need arises

sergeyklay avatar Jun 05 '17 19:06 sergeyklay