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
- Update tests for #12024 Attempt to capture use case of using `_buildValidator` hook to define validators. — committed to cakephp/cakephp by markstory 6 years ago
- Fix up usage. Refs #12024 — committed to cakephp/cakephp by markstory 6 years ago
- added `MeCms\Form\Form` class. This solves issue [12024](https://github.com/cakephp/cakephp/issues/12024) and allows to upgrade CakePHP to a version higher than 3.6.1 — committed to mirko-pagliai/me-cms by mirko-pagliai 6 years ago
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 ofForm::_buildValidator().NVM,
createValidator()should still return the new instance since you have$validator = new AppValidator;