pass all the options of save also to validates #563

Closed
wants to merge 1 commit into from
View
9 data/Model.php
@@ -788,6 +788,8 @@ public static function instanceMethods(array $methods = null) {
* method if `'validate'` is not `false`.
* - `'whitelist'` _array_: An array of fields that are allowed to be saved to this
* record.
+ * - `'validationOptions'` _array_: Options to pass through to `validates()`,
+ * in addition to `'validate'` and `'events'`.
*
* @return boolean Returns `true` on a successful save operation, `false` on failure.
* @filter
@@ -802,7 +804,8 @@ public function save($entity, $data = null, array $options = array()) {
'events' => $entity->exists() ? 'update' : 'create',
'whitelist' => null,
'callbacks' => true,
- 'locked' => $self->_meta['locked']
+ 'locked' => $self->_meta['locked'],
+ 'validationOptions' => array()
);
$options += $defaults;
$params = compact('entity', 'data', 'options');
@@ -816,7 +819,9 @@ public function save($entity, $data = null, array $options = array()) {
}
if ($rules = $options['validate']) {
$events = $options['events'];
- $validateOpts = is_array($rules) ? compact('rules','events') : compact('events');
+ $validateOpts = is_array($rules) ? compact('rules') : array();
+ $validateOpts += $events ? compact('events') : array();
@nateabele
Union of RAD member

Okay, last thing, I promise. :-) This is a really minor nitpick, but since $events will never be empty, it don't think it makes sense to have a ternary for it. Functionally, I don't see how these two lines of code are any different from the one line you're replacing.

Unless there's a reason for it which I'm not seeing, I'd say revert the two lines above. Then squash your commits and we'll get this merged in finally. :-P Thanks again for your work on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ $validateOpts += $options['validationOptions'];
if (!$entity->validates($validateOpts)) {
return false;
}