Tuesday, 12 January 2016

Zend Framework 2 form elements Filters and Validators

Zend Framework 2 form elements Filters and Validators


Question: How to bind Objects to Forms in ZF2?
$contact = new ArrayObject;
$contact['subject'] = '[Form] ';
$contact['message'] = 'Message';
$form    = new Contact\ContactForm;
/** This way we can bind the object to form **/
$form->bind($contact); 



Question: What happen when we bind an objects to Forms in ZF2?
  1. The composed Hydrator calls extract() on the object
  2. When isValid() is called and setData() is not called before, Hydrator extract values from the form object and do validation.



Question: How to create new Element in Form Object?
$form = new Zend\Form\Form()
//Add new element in form
$form->add(array(
     'type' => 'Email',
     'name' => 'email'
));



Question: How to extend existing form?
namespace Application\Form;
use Zend\Form\Form;
class MyNewForm extends Form{
    public function __construct($name = null){
        parent::__construct($name);
        $this->add(array(
            'name' => 'phone',
            'type' => 'text',
        ))
    }
}



Question: How to create form elements using Form Manager?
$formManager = $this->serviceLocator->get('FormElementManager');
$form = $formManager->get('Application\Form\CreatePhotoAlbum');



Question: How to create form elements and add in Form?
use Zend\Form\Element;
use Zend\Form\Form;
$checkboxElement = new Element\Checkbox('checkbox');
$checkboxElement->setLabel('Are you indian');
$checkboxElement->setUseHiddenElement(true);
$checkboxElement->setCheckedValue("yes");
$checkboxElement->setUncheckedValue("No");



Question: How to add Filter in Form?
$form = new Form\CommentForm();
$form->setInputFilter(new Form\CommentFilter());



Question: How to add validation while creating a form?
use Zend\Form\Element;
use Zend\Form\Form;

$month = new Element\Month('month');
$month->setLabel('Month')->setAttributes(array(
        'min'  => '2015-01',
        'max'  => '2017-01',
        'step' => '1', // interval is 1 month
    ));

$formObject = new Form('my-form');
$formObject->add($month);



Question: What are Form elements available in Zend Framework2?
     Button
    Captcha
    Checkbox
    Collection
    Csrf
    File
    Hidden
    Image
    Month Select
    MultiCheckbox
    Password
    Radio
    Select
    Submit
    Text
    Textarea
    Color
    Date
    DateTime
    DateTimeLocal
    Email
    Month
    Number
    Range
    Time
    Url
    Week



Question: How to display form in view phtml file?
In controller set the form.
$this->view->form = $formObject;

In View render the form.
echo $this->form;



Question: How to add input filter in Zend Form?
$inputFilterObj = new InputFilter();
$inputFilterObj->->add([
            'name' => 'profileimage',
            'type' => '\Zend\InputFilter\FileInput',
            'required' => false,
            'allow_empty' => true,
            'priority' => 300,
            'filters' => [
                ['name' => 'StripTags'],
                ['name' => 'StringTrim'],
            ],
            'validators' => [
                [
                    'name' => '\Zend\Validator\File\IsImage',
                ],
                [
                    'name' => '\Zend\Validator\File\UploadFile',
                ],
                [
                    'name' => '\Zend\Validator\File\ImageSize',
                    'options' => [
                        'minWidth' => 150,
                        'minHeight' => 120,
                    ]
                ],
                [
                    'name' => '\Zend\Validator\File\Size',
                    'options' => [
                        'max' => '2MB',
                    ]
                ],
            ]
        ]);



Question: How to make filed readonly in zend form?
$mobile = new Zend_Form_Element_Textarea('Name');
    $mobile->setLabel('mobile')
    ->addFilter('StripTags')
    ->addFilter('StringTrim')
    ->setOptions(array('class' => 'full-width'))
    ->setAttrib('rows', 2)
    ->getDecorator(('label'))->setOption('tag', 'span')
    ->setAttrib('readonly', 'true');;