Question:
I need to show all errors when filling out the form first and then run the FOR, but if I use DIE for each IF it will stop the script and it won't show if there are any more errors, which 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++ ){}
Answer:
A simple way to handle situations like this would be to add each error returned 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 the form was 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 that array, if you don't proceed with the script execution.