Help with error messages on a validation class

Hello all,

I would like to have your help in order to find the simplest way, to deal with this validation error messages.

I know that we could have an abstract validation class and then have the string validation class and numeric validation class etc… I know we can do thinks much better, but I just need to understand how do deal with the error messages on this case.

Here is what I have:

class Validacao
{

    private $_msgErro=array();


    public function limpaString($campoForm)
    {
        if (!empty($campoForm))
        {
            $campoForm = filter_var($campoForm, FILTER_SANITIZE_STRING);
        }
    }

    public function validaNumero($campoForm)
    {
        if (!empty($campoForm))
        {
            if(filter_var($campoForm, FILTER_VALIDATE_INT) === false)
            {
               $this->_msgErro[$campoForm] .= ' Introduza um valor inteiro válido. ';
            }
        }
    }

    public function validaStringMinMax($campoForm, $min, $max)
    {
        if (!empty($campoForm))
        {
            //se o campo for menor que o mínimo ou maior que o máximo
            if ((strlen($campoForm) < $min) || (strlen($campoForm) > $max))
            {
                //echo' Tenha em atenção a dimensão daquilo que quer introduzir.';
               //$this->_msgErro[$campoForm] .= 'Insere entre: '.$min.' e '$max;
            }
        }
    }

    public function requerido($campoForm)
    {
        if ($campoForm=="")
        {
            if (isset($this->_msgErro[$campoForm]))
            {
               $this->_msgErro[$campoForm] .= ' Campo obrigatório. ';
            }
            else
            {
               $this->_msgErro[$campoForm] = ' Campo obrigatório. ';
            }
        }
    }

    public function validaEmail($campoForm)
    {
        if (!preg_match("^[a-zA-Z0-9_\\.\\-]+@[a-zA-Z0-9_\\.\\-]*[a-zA-Z0-9_\\-]+\\.[a-zA-Z]{2,4}$^", $campoForm))
        {
           $this->_msgErro[$campoForm] = 'O e-mail não é válido.';
        }
    }

    public function errosValidacao()
    {
        return $this->_msgErro;
    }

   
}

Usage:
If I want to validate and clean a string field that is required and other things, something like this is been used:


//limpeza e validacao:
    $val = new Validacao();

    $val->requerido($tituloNoticia);
    $val->limpaString($tituloNoticia);
    $val->validaStringMinMax($tituloNoticia, 3, 84);

Then I use the last method on this class to check if there are errors or not:

if (!$val->errosValidacao())
    {
        try
        {

If they exist, on my form I would have something like this near the form field:

<?php echo (isset($val->errosValidacao('tituloNoticia'))) ? '<br /><span class="mensagem-erro-ss">'.$val->errosValidacao('tituloNoticia').'</span><br />':''; ?>

My issue is that I cannot find a way to display the error messages to the user.

I have tried to change errosValidacao() method on several ways already, by adding $campoForm as argument, or to return $this->_msgErro[‘campoForm’], but I’m getting no luck, always getting either undefined index or undefined variable notices.

Can I have a push?

Thanks in advance,
Márcio

Now that I look at it again, it really makes sense. :slight_smile: I will use it right now.
And the funny part is that… well… I actually wrote it, but you have explain me how should I use what I wrote. :eek:

Oh well…

:slight_smile:

I see…

So instead of doing:
$val->errosValidacao(‘tituloNoticia’); that obviously will not work because we don’t pass any parameter to our errosValidacao method (at least it’s not defined on the class), we should grab the array and stored in a variable:

$errors = $val->errosValidacao();

And then, we will get our error message by accessing our array on a given key, were that key is $errors[$tituloNoticia]; for a given field etc…

thanks for your help. I’m unable to verify right now, but It makes sense now that you have point it.

k. Regards,
Márcio

The errors are stored as an array, so retrieve them out, and then access the error message:

$errors = $val->errosValidacao();
$error = $errors[$tituloNoticia];