Chapter 3 of zendframework Learning (Core Component-filter-zend_filter_input filter)

Source: Internet
Author: User

Because the zend_filter_input filter is special, let's take it out and study it together. This is specifically used to filter user input data in the interaction program. To put it bluntly, it is used to filter the form. To implement filtering, follow these steps:

1. Define filtering and validation rules.

2. Create a filter and validation processor.

3. Search processed fields and other report content.

Next we will follow the three steps above to learn the zend_filter_input filter definition method ------------

Define filter and validation rules

Before creating a zend_filter_input class, you must create an array of filter rules and validation rules for the filter.

The two keys and values are the names of the foreground form items to be obtained (that is, the value of name in the form) and the subclass name (that is, the filtering and validation rules) for processing it ).

Here is an example of a book:

$ Filters = array (// define filter rules
'Username' => 'stringtolower ',
'Month' => 'digits'
);

$ Validators = array (// define validation rules
'Username' => 'alpha ',
'Month' => array (Array ('between', 1, 12 ))
);

No, this is the Filtering Rule and validation rule defined in array mode. Username is the value of form name, And stringtolower is the rule.

In addition to this single definition method, you can also use the following form to use a set of validation rules for the specified content.

$ Validators = array (

'Month' => array (

'Digits ', // string indicates the class name

'New zend_validate_int (), // Object Representation class

'Array ('between',) // The array is represented by a string and additional parameters.

)

);

The created validators perform a three-factor verification on the month, namely, Numerical verification, certificate verification, and verification between 1 and 12. It can be seen that the array parameter can be used to indicate the verification name as a string or to make an instance of the validator class.

It can also be expressed in the form of an array, and if there are additional parameters in the checker, it must be represented in the form of an array. You can also use the wildcard "*" to change the symbol to represent all form items. For example, the following code:

$ Filters = array (

'*' => 'Htmlentities ',

'Age' => 'digits'

);

Create input filter

To create an input filter, You Can instantiate an object and add the specified parameters as follows:

$ Input = new zend_filter_input ($ filters, $ validators, $ data)

$ Filters is the Filtering Rule array, $ validators is the validity rule array, and $ data is the source of form data (that is, Form method, such as post ). If the value of $ data is $ _ post. The filter defined by the Code will pass data from the array $ _ post.

Search processed fields and other content

After defining the zend_filter_input filter, you can obtain the content of the filter in multiple ways, including invalid, missing, and unknown. Of course, the most important thing is to return the content processed by the filter. You can obtain the processed content as follows:

$ Input = new zend_filter_input ($ filters, $ validators, $ data)

$ M = $ input-> month;

$ Input is the instance of the specified zend_filter_input filter, and month is the name of the filtered form item.

For the zend_filter_input filter, I made an example. This example is a classic modification.

1. Create a project first.

2. Create a controller selfcontroller. php

3. Create the public function selfaction () method to call the form view.

4. Create the/self. phtml view for writing forms.

5. Create the public function jieguoaction () method to submit the form content here. In this method, retrieve fields and other content

The steps are as follows:

The content of the selfcontroller. php file is as follows --

<? PHP
/* Zend_controller_ation */
Require_once 'zend/controller/action. php ';
// Selfcontroller

Class selfcontroller extends zend_controller_action
{
Public Function selfaction ()
{
// Use the input filter

}



Public Function jieguoaction ()
{

$ Filters = array (// define filter rules
'Username' => 'stringtolower ',
'Month' => 'digits'
);

$ Validators = array (// define validation rules
'Username' => 'alpha ',
'Month' => array (Array ('between', 1, 12 ))
);

$ Data =$ _ post;
$ Input = new zend_filter_input ($ filters, $ validators, $ data); // instantiate the object for the class and specify parameters




If ($ input-> hasinvalid ()){
Echo & quot; 111 & quot ";
$ Invalidfiedlds = $ input-> getinvalid (); // obtain Invalid Content. Form items that have not been validated
}

If ($ input-> hasmissing ()){
Echo & quot; 222 & quot ";
$ Missingfields = $ input-> getmissing ();
}
If ($ input-> hasunknown ()){
Echo & quot; 333 & quot ";
$ Unknownfields = $ input-> getunknown ();
}



$ M = $ input-> month;
$ N = $ input-> username;
$ M3 = $ input-> getunescaped ('month ');


Echo "<p> ";
Echo "unfiltered form items :";
Echo "<p> ";
Print_r ($ invalidfiedlds );

Echo "<p> ";
Echo "no form item is displayed :";
Echo "<p> ";
Print_r ($ missingfields );

Echo "<p> ";
Echo "the form items not defined for filtering are :";
Echo "<p> ";
Print_r ($ unknownfields );





Echo "<p> ";
Echo "the filtered month is :";
Echo "<p> ";
Print_r ($ m );

Echo "<p> ";
Echo "the filtered user name is :";
Echo "<p> ";
Print_r ($ N );


}



}

The content of the file self. phtml is as follows:

<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> application zend_filter_input instance foreground </title>
</Head>
<Body>
<P align = "center">
<Table border = "1">
<Captoin> Add form </captoin>
<Form method = "Post" Action = "<? PHP echo $ this-> baseurl ();?> /Self/jieguo ">
<Tr>
<TD> name: </TD>
<TD> <input type = "text" name = "username"> </TD>
</Tr>
<Tr>
<TD> Gender: </TD>
<TD> <input type = "radio" name = "sex" value = "male" Checked> male
<Input type = "radio" name = "sex" value = "female"> female
<Input type = "radio" name = "sex" value = "abnormal"> abnormal </TD>
</Tr>
<Tr>
<TD> Date of Birth </TD>
<TD>
<Input type = "text" name = "year" size = "4"> year
<Input type = "text" name = "month" size = "4"> month
<Input type = "text" name = "day" size = "4"> day
</TD>
</Tr>
<Tr>
<TD colspan = "2" align = "center">
<Input type = "Submit" value = "Submit">
<Input type = "reset" value = "reset">
</TD>
</Tr>
</Form>
</Table>

</Body>
</Html>

It is similar to the Code in the book, but in some places I have changed it. I write the entire project, and the book only contains a few files, the difference is that I don't need to load classes or anything. I should have loaded all the classes in the system.

A journey of a thousand miles begins with a single step. (Lao Tzu)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.