cakephp: Update to 3.6.2 breaks forms validation

This is a (multiple allowed):

  • bug

  • enhancement

  • feature-discussion (RFC)

  • CakePHP Version: 3.6.2

What you did

I have a Form:

class ContactUsForm extends Form
{
    use MailerAwareTrait;

    protected function _buildValidator(Validator $validator)
    {
        // some validation rules...

        return $validator;
    }

    protected function _execute(array $data)
    {
        return $this->getMailer('ContactUs')->send('contactUsMail', [$data]);
    }
}

What happened

Updating from 3.6.1 to 3.6.2, the _buildValidator() method is executed, but all validation rules are ignored.

All tests fail. For example, if I add the rule:

    $validator->requirePresence('first_name');

This test:

    debug($this->ContactUsForm->validate([]));
    dd($this->ContactUsForm->errors());

Turns out:

########## DEBUG ##########
true
###########################
tests/TestCase/Form/ContactUsFormTest.php (line 84)
########## DEBUG ##########
[]
###########################

Instead with the 3.6.1 version everything works correctly.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 22 (22 by maintainers)

Commits related to this issue

Most upvoted comments

Hey folks,

We ran into a kind of similar problem today. I don’t think it’s the same problem (or maybe it is?), but it might help someone who stumbles upon this thread as I did. I was updating our application from 3.5.17 to 3.6.5 and the buildValidator methods were not called anymore, there simply was no validation. I tried around a bit and noticed that it was working in 3.6.1 but stopped working in 3.6.2.

When debugging I noticed that the Form.buildValidator event wasn’t processed properly, because there was no listener for it. That didn’t make much sense, so I checked it and noticed that the event stuff gets initialized in the constructor, which we overwrote without calling parent, because we probably forgot.

Thus I learned that if you overwrite the constructor of Form without calling the parent constructor, your validation will break in versions of CakePHP > 3.6.1.

I think I found the cause of your issue. Form::buildValidator() doesn’t use the return value of Form::_buildValidator().

NVM, createValidator() should still return the new instance since you have $validator = new AppValidator;