Wednesday, November 12, 2014

Re: cake3 validation rule not performed

SOLVED!!!, Thanks José Lorenzo

public function login() {
        if ($this->request->is('post')) {
            $user = $this->Users->newEntity($this->request->data);
            if ($this->Users->validate($user)) {
                $auth = $this->Auth->identify();
                if ($auth) {
                    $this->Auth->setUser($auth);
                    return $this->redirect($this->Auth->redirectUrl());
                }
                $this->Flash->error(__('Invalid username or password.'));
            }
            $this->Flash->error(__('Please correct the errors below.'));
        }
        $this->set(compact('user'));
    }



El miércoles, 12 de noviembre de 2014 12:13:25 UTC-2, cesar calvo escribió:
Perhaps reusable validators is one solution.

Why username in login form not validates email rule? Presence and notEmpty are validates and show related message but email rule is not applied!. I think this only work on create or update context.

//model
return $validator
            ->validatePresence('username')
            ->notEmpty('username', __('This field is required.'))
            ->add('username', [
                'valid' => [
                    'rule' => 'email',
                    'message' => __('This field requires a valid email address.')
                ]
            ]);

//controller
if ($this->request->is('post')) {
            $user = $this->Auth->identify();
            if ($user) {
                $this->Auth->setUser($user);
                return $this->redirect($this->Auth->redirectUrl());
            }
            $this->Flash->error(__('Invalid username or password.'));
        }
        $this->set('user', $this->Users->newEntity());

//view
    $this->Form->create($user),
    $this->Form->input('username', ['label' => __('Username'), 'placeholder' => __('E-mail address'),]),
    $this->Form->input('password', ['label' => __('Password'), 'value' => false]),
    $this->Form->button(__('Submit')),
    $this->Form->end()

El miércoles, 12 de noviembre de 2014 11:23:59 UTC-2, José Lorenzo escribió:
Then use a validator in the controller instead of validating how data looks like in the table. Validation in the table is meant for data integrity, not so much how data looks like.

On Wednesday, November 12, 2014 1:06:25 PM UTC+1, cesar calvo wrote:
Indeed I have _setPassword in the Entity.

protected function _setPassword($password) {
        if (!empty($password)) {
            return (new DefaultPasswordHasher)->hash($password);
        }
        return $password;
    }

El miércoles, 12 de noviembre de 2014 09:56:57 UTC-2, José Lorenzo escribió:
If you have a _setPassword() function in your entity, that function will be called before validating the data. That means that the length of the password will be much larger than the original passed password. In those cases it is better to not validate using the table, but create a validator in the controller and validate the data that way.

On Wednesday, November 12, 2014 12:27:22 PM UTC+1, cesar calvo wrote:
The login form is very similar to that used in the blog tutorial.
I can see that there is no link to the LOGIN form with associated model.
ADD and EDIT actions perform validation over username but not over password field.
With "ignore" I mean that the model validation rules do not apply.

public function login() {
        if ($this->request->is('post')) {
            $user = $this->Auth->identify();
            if ($user) {
                $this->Auth->setUser($user);
                return $this->redirect($this->Auth->redirectUrl());
            }
            $this->Flash->error(__('Invalid username or password.'));
        }
    }

public function add() {
        $user = $this->Users->newEntity($this->request->data);
        if ($this->request->is('post')) {
            if ($this->Users->save($user)) {
                //mail connection...
                $this->Flash->success(__('A message has been sent to your email address.'));
                return $this->redirect(['action' => 'login']);
            }
            $this->Flash->error(__('Unable to save your data.'));
        }
        $this->set(compact('user'));
    }

public function edit() {
        $user = $this->Users->get($this->Auth->user('id'));
        if ($this->request->is(['post', 'put'])) {
            $this->Users->patchEntity($user, ['password' => $this->request->data('password')]);
            if ($this->Users->save($user)) {
                $this->Flash->success(__('Your data has been updated.'));
                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('Unable to update your data.'));
        }
        $this->set(compact('user'));
    }

//login.ctp
<?=
    $this->Form->create(),
    $this->Form->input('username', ['label' => __('Username'), 'required' => true, 'placeholder' => __('E-mail address'),]),
    $this->Form->input('password', ['label' => __('Password'), 'required' => true, 'value' => false]),
    $this->Form->button(__('Submit')),
    $this->Form->end()
?>

//add.ctp
<?=
    $this->Form->create($user),
    $this->Form->input('username', ['label' => __('Username'), 'placeholder' => __('E-mail address')]),
    $this->Form->input('password', ['label' => __('Password'), 'value' => false]),
    $this->Form->button(__('Submit')),
    $this->Form->end()
?>

//edit.ctp
<?=
    $this->Form->create($user),
    $this->Form->input('username', ['label' => __('Username'), 'disabled' => true]),
    $this->Form->input('password', ['label' => __('Password'), 'value' => false]),
    $this->Form->button(__('Submit')),
    $this->Form->end();
?>

El miércoles, 12 de noviembre de 2014 07:30:41 UTC-2, José Lorenzo escribió:
What do you mean with "the validator ignores?" What is the data that you are trying to validate and what is the result?

On Tuesday, November 11, 2014 5:03:19 PM UTC+1, cesar calvo wrote:
Hi people, I have this validation rules in UsersTable.
The validator ignores the ->add(password, lenght) sentence.
The issue is when I try to create or update a record.

Best regards --cesar

public function validationDefault(Validator $validator) {
        return $validator
            ->validatePresence('username', 'create')
            ->notEmpty('username', __('This field is required.'))
            ->add('username', [
                'valid' => [
                    'rule' => 'email',
                    'message' => __('This field requires a valid email address.')
                ],
                'unique' => [
                    'rule' => 'validateUnique',
                    'provider' => 'table',
                    'message' => __('This field must be unique.')
                ]
            ])
            ->validatePresence('password', 'create')
            ->notEmpty('password', __('This field is required.'))
            ->add('password', [
                'length' => [
                    'rule' => ['minLength', 8],
                    'message' => __('Password must be at least {0} characters long.', 8),
                ]
            ])
        ;
    }

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: