Allow $options['fieldList'] to be mangled/modified inside beforeValidate(). #957

Closed
wants to merge 1 commit into
from

3 participants

@bar

Right now, ModelValidator::errors() triggers ModelValidator::_triggerBeforeValidate() with a copy of $options.

Later, ModelValidator::_triggerBeforeValidate() dispatches an event with those options, but if modified inside Model::beforeValidate() they are never updated inside ModelValidator::errors(), where actual validation occurs.

This is handy when needing to update $fieldList after a Model::save() is triggered.

@lorenzo
CakePHP member

We will need a test or this and one that involves behaviors

@bar

Roger, I'll do that.

@ceeram ceeram commented on the diff Nov 13, 2012
lib/Cake/Model/ModelValidator.php
@@ -247,7 +247,7 @@ public function validateMany(&$data, $options = array()) {
* @see ModelValidator::validates()
*/
public function errors($options = array()) {
- if (!$this->_triggerBeforeValidate($options)) {
+ if (!$this->_triggerBeforeValidate(&$options)) {
@ceeram
CakePHP member
ceeram added a line comment Nov 13, 2012

from: http://php.net/manual/en/language.references.pass.php

There is no reference sign on a function call - only on function definitions.

@bar
bar added a line comment Nov 13, 2012

Thanks ceeram! you are right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@ADmad
CakePHP member

"Call-time pass-by-reference" errors 😄

@bar bar closed this Nov 13, 2012
@bar

This can't be done this way, as @ceeram correctly states, the reference sign should be set in the function definition, and to achieve this, CakeEvent should also pass its $data parameter always as a reference.

Maybe it is "the way" but its more complicated than this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment