Am I validating twice?

-3

Hello, I have a Select on a form with only integer values.

Upon receiving this information in PHP, I make the following validations:

BecauseI'malreadyusing(int)online297andline302,ismyfirstvalidation(atline292,with!is_numeric)redundant?

Inline292Icheckifitisanumber,atline297ifitisnot"0" (which would indicate that it is the first select option "Select ...", that is, the user sent the form without selecting no option) and on line 302 I check whether the selected option actually exists in the DB (and that was not manually changed by the "Inspect Element") and that it is not "99", which is equivalent to the "Other" option of the select.

That is, how am I using the (int), do I really need the 292 line? Or do I need to do this double-check?

Thank you.

    
asked by anonymous 18.07.2016 / 07:42

1 answer

2

Yes, it's redundant.

You could apply the cast only once. The name of this is sanitize.

After sanitizing, filter and validate.

Example

$var = (int)$var; // Faz o cast para numérico inteiro

// verifica se é vazio ou igual a zero.
// o motivo é que o casting acima remove tudo que não for numérico.
if (empty($var || $var == 0)) {
    // mensagem de erro
}

On the other side of checkcategoryid and != 99 , reverse the order by first checking to see if it is 99, as this will avoid unnecessary processing if the number is 99.

note: This answer is based only on the code snippet you posted, as well as the explanations in the question.

    
18.07.2016 / 09:00