form decorators in zend framework - zend-framework

I am new to zend framework. I have used zend form and decorators to make form using class and decorators. The form i created was simple register form.
Now my question is "Is it advisable to use zend form for many complex forms as well ?" i have made many complex forms full of jquery with lots of conditions.. so at this moment i feel that it will very much difficult to make such form using decorators.
The power of form is zend filters and zend validartios which saves our hell lot of time...
So can we make forms in our phtml files and still use the power of filters and validators or is dere any other way...???

Well, my opinion would be to try to make the forms using Zend Form. The reason is that time spent on making Zend_Forms, setting up decorators, customizing elements or creating your own elements will be saved once the form is in use. With Zend Form you get very easy and straightforward way of validating your form, filtering contents of your fields, managing error messages, translating it, etc. In addition, once you spent time on e.g. writing your own or customizing existing decorators, form view helpers, form elements, it will be quite easy just to take them and use them in your next ZF project.
So in my opinion, in the long term using Zend_Form will save you time, even if at the beginning it may seem that using Zend_Form causes more troubles and headaches than not using them.
But off course, if you want to make your forms directly in phtml there is nothing that stops you. Both Zend filters and validators can be used on their own, independently of Zend_Form. You could also create zend form that has the same elements and structure as your "phtml form". This way you could only populate the zend_form, and use it only for validation and filtering of submmited data, not for actual rendering of your form.

Related

Is there / What is a suggested way to create autocompleted form fields in ZF2?

Does the ZF2 Form (zendframework/zend-form) provide a mechanism to create text fields with autocompleted value list? Or is it just done by using the common Zend\Form\Element\Text, a Controller, that provides the data, and some JavaScript (not by ZF generated, but implemented in any proper way)?
You can use jQuery Autocomplete field with Any Framework or CMS, You can grab it here. https://jqueryui.com/autocomplete/
Its quite simple and easy to use.
You can fill source array on document.ready() or on field.change() or on field.keypress() events as per your requirements using Ajax.
Or you can read similar Resolved issue here
JQuery UI Autocomplete with Zend Framework
Zend Form doesn't have such mechanism, so proper way is use AJAX request with JSON response. See Rob Allen's tutorial for this:
Returning JSON from a ZF2 controller action

Zend Forms or simple html forms

It is mandatory to use a Zend Form everytime I need one? Can I do it with a simple html form in my phtml?
Are there dangers or trouble using the html one?
You can still use pure html forms wherever you want.
Zend_Forms try to help to easily implement the best pratices particularly validating, filtering and escaping user input. If you use pure HTML, you may forget these best pratices.

why use zend form decorators instead of individually rendering?

I am almost at the end of my rope trying to style my Zend form using decorators. Previously to avoid these issues I would create a form script extending zend_form, add whatever validators, labels, etc I needed then retrieved the element from my view script using $form->getElement('my_form_element');
that way I could wrap whatever css tags I wanted around the element. I thought I should learn how to use the built in decorators, but I'm starting to feel like it's a waste of my time. My old way seems easier, is there some reason I am not seeing that makes using custom decorators better?
In general decorators are used to dynamically add functionality without having to touch the code's core functionality and for a better re-usage of code. In Zend_Form however, I think that the decorator system (as well as Zend_Form in general) is unintuitive and heavily over-engineered and so it does exactly the opposite of what it should do: Help the developer to create better and more intuitive code faster.
In my opinion the usage of Zend_Form_Decorator makes only sense in the case where you have some extended logic that you plan to reuse throughout your project on multiple and different types of elements.
Let me give you two examples:
You want to add a tooltip icon next to an arbitrary form element with a nice icon and a fancy JavaScript hover box.
An element should be validated directly upon entering data by posting an AJAX request and adding either a green check mark on success or a red cross icon on failure next to the element.
With the decorator you can now separate the logic of the added functionality from that of the underlying element and so you can use the same code to add the tooltip and/or the live validation feature to a textbox element as well as any other element simply by adding all the decorators you want to the element.
There is also a great article from Matthew Weier O'Phinney, the lead developer of Zend Framwork, that gives some background insight on the motivations for using decorators along with a lot of examples: Decorators with Zend_Form
Decorators can come in handy if you have a specific repeatable markup for your forms, for example if you want to use Bootstrap. Other than that, especially if you have a good html/css person, I would avoid them.

Should I generate html form?

I'm building a php application that will have many modules/plugins. The issue is that creating smarty template for form's of each plugin and writing validation code for each looks redundant and at the same time using PHP to generate form will restrict the flexibility in controlling each form's layout structure the way I would have wanted.
Using PHP classes and inheritance you can write the validation and layout code once and overriding them whenever you need specific behaviour.

Why do we use HTML helper in ASP.NET MVC?

Are there any good thing, best practice or profit we have after using the HTML helper in an ASP.NET MVC project?
When I am trying to use them I found that I lose the speed I have with HTML and many difficulties I have whenever I use an HTML helper.
Other [non-techie] persons can't understand what I write using Helper if I want to show them or they want to do something they need to spent more time on, even if they have working knowledge of HTML.
If I use an HTML helper I lose the speed. When I use HTML I just type and of course I am not aware of it. But using helper, it is hard to understand.
What thing do we get when I use HTML helper? I think it is nothing I get because I lose the speeed. Others can't understand what I do using helper and can't customize the code if they want.
Why do we use HTML helpers?
You use HTML helpers to encapsulate some small HTML fragments which are repeated all over your pages. And to avoid writing those HTML snippets all over again you use helpers.
They are very useful, especially when dealing with things like URLs because instead of hardcoding your links helpers take advantage of routing the definition on your server and by simply changing those routes the whole site URLs' change without ever touching any single HTML page.
Another scenario where HTML helpers are useful is for generating form input fields. In this case they automatically could handle values when posting back and show associated validation messages. Can you imagine the spaghetti code you would have to write in your views if there weren't HTML helpers?
The biggest advantage I find is with the editor and display templates.
If your editor for a field is more than just a simple input box, you can put that into a template and replace the several tags with a call to
<%:Html.EditorFor(m=>m.Property)%>
This means that your page is a lot easier to edit as you aren't wading through a lot of fluff HTML to find what you want.