How to do form field checks correctly

0

I have form and I have two checks , they are defined with these names: $ checkFin and $ checkDep, if any of these checks are checked, it is mandatory to fill in the fields corresponding to them. I did the validation but the same is failing and I do not know if the form I did is more indicated or even the correct form, the verification I am doing this:

// FINANCIAMENTO
if (isset($checkFin)) {     
    if (empty($BancoFinanc)):
        $retorno = array('codigo' => 0, 'mensagem' => ' Opção Financiamento marcada, por favor preencha o banco!');
        echo json_encode($retorno);
        exit();
    endif;  
} 

// DEPÓSITO BANCÁRIO
if (isset($checkDep)) {     

    if (empty($BancoDepCta)):
        $retorno = array('codigo' => 0, 'mensagem' => ' Opção Depósito Bancário marcada, por favor preencha o Banco!');
        echo json_encode($retorno);
        exit();
    endif;  

    if (empty($Agencia)):
        $retorno = array('codigo' => 0, 'mensagem' => ' Opção Depósito Bancário marcada, por favor preencha a Agência!');
        echo json_encode($retorno);
        exit();
    endif;

    if (empty($ContaCorrent)):
        $retorno = array('codigo' => 0, 'mensagem' => ' Opção Depósito Bancário marcada, por favor preencha a ContaCorrent!');
        echo json_encode($retorno);
        exit();
    endif;

    if (empty($Correntista)):
        $retorno = array('codigo' => 0, 'mensagem' => ' Opção Depósito Bancário marcada, por favor preencha o Correntista!');
        echo json_encode($retorno);
        exit();
    endif;      

    if (empty($CPFCNPJ)):
        $retorno = array('codigo' => 0, 'mensagem' => ' Opção Depósito Bancário marcada, por favor informe CPF/CNPJ o banco!');
        echo json_encode($retorno);
        exit();
    endif;      
}

The error that is occurring is that by checking the second check $ checkDep and do not fill any fields corresponding to it the script is giving me the message of the first check.

    
asked by anonymous 15.08.2016 / 23:24

1 answer

1

In this case if the first check fails you are doing Exit() . In your test did you mark it?

If you want all error messages at the same time modify your code to something like this:

// FINANCIAMENTO
$retorno = array();

if (isset($checkFin)) {     
    if (empty($BancoFinanc)):
        $retorno[] = array('codigo' => 0, 'mensagem' => ' Opção Financiamento marcada, por favor preencha o banco!');
        //echo json_encode($retorno);
        //exit();
    endif;  
} 

// DEPÓSITO BANCÁRIO
if (isset($checkDep)) {     

    if (empty($BancoDepCta)):
        $retorno = array('codigo' => 0, 'mensagem' => ' Opção Depósito Bancário marcada, por favor preencha o Banco!');
        //echo json_encode($retorno);
        //exit();
    endif;  
...

echo json_encode($retorno);
    
16.08.2016 / 14:25