Use filter_input (FILTER_SANITIZE_NUMBER_INT) in an array with integer type items

1

I have the following form:

<form action="<?=PATH_ROOT?>professor/cadastrar" method="post">
     Matrícula:
     <input type="text" name="matricula">

     <br><br>
     Nome:
     <input type="text" name="nome">

     <br><br>
     Turmas:<br>
     <select name="turmas[]" multiple="multiple">
          <?=$this->optionsTurma?>
     </select>

     <br><br>
     <input type="submit" value="Cadastrar">
</form>

Where select classes [] will have a dynamic size, that is, it can have 1, 2, 3, 4, ... , because it is a table of bank.

And I'm modifying the validation using filter_input(); and it looks like this:

if($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST)) {

     $post['matricula'] = filter_input(INPUT_POST, 'matricula', FILTER_SANITIZE_NUMBER_INT);
     $post['nome'] = filter_input(INPUT_POST, 'nome', FILTER_SANITIZE_STRING);
     $post['turmas'] = filter_input(INPUT_POST, 'turmas', FILTER_SANITIZE_NUMBER_INT);

     if ($this->model->save($post))
          echo '<script>alert("Cadastrado com sucesso!");</script>';
     else
          echo '<script>alert("Erro ao cadastrar.");</script>';
}

My $post['turmas'] will always return item (ns) of integer type. The problem is that I always return boolean false because of the reason I'm giving FILTER_SANITIZE_NUMBER_INT to an array.

$post['turmas'] = filter_input(INPUT_POST, 'turmas', FILTER_SANITIZE_NUMBER_INT);

How can I give FILTER_SANITIZE_NUMBER_INT to each item in my class array brought from the form, is it possible?

    
asked by anonymous 07.12.2014 / 15:02

1 answer

2

Place FILTER_REQUIRE_ARRAY as the fourth argument of the filter_input function.

$post['turmas'] = filter_input(INPUT_POST, 'turmas', FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY);
    
07.12.2014 / 15:25