Form Zend Framework
primarily as a bridge between your domain models and the View Layer.
Zend/form-
This is used to bind/bridge b/w data model to view layer and provide generic api to build view form with element also provide interface to build custom form element
Form have following following objects
- inputFliter -This provide small number of method to filter data
Example
$inputFilter = new InputFilter();
- Elements - It simply create html form element & Zend\Form\Element is a base class for all specialized elements
Example.
$name = new Element('name');
$name->setLabel('Your name');
$name->setAttributes(array(
'type' => 'text',
'class' => 'name',
'size' => '30',
));
Zend\InputFilter\InputProviderInterface interface will be used to create custom input type and custom element need to add in the plugin manager, in your Module.php class:
Example
abstract class Phone extends Element implements InputProviderInterface{
public function getValidator(){}
public function setValidator(){}
public function getInputSpecification(){}
}
-- Button , Captcha , Checkbox , Collection , Csrf , File , Hidden , Image , MonthSelect , MultiCheckbox , Password , Radio , Select , Submit , Text , Textarea , Color , Date , DateTime , DateTimeLocal , Email , Month , Number , Range , Time , Url , Week ,
- Fieldsets - Fieldsets is used to build reusable group of form element which can be attached in form fieldsets can also have Fieldsets and so on. Fieldsets extend Elements but allow composing other fieldsets and elements.
Example
$sender = new Fieldset('sender');
$sender->add($name);
Collections - Collections is used to add fieldsets or elements dynamically in form.
Exmaple
$form->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'categories',
'options' => array(
'label' => 'Please choose categories for this product',
'count' => 2,
'should_create_template' => true,
'allow_add' => true,
'target_element' => array(
'type' => 'Application\Form\CategoryFieldset',
),
),
));
NB.
###count -- this is how many times the element (in this case a category) has to be rendered
###should_create_template -- if set to true, it will generate a template markup in a <span> element
###allow_add -- if set to true (which is the default), dynamically added elements will be retrieved and validated; otherwise, they will be completely ignored. This, of course, depends on what you want to do
###target_element -- this is either an element or, as this is the case in this example, an array that describes the element or fieldset that will be used in the collection. In this case, the target_element is a Category fieldset.
- Forms - this accept elements, fieldsets, and/or forms, and use the attributes they compose to render markup. this can also build with
-- by annotate data model and hydrators that should all be used together. Zend\Form\Annotation\AnnotationBuilder
-- by initiating form object
-- by factory menthod
-- by factory backed Form Extension
Example :
$form = new Form('contact');
$form->add($name);
$form->setInputFilter($inputFilter);
$form->add($sender);
#Validating forms requires three steps.
- The form must have an input filter attached
- You must inject the data to validate into the form.
- You validate the form. If invalid, you can retrieve the error messages, if any
Example :
$data = $request->getPost();
$form->setData($data);
if ($form->isValid()) {
$validatedData = $form->getData();
$validatedData = $form->getData(FormInterface::VALUES_AS_ARRAY);
}else{
$messages = $form->getMessages();
}
--Validation Groups - Sometimes you want to validate only a subset of form elements
$form->setValidationGroup('name', 'email', 'subject', 'message');
$form->setData($data);
if ($form->isValid()) {
// Contains only the name", "email", "subject", and "message" values
}
Pass FormInterface::VALIDATE_ALL flag to the setValidationGroup() to validate all input
-file Uploading
$prg = $this->fileprg($form);
if ($prg instanceof \Zend\Http\PhpEnvironment\Response) {
return $prg; // Return PRG redirect response
} elseif (is_array($prg)) {
if ($form->isValid()) {
}
}
Behind the scenes, the FilePRG plugin will:
--Run the Form’s filters, namely the RenameUpload filter, to move the files out of temporary storage.
Store the valid POST data in the session across requests.
--Change the required flag of any file inputs that had valid uploads to false. This is so that form re-submissions without uploads will not cause validation errors.
#Factory - this is used to create elements, fieldsets, forms, and the related input filter.
#Binding an object: in first step i say forms is bridge b/w data model and the view layer.
When you bind() an object to the form the following happens:
-- The composed Hydrator calls extract() on the object, and uses the values returned, if any, to populate the value attributes of all elements. If a form contains a fieldset that itself contains another fieldset, the form will recursively extract the values.
-- When isValid() is called, if setData() has not been previously set, the form uses the composed Hydrator to extract values from the object, and uses those during validation.
-- If isValid() is successful (and the bindOnValidate flag is enabled, which is true by default), then the Hydrator will be passed the validated values to use to hydrate the bound object. (If you do not want this behavior, call setBindOnValidate(FormInterface::BIND_MANUAL)).
-- If the object implements Zend\InputFilter\InputFilterAwareInterface, the input filter it composes will be used instead of the one composed on the form.
#Rendering - form component ships a set of form-specific view helpers. When preparing to render, you will likely want to call prepare(). This method ensures that certain injections are done, and will likely in the future munge names to allow for scoped[array][notation]
View helpers with example
--Form
$form = $this->form;
$form->prepare();
--FormElement
$this->formElement($form->get('send'))
--FormLabel
$formLabel = $this->plugin('formLabel');
--FormElementErrors
$this->formElementErrors($name);
--formButton
$this->formButton($element);
--FormCaptcha
$this->formCaptcha($captchaElement);
--FormCheckbox
--FormCollection
--FormFile
--FormHidden
--FormImage
--FormInput
--FormMultiCheckbox
--FormPassword
--FormRadio
--FormFileApcProgress
echo $this->formFileApcProgress();
Zend/form-
This is used to bind/bridge b/w data model to view layer and provide generic api to build view form with element also provide interface to build custom form element
Form have following following objects
- inputFliter -This provide small number of method to filter data
Example
|
- Elements - It simply create html form element & Zend\Form\Element is a base class for all specialized elements
Example.
$name = new Element('name');$name->setLabel('Your name');$name->setAttributes(array('type' => 'text','class' => 'name','size' => '30',));
Zend\InputFilter\InputProviderInterface interface will be used to create custom input type and custom element need to add in the plugin manager, in your Module.php class:
Example
|
-- Button , Captcha , Checkbox , Collection , Csrf , File , Hidden , Image , MonthSelect , MultiCheckbox , Password , Radio , Select , Submit , Text , Textarea , Color , Date , DateTime , DateTimeLocal , Email , Month , Number , Range , Time , Url , Week ,
- Fieldsets - Fieldsets is used to build reusable group of form element which can be attached in form fieldsets can also have Fieldsets and so on. Fieldsets extend Elements but allow composing other fieldsets and elements.
Example
|
Collections - Collections is used to add fieldsets or elements dynamically in form.
Exmaple
|
NB.
###count -- this is how many times the element (in this case a category) has to be rendered
###should_create_template -- if set to true, it will generate a template markup in a <span> element
###allow_add -- if set to true (which is the default), dynamically added elements will be retrieved and validated; otherwise, they will be completely ignored. This, of course, depends on what you want to do
###target_element -- this is either an element or, as this is the case in this example, an array that describes the element or fieldset that will be used in the collection. In this case, the target_element is a Category fieldset.
- Forms - this accept elements, fieldsets, and/or forms, and use the attributes they compose to render markup. this can also build with
-- by annotate data model and hydrators that should all be used together. Zend\Form\Annotation\AnnotationBuilder
-- by initiating form object
-- by factory menthod
-- by factory backed Form Extension
Example :
|
#Validating forms requires three steps.
- The form must have an input filter attached
- You must inject the data to validate into the form.
- You validate the form. If invalid, you can retrieve the error messages, if any
Example :
|
--Validation Groups - Sometimes you want to validate only a subset of form elements
|
Pass FormInterface::VALIDATE_ALL flag to the setValidationGroup() to validate all input
-file Uploading
|
Behind the scenes, the FilePRG plugin will:
--Run the Form’s filters, namely the RenameUpload filter, to move the files out of temporary storage.
Store the valid POST data in the session across requests.
--Change the required flag of any file inputs that had valid uploads to false. This is so that form re-submissions without uploads will not cause validation errors.
#Factory - this is used to create elements, fieldsets, forms, and the related input filter.
#Binding an object: in first step i say forms is bridge b/w data model and the view layer.
When you bind() an object to the form the following happens:
-- The composed Hydrator calls extract() on the object, and uses the values returned, if any, to populate the value attributes of all elements. If a form contains a fieldset that itself contains another fieldset, the form will recursively extract the values.
-- When isValid() is called, if setData() has not been previously set, the form uses the composed Hydrator to extract values from the object, and uses those during validation.
-- If isValid() is successful (and the bindOnValidate flag is enabled, which is true by default), then the Hydrator will be passed the validated values to use to hydrate the bound object. (If you do not want this behavior, call setBindOnValidate(FormInterface::BIND_MANUAL)).
-- If the object implements Zend\InputFilter\InputFilterAwareInterface, the input filter it composes will be used instead of the one composed on the form.
#Rendering - form component ships a set of form-specific view helpers. When preparing to render, you will likely want to call prepare(). This method ensures that certain injections are done, and will likely in the future munge names to allow for scoped[array][notation]
View helpers with example
--Form
$form = $this->form;
$form->prepare();
--FormElement
$this->formElement($form->get('send'))
--FormLabel
$formLabel = $this->plugin('formLabel');
--FormElementErrors
$this->formElementErrors($name);
--formButton
$this->formButton($element);
--FormCaptcha
$this->formCaptcha($captchaElement);
--FormCheckbox
--FormCollection
--FormFile
--FormHidden
--FormImage
--FormInput
--FormMultiCheckbox
--FormPassword
--FormRadio
--FormFileApcProgress
echo $this->formFileApcProgress();
Comments
Post a Comment