Zend_Form and. ini files
I want to talk about form customization via ini files. It will be useful to programmers and coders.
Believe that you already know roughly what is the Zend Framework and know where documentation. You might even use Zend_Form in your projects.
If not, here are the basic concepts:
Element. Sobsno, formalized an html form element. Standard called Zend_Form_Element_* and are descendants of Zend_Form_Element. Their own items are created by extending a standard. Read more here.
Decorators. Responsible for generating html. Somehow, one of the most difficult to understand pieces, but it is very powerful and flexible tool. Read more here, tut here.
Validators. Do data validation and generating error messages. Read more here.
Filters. Filter and modify data received from the form. For example, if the user enters " John DOE ", bringing to mind "John DOE" engaged in the filters. Read more here.
member Group. DisplayGroup — the tool to visually group items in group.
Translators. Support for multiple languages. Read more here.
To validate forms you will need two files,
form.php
and form.ini
. First here, and second I'll tell you.<?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;
the
Structure of ini file
In the file specify the attributes of a tag <form> global values, elements, default values for elements, etc.
the
Attributes
Any unrecognized keys are interpreted as attributes of a tag <form>. But you can specify explicitly through the
attrs.*
.;;;;
;;;; form.ini
;;;;
action = /some/url
; post | get | put | delete
method = post
; "application/x-www-form-urlencoded" | "multipart/form-data"
enctype = "custom-enctype"
; requires decorator Description
description = "form description"
; requires decorator Fieldset
legend = "form legend"
; <form name="">
name = "attr name"
; int - the order for the SubForm (where the form is part of another form)
order = 1
; true | false - off installation and booting decorators by default
; When using ini files is absolutely useless option (see decorators.*)
disableLoadDefaultDecorators = false
; true | false - off translators
disableTranslator = false
; attribs.* - explicitly setting the attributes of a tag <form>
attribs.action = /yet/another/url ; overwrites value if the action specified after it
; defaults.* - set field values by default.
defaults.myfield = "Default value for my field"
defaults.anotherfield = "Pretty nice value for another field""
the
class Path
; prefixPath prefix for automatic class loading elements and decorators.
; Default value:
prefixPath.prefix = Zend_Form_
prefixPath.path = Zend/Form/
; decorators prefix and the path will be changed to Zend_Form_Decorator_MyDecorator
; and Zend/Form/Decorator/MyDecorator.php
; For the elements respectively and Zend_Form_Element_MyElement Zend/Form/Element/MyElement.php
; If classes need to look in several places:
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 prefix that will be set to each element of the form elements.*
; Set for decorators, filters, and validators
; Otherwise, same as 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 - similar prefixPath.* for groups
; defaultDisplayGroupClass class for processing groups of fields.
; Default is Zend_Form_DisplayGroup
defaultDisplayGroupClass = Zend_Form_DisplayGroup
the
Decorators
; decorators.* - set decorators of the form.
; The mention here at least one decorator decorators will override the default.
; IMPORTANT: If you forget about the FormElements decorator, the form elements will not be rendered!
; Example for decorators by default:
decorators.elements = FormElements
decorators.tag = HtmlTag
decorators.tag.options.tag = dl
decorators.tag.options.class = zend_form
decorators.form = Form
; displayGroupDecorators.* - decorators for field groups
; sets the decorators for all field groups
; Decorators for specific groups are set using displayGroups.groupName.options.decorators.*
; If you do not specify displayGroupDecorators.*, we will use an analogue of such determination:
displayGroupDecorators.el = FormElements
displayGroupDecorators.tg = HtmlTag
displayGroupDecorators.tg.options.tag = dl
displayGroupDecorators.fs = Fieldset
displayGroupDecorators.dt = DtDdWrapper
; elementDecorators.* default decorators for form fields
; Private decorators are set similarly via the elements.myfield.options.decorators.*
; If you do not specify elementDecorators.*, it will be used by decorators is identical to the definition:
elementDecorators.vh = ViewHelper
elementDecorators.er = Errors
elementDecorators.ht = HtmlTag
elementDecorators.ht.options.tag = dd
elementDecorators.lb = Label
elementDecorators.lb.options.tag = dt
the
form Elements
; elementsBelongTo - array name by default, which will be grouped field values.
; Useful when using SubForm''s.
; If you do not specify this parameter, the field name will be as is (e.g. <input name="myfield"/>)
; And if you do so:
elementsBelongTo = wholeFormElements
; the field name is inside the specified array.
; (for the example above would become: <input name="wholeFormElements[myfield]"/>
; NOTE: to Change the default value for a specific field using
; elements.myfield.options.already's belongsto not be
; elementFilters.* filters by default for all elements
elementFilters.flt1.filter = StringTrim
elementFilters.flt1.options.chatList = " _-"
; this example will trimite not only spaces, but also hyphens and underscores
; value .filter is the name of the filter class without Zend_Filter_
; elements.* - form elements
; elements.FIELDNAME.type - the suffix of the class name Zend_Form_Element_
; elements.FIELDNAME.type = Text - will be used Zend_Form_Element_Text
elements.myfield.type = Button
elements.myfield.options.label = "Click me ;-)"
; elements.foobar.name = FIELDNAME convenient when the element has many options:
elements.a.name = myfield
elements.a.type = Text
elements.a.options.required = true
...
; elements.FIELDNAME.options.* - option item: decorators, validators, filters, attributes, etc.
the
member Groups
; displayGroups.* - field group. The default is merged with the tag <fieldset/>
displayGroups.foo.options.legend = "Group Visible Display Title"
displayGroups.foo.options.order = 1
displayGroups.foo.options.description = "Cool fields" ; requires decorator Description
displayGroups.foo.options.decorators.* ; similarly displayGroupDecorators.*
displayGroups.foo.elements.a = myfield1 ; the field defined elements.myfield1
displayGroups.foo.elements.b = myfield2 ; similarly,
And finally. Storing forms in the. ini files and their format can train the coders and designers, but there is a fly in the ointment is performance. In this application, the forms we get a penalty in reading a file, parsing it and setting up the form.
To avoid this, I kichirou a Zend_Form object that was created via the ini file.
PS: If the article will be useful not only to me, in the next articles, I will describe the configuration of elements, filters, validators, and decorators.
Комментарии
Отправить комментарий