Thứ Hai, 8 tháng 7, 2013

ZEND FORM in ZEND 2

http://www.michaelgallego.fr/blog/2012/11/09/discover-whats-coming-for-zendform-in-zf-2-1/

Note : If DateSelect is not working, you can use Datetimepicker
http://tarruda.github.io/bootstrap-datetimepicker/





Discover what’s coming for Zend\Form in ZF 2.1


As you may know, the very first minor release (ZF 2.1) of Zend Framework 2 is expected to show up at the end of the month. Following the policy of Zend Framework 2, no BC (breaking-compatibility) is expected in 2.1, so you can (okey… you might, I don’t want any troubles  ) safely migrate to 2.1, and just enjoy the new features.
I’m going to talk here about the main new features of Zend\Form, hope you’ll like it !

New Elements

ZF 2.1 comes with two new elements I’ve written, namely DateSelect and MonthSelect. Instead of the traditional Date or Month element (that are rendered as text element or with a date picker in some browsers like Opera), DateSelect and MonthSelect are rendered using three selects (or two for MonthSelect). In some cases (birthdate…) this is by far more convenient for the user that a date picker.
To create such element, this is just deadly simple :
Those elements accept a lot of new options to finely tune it :
As you can see, this element accepts different options :
  • create_empty_option: if set to true, it will automatically creates an option whose value is empty for all three selects (this is useful for many JavaScript select libraries that ask you an empty option to allow a placeholder).
  • min_year and max_year : the min and max year for the… year select options (captain obvious).
  • day_attributes, month_attributes, year_attributes : those allow to set attributes that will be applied to the selects.
Of course, the MonthSelect works exactly the same way, excepts it does not have any day select.
You might wonder about validation and localization ? Just don’t ! What’s cool is that it automatically validate for you the data. About localization, it uses Intl data, so that the order of the “day-month-year”, as well as the separator (“day-month-year”, “day/month/year”) and even the months names are automatically localized using the current Locale.
Please note too that if you call getValue on such an element, it will returns the date using the following format : ‘Y-m-d’. It is up to you to convert it to DateTime object if you want.

FormElementManager

This is the next big thing. The Form now follows other components and adopt a PluginHelperManager, which allow… well, a lot of things.

Short names

First of all, you won’t need to write the whole type of the element now. This means that this :
can simply be rewritten too :
This works because form elements are now pulled from the Service Manager. This means that you can also override the standard Url element and provide your own.

Dependencies

Pulling objects from ServiceManager also means that dependencies are a lot easier to handle.
Let’s say that your Fieldset needs the ServiceManager. Before, you had to transfer it from Form to Fieldset (and if you use, like me, very deep structure, you may need to transfer it to many many fieldsets… although only the last one may use it).
Now, just make your Fieldset implements the ServiceLocatorAwareInterface :
And add the fieldset to your form :
Now, instead of directly instantiates the fieldset using the name, it will try to pull it from the ServiceManager first. You can also define the dependencies using the new getFormElementConfig function in your Module.php class :
Of course, for this to work, you have to instantiate your form using the service manager too. So in your controller, instead of doing that :
Write this :
We first get the FormElementManager class (that is a Plugin Manager class), and then get the form. Please note that you don’t need to declare “MyForm” as an invokables, as unknown strings will be considered by default as invokables.
Also, as most of the dependencies are set through initializers (for example ServiceLocatorAwareInterface), this means that adding elements in constructor is too soon, as the dependencies through initializers are not set yet. This is why we added an “init” function that is called after all the dependencies have been set.
So in your fieldset, instead of adding elements in __construct, just add them in “init” (yeah, like the old ZF1 style !).

Completely new File upload

Last but not least, thanks to cgmartin, the File upload has been completely updated. This is something we did not had time to update for ZF 2.0, and hence, updating file was a mess as it did not work at all with the new architecture of forms.
This is completely solved, and Chris has done a remarkable job for it ! I suggest you to have a look at the examples : https://github.com/cgmartin/ZF2FileUploadExamples
I hope you’ll like ZF 2 forms even more ! Enjoy !

Không có nhận xét nào:

Đăng nhận xét

Học lập trình web căn bản với PHP

Bài 1: Các kiến thức căn bản Part 1:  https://jimmyvan88.blogspot.com/2012/05/can-ban-lap-trinh-web-voi-php-bai-1-cac.html Part 2:  https://...