Rules for Validation of Parameters established in PHP Database

Asked

Viewed 96 times

0

Friends, I would like your feedback and suggestions regarding validation rules by parameters established on a website. I have two types of parameters, a global one, which will be used for the entire site, and a so-called product, which will be used for products. There are n validations that I have to do, first I have to validate for the global, type, the customer can only buy 3 products in general until its delivery and other parameters. Once this has been checked for product parameters, type the user may take only 3 unit of a particular product. There are many rules that will have to be compared in order to validate the data. I would like ideas, suggestions to make these validations efficiently. I have a table called parameters, where save the general parameters, and each product, except in the same products table I await the help of all

  • Does your application use the MVC standard? It is object-oriented?

  • 1

    no MVC standard and no object hole. Structured programming.

1 answer

0

Well, from what I understand your question is more conceptual than practical, you’re looking for the best way to make these validations.

In structured programming, I would do the following:

  • A function to validate global parameters
  • A function to validate product parameters
  • Another function which is the "parent" function, which processes some things and calls these two validation functions

Follow the example below:

// $dados são os valores enviados no form
function controller($dados)  // esta é a function "pai"
{
    // alguns processamentos caso você precise
    if (validacaoGlobal($dados) && validacaoProdutos($dados)) {
        // sucesso em ambas validações, exibir mensagem de sucesso
    }
    // caso contrário, algo deu errado e você retornar uma mensagem de erro informativa ao usuário
    // sobre o que aconteceu

}

function validacaoGlobal($dados)
{
    $sucesso = true;

    // faz as validaões, se algo der errado $sucesso vira false;
    // tente implementar uma lógica para elaborar uma mensagem de erro intuitiva caso algo dê errado
    // essa mensagem seria exibida para o usuário

    return $sucesso;
}

function validacaoProdutos($dados)
{
    $sucesso = true;

    // faz as validaões, se algo der errado $sucesso vira false;
    // tente implementar uma lógica para elaborar uma mensagem de erro intuitiva caso algo dê errado
    // essa mensagem seria exibida para o usuário

    return $sucesso;   
}

If you are interested in how this would be done with MVC and Object Orientation, see this response

  • the structure is already this way. Are N global parameters and N product parameters. The Return should at the time a condition is not validated. I don’t think I’ll be able to get out of many ifs and return the reason for the failure. the question is not even about code but exchange ideas to do efficiently. Thanks for the help, my thinking is the same as yours. Since the rules are in the comic book, I’ll have to consult

  • @lelopes I don’t know if you search a rule at a time as needed, but if you’re doing this, a performance improvement would be to search all the rules at once and store them in an array or something like that, so you save queries in the bank.

Browser other questions tagged

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