Wednesday, November 12, 2014

Re: cake3 validation rule not performed

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: