cakephp: Wrong required="required" attribute for requirePresence validation
-
bug
-
CakePHP Version: 4.2
What you did
Code in model-less form or in table:
public function validationDefault(\Cake\Validation\Validator $Validator): \Cake\Validation\Validator {
return parent::validationDefault($Validator)
->requirePresence('fieldname')
;
}
What happened
Input was generated with form helper has required="required" attribute
What you expected to happen
Input is generated with form helper does not have required="required" attribute, because Validator::requirePresence() does not check that field can not be empty, it only checks that field is present in request.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 16 (15 by maintainers)
Commits related to this issue
- Fix for #15699 — committed to andrii-pukhalevych/cakephp by andrii-pukhalevych 10 months ago
It could if someone wanted to put together a pull request for the changes.
Then we should revisit this for 5.x then.
That is odd. I’m reluctant to change the behavior in a bugfix release because it will most likely cause problems in userland applications as previously required fields become no longer required.
I don’t know how we change the behavior and also have it be ‘backwards compatible’ at the same time.
Fields by default are not allowed to be empty. You need to set
allowEmpty()on the field to make it not required.