📜 ⬆️ ⬇️

Zend_Form and ini files

Zend Framework is sucked from many sides and has quite good documentation. But working with forms through Zend_Form has its pitfalls, due to the mass of possibilities, which even in the official documentation are not covered well enough.

I want to talk about setting up forms through ini-files. This will be useful for programmers and layout designers.

I believe that you already know approximately what Zend Framework is and where its documentation is located. You may even use Zend_Form in your projects.

If not, here are the basic concepts:
To verify the forms, you will need two files, form.php and form.ini . The first one is here, and I'll tell you about the second one.
<?php

require_once 'Zend/Loader.php' ;
Zend_Loader :: registerAutoload ();

$view = new Zend_View ();
$view -> doctype ( Zend_View_Helper_Doctype :: XHTML1_STRICT );

$conf = new Zend_Config_Ini ( 'form.ini' );

$form = new Zend_Form ( $conf );
$form -> setView ( $view );

echo
$form ;


Ini-file structure


The file contains the attributes of the <form> tag, global values, elements, default values ​​for elements, etc.
')

Attributes


All unrecognized keys are interpreted as attributes of the <form> tag. But they can be specified and explicitly through attrs.* .
;;;;
;;;; form.ini
;;;;


action = /some/url

; post | get | put | delete
method = post

; "application/x-www-form-urlencoded" | "multipart/form-data"
enctype = "custom-enctype"

; Description
description = "form description"

; Fieldset
legend = "form legend"

; <form name="">
name = "attr name"

; int - SubForm ( )
order = 1

; true | false - -
; ini- (. decorators.*)

disableLoadDefaultDecorators = false

; true | false -
disableTranslator = false

; attribs.* - <form>
attribs.action = /yet/another/url ; action
attribs.my_pretty_cool_attr = value ; <form my_pretty_cool_attr="value"/>

; defaults.* - -.
defaults.myfield = "Default value for my field"
defaults.anotherfield = "Pretty nice value for another field""

Class paths


; prefixPath -
; :

prefixPath.prefix = Zend_Form_
prefixPath.path = Zend/Form/

; Zend_Form_Decorator_MyDecorator
; Zend/Form/Decorator/MyDecorator.php
; Zend_Form_Element_MyElement Zend/Form/Element/MyElement.php
; , , :

prefixPath.zend.element.prefix = Zend_Form_Element_
prefixPath.zend.element.path = Zend/Form/Element/
prefixPath.zend.decorator.prefix = Zend_Form_Decorator_
prefixPath.zend.decorator.path = Zend/Form/Decorator/
prefixPath.custom.prefix = MyProject_Form_
prefixPath.custom.path = MyProject/Form/

; elementPrefixPath - , elements.*
; ,
; , prefixPath.*

elementPrefixPath.decorator.prefix = Zend_Form_Decorator_
elementPrefixPath.decorator.path = Zend/Form/Decorator/
elementPrefixPath.filter.prefix = Zend_Filter_
elementPrefixPath.filter.path = Zend/Filter/
elementPrefixPath.validate.prefix = Zend_Validate_
elementPrefixPath.validate.path = Zend/Validate/

; displayGroupPrefixPath - prefixPath.*

; defaultDisplayGroupClass - .
; - Zend_Form_DisplayGroup

defaultDisplayGroupClass = Zend_Form_DisplayGroup

Decorators


; decorators.* - .
; -.
; : FormElements, !
; -:

decorators.elements = FormElements
decorators.tag = HtmlTag
decorators.tag.options.tag = dl
decorators.tag.options.class = zend_form
decorators.form = Form

; displayGroupDecorators.* -
;
; displayGroups.groupName.options.decorators.*
; displayGroupDecorators.*, :

displayGroupDecorators.el = FormElements
displayGroupDecorators.tg = HtmlTag
displayGroupDecorators.tg.options.tag = dl
displayGroupDecorators.fs = Fieldset
displayGroupDecorators.dt = DtDdWrapper

; elementDecorators.* -
; elements.myfield.options.decorators.*
; elementDecorators.*, :

elementDecorators.vh = ViewHelper
elementDecorators.er = Errors
elementDecorators.ht = HtmlTag
elementDecorators.ht.options.tag = dd
elementDecorators.lb = Label
elementDecorators.lb.options.tag = dt

Form elements


; elementsBelongTo - -, .
; SubForm's.
; , as is (, <input name="myfield"/>)
; :

elementsBelongTo = wholeFormElements
;
; ( : <input name="wholeFormElements[myfield]"/>
; :
; elements.myfield.options.belongsTo


; elementFilters.* - -
elementFilters.flt1.filter = StringTrim
elementFilters.flt1.options.chatList = " _-"
; ,
; .filter - Zend_Filter_


; elements.* -
; elements.FIELDNAME.type - Zend_Form_Element_
; elements.FIELDNAME.type = Text - Zend_Form_Element_Text

elements.myfield.type = Button
elements.myfield.options.label = "Click me ;-)"

; elements.foobar.name = FIELDNAME - :
elements.a.name = myfield
elements.a.type = Text
elements.a.options.required = true
...

; elements.FIELDNAME.options.* - : , , , ..

Groups of elements


; displayGroups.* - . - <fieldset/>
displayGroups.foo.name = mygroup
displayGroups.foo.options.legend = "Display Group Visible Title"
displayGroups.foo.options.order = 1
displayGroups.foo.options.description = "Cool fields" ; Description
displayGroups.foo.options.decorators.* ; displayGroupDecorators.*
displayGroups.foo.elements.a = myfield1 ; , elements.myfield1
displayGroups.foo.elements.b = myfield2 ;

And finally. The storage of forms in ini-files is convenient and their format can be trained by designers and designers, but there is also a fly in the ointment - performance. With this use of forms, we get a penalty on reading the file, parsing it and setting up the form.
To avoid this, I cache the Zend_Form object that was created on the ini-file.

PS: If the article will be useful not only to me, then in the following articles I will describe in detail the configuration of elements, filters, validators and decorators.

Source: https://habr.com/ru/post/46111/


All Articles