0

I have problem with validations in CakePHP 3. I preapre some code with validation for email field, like this:

    $validator
        ->email('email')
        ->notEmpty('email')
        ->add('email', 'correct',[
            'rule' => [
                'custom', '/@example.com$/i'
            ],
            'message' => 'Not correct...'
        ]);

form view:

<?php
    echo $this->Form->create('Users', [
        'novalidate' => true
    ]);
?>
    <fieldset>
        <?php
            echo $this->Form->control('email', [
                'label' => __d('admin', 'E-mail')
            ]);
        ?>
    </fieldset>
    <?php echo $this->Form->button(__d('admin', 'Send')); ?>
<?php echo $this->Form->end(); ?>

and controller content:

    if ($this->request->is('post')) {
        $user = $this->Users->find()->select([
            'Users.' . $this->Users->getPrimaryKey()
        ])->where([
            'Users.email' => $this->request->getData('email')
        ])->first();

        if (!is_null($user)) {
            $this->Users->patchEntity($user, $this->request->getData());

            $user->uuid = 'random string...';

            if (empty($user->errors())) {
                if ($this->Users->save($user)) {
                    $this->Flash->success(__d('admin', 'Saved...'));
                } else {
                    $this->Flash->error(__d('admin', 'Not saved...'));
                }
            } else {
                $this->Flash->error(__d('admin', 'Form validation...'));

                pr($user->errors()); // Is not empty...
            }
        } else {
            $this->Flash->error(__d('admin', 'E-mail doesn't exists...'));
        }
    }

Form and controller action works fine, but there aren't show validation messages below form email field, why?

kicaj
  • 2,881
  • 5
  • 42
  • 68
  • Hard to tell without seeing the rest of the action and the form creation code. – ndm Jul 09 '17 at 17:07
  • @ndm This is the full code of action, what would like see more? – kicaj Jul 09 '17 at 18:00
  • 1
    Ideally the form creation code as mentioned, however if that is all the contrroller action code, then I can imagine what it looks like, you're probably not passing the entity to the form, as your controller action doesn't send it to the view in the first place!? – ndm Jul 09 '17 at 19:08
  • @ndm I added form view. What do You mean "passing entity to form"? – kicaj Jul 09 '17 at 19:27
  • 1
    Gotta fly... check the first section of the form helper docs: **https://book.cakephp.org/3.0/en/views/helpers/form.html** – ndm Jul 09 '17 at 20:02
  • Thanks @ndm, there was no $context in create method of Form Helper! – kicaj Jul 10 '17 at 07:04

0 Answers0