Wednesday, 14 May 2014

Zend_Filter_Input - Zend_Filter - Zend Framework

When user submit the data through Form, Developer need to filter the submitted data. because we can not trust on end user, he can be hacker. User can be add invalid data that cause, crash your page OR website. So Never trust on user and  always filter form data before saving in your database OR showing in website.

In Zend Framework, we can filter data differently with different field in single function. There is not need to use two different functions, one for filter and second for validator. There is zend_filter_input which can perform these two task. Lets take an example.

For Example 
In Age, We need to trim all Alphabets, only number is accepted
In Name, W need to trim all except alphabets
In DOB, We need to trim all except number and hyphen(-)

In Description, We need to removed all scripts tags because It can be XSS Attack.

We can add different filters on different fields. For Example in below code:
We add strip_tags and trim for Name and Address
We allow Alphanumeric and space for Address

Use following code to filter Form-Data in Zend Framework
  $formData = array(
            'name' => ' arun ..7809890809843 !@#$%%%',
            'address' => 'filter data *)(*)('
        $filters = array(
            '*' => array('StripTags', 'StringTrim'),
            'address' => array(array('Alnum', array('allowwhitespace' => true))),

        $data = new Zend_Filter_Input($filters, null, $formData);
        $filterData = $data->getEscaped();        

5 Best Related Posts are Following:1. Web service - current time zone for a city- Free API
2. Zend Gdata Youtube API - Search Video - View Video Detail
3. Download the video from Amazon Simple Storage Service S3
4. How to set Cron in Zend Framework
5. Zend Cache Tutorial - Zend Framework 1.12

No comments:

Post a Comment