How to display errors in the form

Asked

Viewed 49 times

1

I’m working with the design standard (PRG) Post Redirect Get).

I have 3 files

1 - Class Pessoa.PHP

2 - formulario.PHP

3 - processa_form.PHP

What is the best way to handle and display errors in a web application?

Follow an example

class Pessoa {
   private nome;
   private ano_nasc;
   private cpf;

   public function getAno_nasc(){
       $this->ano_nasc;
   }
   public function getNome()....

the problem begins with the methods set

What is the best way to return errors from set and insert to the page containing the form?

I thought of the following possibilities

1 - I found it ineffective because I won’t know for sure the reason for the error

public function setAno_nasc($ano){
        if ($ano > 3000 || $ano < 1900 )
            return false;
        else
            return true;

2 - I thought it better but I still think that in the future it can be a problem because I’ll have to unite all the mistakes of set and then display on the form

public function setAno_nasc($ano){
        if ($ano > 3000 || $ano < 1900 ){
       return "Ano inválido";
   }
public function setCpf($cpf){
        if (strlen($cpf) > 11)
           return "CPF inválido";    
}    

3 - This way I thought better but I do not know if it is correct

public function setAno_nasc($ano){
        if ($ano > 3000 || $ano < 1900 ){
             $_SESSION['erro'] .= "Ano inválido#";
        }
}

public function setCpf($cpf){
        if (strlen($cpf) > 11)
           $_SESSION['erro'] .= "CPF inválido#";    
}

The operation is as follows:

1- The person accesses the processa_formulario.php and send the data to processa_form.php

2- The processa_form.php instance a person and arrow the data coming from the formulario.php in attributes, check that everything is all right and saved in the bank after redirects to the formulario.php with errors or with the message from success.

3- The formulario.php displays whether you were able to register or not, if no, it informs the problems so that they can be corrected

I have gone a little long so that there is no doubt in the understanding of the problem. This is a summary model of my problem, my person class has more than 50 attributes. (Use a dynamic function to set all class attributes)

  • I always see the problem that seems bigger than what people in general notice, the unbridled use of OOP for anything (it’s not against OOP, it’s against misuse), they think that using OOP solves everything, they think it gets more organized, they think it "increases productivity" ... I will summarize, your scripts look VERY simple, nor would OOP need to process the form, it would be more useful to fill a class (if it were really necessary) if everything validated. But people don’t care, they just want to use what seems most beautiful with various excuses, it’s not a direct criticism to you.

  • @Alonein already tried to throw a new Exception throw?

  • will display the error in the wrong mpping ( during set ). I need the form validation to be displayed on the form page

  • Then you need to validate all the fields to show all the errors you have at the same time?

  • Exactly that

  • Generally, forms are dealt with in two stages. The first with javascript making validations that do not need to communicate with the application and the database. The second validation is in the application and is used throw exception for exceptions in the code. What you want to do seems to be more a javascript processing than in the application.

  • If I treat everything in javascript is very easy to circumvent validations

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.