Zend框架1.6使用Zend Filter Input验证POST请求

With Zend framework 1.6 I recive a POST request like this:

array(2) {
  ["checkins"] => array(18) {
    ["nome"] => string(6) "MYNAME" //the field is correctly filled
    ["pec"] => string(0) ""
    ["sito_web"] => string(0) ""
    ["note"] => string(0) ""
  }
  ["users"] => array(19) {
    ["email"] => string(29) "email@gmail.com"
    ["u_id"] => string(1) "1"
  }
}

To Validate the 'nome' field I am using Zend Input Filter. This is my validator array:

    $validators = array (
                'nome' => array (
                        'presence' => 'required'
    ));
    $params = $this->getRequest ()->getPost ();

    $input = new Zend_Filter_Input ( array (), $validators,$params );

    if (! $input->isValid ()) {
        print_r($input->getMessages ());
    }

It seems that the validation is not well formed becouse I recive the message:

Field 'nome' is required by rule 'nome', but the field is missing

In my opinion there is an error in my $validators array, but I can't find it.

Your problem is, you use an array notation in your html form.

You may have something like this in your html:

<form ...>
...
<input type="text" name="checkins[nome]" ... /> 
...
</form>

This is called array notation in form elements.

You can get this array in ZF by calling:

$request = $this->getRequest();

    if($request->isPost()) {

        $post = $request->getPost();

        $validators = array (
            //nome has to be present and not empty
            'checkins' => array(
                'nome' => array(
                    'NotEmpty',
                    'presence' => 'required'
                ),
                //if pec is present, it has to be not empty
                'pec' => array(
                    'NotEmpty'
                )
            )
        );

        //validate the post array
        $input = new Zend_Filter_Input( array(), $validators, $post);

        if (!$input->isValid()) {
            Zend_Debug::dump($input->getMessages());
        }

        Zend_Debug::dump($post);
    }

But there are things happening, I dont understand..

If using the next approach, everything works just fine...

$request = $this->getRequest();

$checkins = $request->getPost('checkins');

$validators = array (
    //nome has to be present and not empty
    'nome' => array(
        'NotEmpty',
        'presence' => 'required'
    ),
    //if pec is present, it has to be not empty
    'pec' => array(
        'NotEmpty'
    )
);

//validate the checkins array instead of the whole array
$checkinsFilter = new Zend_Filter_Input( array(), $validators, $checkins);

if (!$checkinsFilter->isValid()) {
    Zend_Debug::dump($checkinsFilter->getMessages());
}

Hope it helps.

Read more about Zend_Validate_Input and the metacommands here: http://framework.zend.com/manual/1.6/de/zend.filter.input.html#zend.filter.input.metacommands.presence

And more about the available validator classes here: http://framework.zend.com/manual/1.6/de/zend.validate.set.html

And maybe think about using a Zend_Form to generate an Object that has all the validators, filters and elements you need. You dont need to use this form to render an html output. But use it for validating and filtering is much simpler than doing it manually for all kind of forms.

Have fun and good luck!