Validating inputs php

3

I need to show all errors in completing the form first to then run FOR, but if I use DIE for each IF it will stop the script and it will not show if there are any more errors, what way to do this?

if($parcelas == 0){
echo "Digite valor acima de 1 parcela para gerar<br>";
}
if($valor == 0){
echo "Digite valor acima de 0 para gerar<br>";
}
if($vencimento) {
function validateDate($date, $format = 'd-m-Y')
{
    $d = DateTime::createFromFormat($format, $date);
    return $d && $d->format($format) == $date;
}
if (validateDate($vencimento,'d/m/Y') == false){
    echo "Digite a data de vencimento corretamente DIA/MÊS/ANO<br>";
    die;
}
}
///Executar o for abaixo
for($i =0, $x = 1 ;$x <= $parcelas, $i <= $parcelas; $x++ ,$i++ ){}
    
asked by anonymous 26.11.2015 / 12:54

1 answer

2

A simple way to deal with situations like this would be to add each returned error to a single array.

<?php

$erros = "";

function vazio($args){
    global $erros;
    if(!empty($args) && is_array($args)){
        foreach($args as $arg){
            if(empty($_POST[$arg])){
                $erros[$arg] = $arg . " nao pode estar vazio";
            }
        }
    }
}
if(isset($_POST)){
    vazio(['nome','email','senha']);
    if($erros){
        print "<strong>Erros encontrados</strong>";
        foreach($erros as $erro){
            print "<li>{$erro}</li>";
        }
    } else {
        print "<strong>Nenhum erro encontrado</strong><br/>";
        print "Ola {$_POST['nome']}";
    }
}

?>

Imagining that the form looks something like this:

<form method="POST" action="">
    <input type="text" name="nome">
    <input type="email" name="email">
    <input type="password" name="senha">
    <input type="submit" name="enviar" value="enviar">
</form>

The logic is this, log each error returned in a single array, and then check if there is any error in this array, if you do not proceed with the script.

    
26.11.2015 / 13:16