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

HTML Emailing in ZF2

<?php namespace Application\Model; use Zend\Mail\Message; use Zend\Mime\Message as MimeMessage; use Zend\Mime\Part as MimePart; use Zend\Mail\Transport\Sendmail as SendmailTransport; use Zend\Mail\Transport\Smtp as SmtpTransport; use Zend\Mail\Transport\SmtpOptions; use Zend\View\Renderer\PhpRenderer; use Zend\View\Resolver\TemplatePathStack; use Zend\View\Model\ViewModel; use Zend\View\Model\ModelInterface; class ApiEmail extends ApiInterface {    protected $From = "XXXXX@gmail.com";    protected $FromName = "XXXX";    protected $To = "" ;    protected $Subject = "" ;    protected $Body = "" ;    protected $Cc = "XXXXXX@gmail.com";    protected $Bcc = "XXXXX@gmail.com" ;    protected $ReplyTo = "XXXX@gmail.com" ;    protected $Sender = "" ;    protected $Encoding = "UTF-8" ;    protected $MESSAGE ; // getter setter    public fun

How to start working with Django web framework

 Django is a powerful web framework for Python that makes it easy to build web applications quickly. Here are some steps you can follow to start learning Django programming: Install Python: Django requires Python to be installed on your machine. If you don ' t already have Python installed, you can download it from the official Python website (https://www.python.org/downloads/). Install Django: Once you have Python installed, you can use the pip package manager to install Django. Open a terminal or command prompt and type the following command: pip install django Create a new Django project: To create a new Django project, open a terminal or command prompt and navigate to the directory where you want to store your project. Then run the following command: django - admin startproject myproject Replace myproject with the name you want to give to your project. This will create a new Django project with the specified name in the current directory. Run the development

How to setup django | django installation ubuntu

Introduction to Django 1.     High level web framework 1.     Basic modules, classes, and tools to quickly develop and deploy web apps 2.     Contains an ORM (Object-Relational Mapper) that allows for the use of standard Python language syntax when interfacing with a back-end database. 1.     Developer need not learn SQL, DDL, etc! 3.     Provides a template framework that allows HTML, XML, and other “documents” to be converted into “templates”, which can then be converted to “output” via a wide range of substitution techniques. 4.     Elegant URL support (fancy, beautiful URL's, no “?blah=blah”, etc.) 5.     Multi-lingual 2.     Fast and easy to use, robust, flexible, and lots of contributed components available! 1.     Built in administrative interface to manage data models. 2.     Built-in authentication/access control components 3.     Contributed Installing & Configuring Django Components 1.     Django can be downloaded from http://w