cakephp / app

CakePHP application template
373 stars 393 forks source link

Bug. E-mail validation rules #516

Closed Thiagocfn closed 7 years ago

Thiagocfn commented 7 years ago

This is a (multiple allowed):

What you did

Tryed to test a e-mail validation.

on entity table class added following `validations:

public function validationDefault(Validator $validator)
    {
        ...
        $user_email = __("Informe o e-mail de acesso."); //translation: Please, type your access e-mail
        $validator
            ->requirePresence('user_email', 'create', $user_email)
            ->email('user_email', __("É necessário ser um e-mail válido.")) //translation: It must be a valid e-mail
            ->notEmpty('user_email', $user_email);
        ...
    }

there's no other validation rules to this field.

on test there is:

public function testRegister()
    {
        $data = [
            'user_name' => "Alexander Oliveira",
            'user_email' => 'alexander.oliveira@microsffer.com',
            'user_telefone' => "(00) 000000000",
            "user_pass" => "p455w0rd",
            "user_pass_confirm" => "p455w0rd",
            "partner" => [
                "empresa" => [
                    "cnpj" => "23.541.675/0001-43",
                    "uf" => "RJ",
                    "razao_social" => "Razão Social S.A."
                ]
            ]
        ];
        $this->post('/user/register', $data);
        echo $this->_response->body();
    ...
    }

On register action:

public function register()
    {
        $user = $this->User->newEntity();
        if ($this->request->is('post')) {
       .....

                $user = $this->User->patchEntity($user, $this->request->data, ['validate' => 'register', 'associated' => ['Partners' => ['associated' => ['Empresas' => ['validate' => 'register']]]]]);
                //if (!$user->errors()) {
                if ($this->User->save($user)) {
                    $this->_removeProtecteds($user);
                    $this->set(compact('user'));
                    $this->set('_serialize', ['user']);
                } else {
                    $this->response->statusCode(400);
                    $errors = $user->errors();
                    $this->_removeProtecteds($user);
                    $this->set(compact('errors', 'user'));
                    $this->set('_serialize', ['user', 'errors']);
                }
            }
        }
    }

response body:

{
    "user": {
        "user_name": ""Alexander Oliveira",
        "user_telefone": "(00) 000000000",
        "partner": {
            "empresa": {
                "cnpj": "23541675000143",
                "uf": "RJ",
                "razao_social": "Raz\u00e3o Social S.A."
            },
            "total_licencas_count": 0
        },
        "enabled": true,
        "user_type": 2,
        "tipo": "ADMIN"
    },
    "errors": {
        "user_email": {
            "email": "The provided value is invalid"
        }
    }
}

What happened

On run that test, we receive an generic error about validation e-mail: "The provided value is invalid". But there'is no other validation rules. No field manipulation.

e-mails that i received success: thiago@silva.com.br //fake, but well formed thiago@aaa.com.br //fake, but well formed e-mails failed and received generic message error: alexander.oliveira@microsffer.com //real e-mail thiago@sss.com.br //fake, but well formed

What you expected to happen

I expect to validade any well formed e-mails, as well receive one of my custom messages that i had specified on validation rules, in case of failure.

I guess there is a bug or technical uncompliance with e-mail validation rules.

cleptric commented 7 years ago

The signature of the Validator::email methods looks like that: public function email($field, $checkMX = false, $message = null, $when = null)

So you have to add your message as the third parameter.

Also, this is the wrong repo for this kind of issues. Please head over to https://github.com/cakephp/cakephp the next time 😄

Thiagocfn commented 7 years ago

Not a bug, just a misunderstanding. Note: all simple validation have a pattern: $validator ->{{validation}}('field', 'create|update\callback', 'message') or $validator ->{{validation}}('field', 'message')

but the validation e-mail structure is: $validator ->email('field', 'checkMX: bool','message or null', 'when or null')

how we can se on API https://api.cakephp.org/3.0/class-Cake.Validation.Validation.html#_email

But there is no information about on cookbook in any mention about e-mail validation https://book.cakephp.org/3.0/en/core-libraries/validation.html

I recommend add that mention and/or adjust the structure of e-mail validation to be intuitive and be similar than other ones.

Thiagocfn commented 7 years ago

@cleptric thanx. i'm was writting just when you wrote that message.

I appreciate your answer and ask you to take some note to my previous considerations and why i made that mistake. I'll write in the right section at next time if i find other problem.

Thank you

cleptric commented 7 years ago

https://api.cakephp.org/3.0/class-Cake.Validation.Validation.html#_email is the wrong class. This is the generic validation class. You're looking for https://api.cakephp.org/3.4/class-Cake.Validation.Validator.html#_email

Thiagocfn commented 7 years ago

@cleptric Thank you, and I apologize for misunderstood that. But take a note to that different structure and no mention about that on "cookbook" email validation usage,

Regards.

cleptric commented 7 years ago

No problem at all 🙂 I'll take a look at the docs and see how we can improve this.