Skip to main content

Form Zend Framework


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();








Comments

Popular posts from this blog

Magento-Concept

1) Mcrypt : This is an interface to the mcrypt library, which supports a wide variety of block algorithms such as DES, TripleDES, Blowfish (default), 3-WAY, SAFER-SK64, SAFER-SK128, TWOFISH, TEA, RC2 and GOST in CBC, OFB, CFB and ECB cipher modes. Additionally, it supports RC6 and IDEA which are considered "non-free". CFB/OFB are 8bit by default. 2) Mhash : These functions are intended to work with » mhash. Mhash can be used to create checksums, message digests, message authentication codes, and more. This is an interface to the mhash library. Mhash supports a wide variety of hash algorithms such as MD5, SHA1, GOST, and many others. 3) Simplexml : The SimpleXML extension provides a very simple and easily usable toolset to convert XML to an object that can be processed with normal property selectors and array iterators. 4) DOM : The DOM extension allows you to op  erate on XML documents through the DOM API with PHP 5. 5) InnoDB storage engine : InnoDB is ...

What are the advantages and disadvantages of using zsh instead of bash (or other shells)?

Everything y ou kno w from bash  still  applies, but zsh does a bunch of other useful things.  I  know that I use only a small subset of them, but t hese are especially useful for me: Extended globbing: For example,  *(.)  matches only regular files, not directories, whereas  a*z(/)  matches directories whose names start with  a  and end with  z . There are a bunch of other things, such as  **  as described by  Ole Tange . Inline glob expansion: For example, type  rm *.pdf  and then hit tab. The glob  *.pdf  will expand inline into the list of .pdf files, which means you can change the result of the expansion, perhaps by removing from the command the name of one particular file you don’t want to  rm . Interactive path expansion: Type  cd /u/l/b  and hit tab. If there is only one existing path each of whose components start...