How to use validators in InputFilter on array from post?

Makash

I have a form with an input type='text' name='article[]' . I don't know the number of article that can be post because there is a little javascript button where I can add as much I want input name=article[].

For now, I use Zend\InputFilter\InputFilter but the validators never get the value on the array in my $_POST.

My input : 
<input name="article[]" class="form-control input-md" type="text" >         


My InputFilter :
class ArticleFormFilter extends InputFilter{
    public function __construct()    {

    $this->add(array(
            'name'       => 'article[]',
            'required'   => true,
            'filters' => array(
                array(
                    'name'    => 'Zend\Filter\StripTags',
                ),
                array(
                    'name'    => 'Zend\Filter\StringTrim',
                ),
            ),
            'validators' => array(
                array(
                    'name' => 'NotEmpty',                       
                ),
            ),            
        ));
    }
}

If I do it with only one article, using article instead of article[] and no Javascript, it works of course.

Filipp Qoma

To validate and/or filter arrays of POST data use CollectionInputFilter:

class MagazineInputFilter extends \Zend\InputFilter\InputFilter
{
    public function __construct()
    {            
        $this->add(new \Zend\InputFilter\Input('title'));        
        $this->add(new ArticlesCollectionInputFilter(), 'articles');
    }
}

class ArticlesCollectionInputFilter extends \Zend\InputFilter\CollectionInputFilter
{
    public function __construct()
    {
        // input filter used for each article validation. 
        // see source code of isValid() method of this class
        $inputFilter = new \Zend\InputFilter\InputFilter();

        /* 
        add inputs and its validation/filtration chains
        */

        $this->setInputFilter($inputFilter);
    }
}

Or setup input filter for collection inside main input filter of magazine:

class MagazineInputFilter extends \Zend\InputFilter\InputFilter
{
    public function __construct()
    {
        $articles = new \Zend\InputFilter\CollectionInputFilter();
        $articlesInputFilter = new \Zend\InputFilter\InputFilter();
        /*
        add inputs and its validation/filtration chains
         */
        $articles->setInputFilter($articlesInputFilter);

        $this->add(new \Zend\InputFilter\Input('title'));        
        $this->add($articles, 'articles');
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use Java Bean Validators (JSR-303/JSR-349) on elements of an array/list/collection

From Dev

Use returned array from jQuery $.post

From Dev

How to get the value from array from custom post and use in my ajax call

From Dev

How to receive array data from post in php?

From Dev

How to store values from ViewModel for use in POST

From Dev

Use $validators on blur/focus events

From Dev

Use $validators on blur/focus events

From Dev

How to use a string array returned from a function?

From Dev

How to use array from one method in another?

From Dev

How to use JSON to return array from a query?

From Dev

How to use array from one method in another?

From Dev

How to use JSON to return array from a query?

From Dev

How to use Array.from with a XPathResult?

From Dev

how to get post value from multiple array in yii2

From Dev

How to pull info from a dynamic dropdown of array to post in another table

From Dev

How to programatically HTTP POST a CSV file directly from an array in PHP?

From Dev

How to get values from array using POST method

From Dev

PHP: How to save multiple checkbox array from $_POST in variable

From Dev

How to insert mulitple POST value from array using PHP

From Dev

How to Submit empty array from HTML Form Post to PHP

From Dev

How to post back array of strings from razor view to MVC controller?

From Dev

PHP: How to save multiple checkbox array from $_POST in variable

From Dev

How to use POST with javascript adapter from a cordova app?

From Dev

How use script for button which load on page from .post?

From Dev

How can laravel use the data from angular ajax post

From Dev

How to use POST with javascript adapter from a cordova app?

From Dev

How to use $_SESSION to pass on values from multiple pages and $_POST together

From Dev

How to use HttpClient to return data from webAPI using Post?

From Dev

FluentValidation Validators and Simple Injector where Validators Injected as Array

Related Related

  1. 1

    How to use Java Bean Validators (JSR-303/JSR-349) on elements of an array/list/collection

  2. 2

    Use returned array from jQuery $.post

  3. 3

    How to get the value from array from custom post and use in my ajax call

  4. 4

    How to receive array data from post in php?

  5. 5

    How to store values from ViewModel for use in POST

  6. 6

    Use $validators on blur/focus events

  7. 7

    Use $validators on blur/focus events

  8. 8

    How to use a string array returned from a function?

  9. 9

    How to use array from one method in another?

  10. 10

    How to use JSON to return array from a query?

  11. 11

    How to use array from one method in another?

  12. 12

    How to use JSON to return array from a query?

  13. 13

    How to use Array.from with a XPathResult?

  14. 14

    how to get post value from multiple array in yii2

  15. 15

    How to pull info from a dynamic dropdown of array to post in another table

  16. 16

    How to programatically HTTP POST a CSV file directly from an array in PHP?

  17. 17

    How to get values from array using POST method

  18. 18

    PHP: How to save multiple checkbox array from $_POST in variable

  19. 19

    How to insert mulitple POST value from array using PHP

  20. 20

    How to Submit empty array from HTML Form Post to PHP

  21. 21

    How to post back array of strings from razor view to MVC controller?

  22. 22

    PHP: How to save multiple checkbox array from $_POST in variable

  23. 23

    How to use POST with javascript adapter from a cordova app?

  24. 24

    How use script for button which load on page from .post?

  25. 25

    How can laravel use the data from angular ajax post

  26. 26

    How to use POST with javascript adapter from a cordova app?

  27. 27

    How to use $_SESSION to pass on values from multiple pages and $_POST together

  28. 28

    How to use HttpClient to return data from webAPI using Post?

  29. 29

    FluentValidation Validators and Simple Injector where Validators Injected as Array

HotTag

Archive