diff --git a/README.md b/README.md index a7b801a..068232b 100644 --- a/README.md +++ b/README.md @@ -182,7 +182,7 @@ There are two ways to override Ardent's validation: `forceSave()` validates the model but saves regardless of whether or not there are validation errors. #### 2. Override Rules and Messages -both `Ardent->save($rules, $customMessages)` and `Ardent->validate($rules, $customMessages)` take two parameters: +both `Ardent->save(null, $rules, $customMessages)` and `Ardent->validate($rules, $customMessages)` take two parameters: - `$rules` is an array of Validator rules of the same form as `Ardent::$rules`. - The same is true of the `$customMessages` parameter (same as `Ardent::$customMessages`) @@ -282,7 +282,7 @@ class User extends \LaravelBook\Ardent\Ardent { `beforeSave` and `afterSave` can be included at run-time. Simply pass in closures with the model as argument to the `save()` (or `forceSave()`) method. ```php -$user->save(array(), array(), array(), +$user->save(null, array(), array(), array(), function ($model) { // closure for beforeSave echo "saving the model object..."; return true; diff --git a/src/LaravelBook/Ardent/Ardent.php b/src/LaravelBook/Ardent/Ardent.php index 271f543..54f8fed 100644 --- a/src/LaravelBook/Ardent/Ardent.php +++ b/src/LaravelBook/Ardent/Ardent.php @@ -484,12 +484,13 @@ protected static function makeValidator($data, $rules, $customMessages) { /** * Validate the model instance * + * @param array $data Data to validate (nullable) * @param array $rules Validation rules * @param array $customMessages Custom error messages * @return bool * @throws InvalidModelException */ - public function validate(array $rules = array(), array $customMessages = array()) { + public function validate(array $data = null, array $rules = array(), array $customMessages = array()) { if ($this->fireModelEvent('validating') === false) { if ($this->throwOnValidation) { throw new InvalidModelException($this); @@ -515,7 +516,12 @@ public function validate(array $rules = array(), array $customMessages = array() $this->fill(Input::all()); } - $data = $this->getAttributes(); // the data under validation + if ($data) { + // if data is specified, fill object with it + $this->fill($data); + } else { + $data = $this->getAttributes(); // the data under validation + } // perform validation $validator = static::makeValidator($data, $rules, $customMessages); @@ -531,7 +537,7 @@ public function validate(array $rules = array(), array $customMessages = array() $this->validationErrors = $validator->messages(); // stash the input to the current session - if (!self::$externalValidator && Input::hasSessionStore()) { + if (!self::$externalValidator && Input::hasSession()) { Input::flash(); } } @@ -549,6 +555,7 @@ public function validate(array $rules = array(), array $customMessages = array() /** * Save the model to the database. Is used by {@link save()} and {@link forceSave()} as a way to DRY code. * + * @param array $data * @param array $rules * @param array $customMessages * @param array $options @@ -560,12 +567,13 @@ public function validate(array $rules = array(), array $customMessages = array() * @see Ardent::save() * @see Ardent::forceSave() */ - protected function internalSave(array $rules = array(), - array $customMessages = array(), - array $options = array(), - Closure $beforeSave = null, - Closure $afterSave = null, - $force = false + protected function internalSave(array $data = null, + array $rules = array(), + array $customMessages = array(), + array $options = array(), + Closure $beforeSave = null, + Closure $afterSave = null, + $force = false ) { if ($beforeSave) { self::saving($beforeSave); @@ -574,7 +582,7 @@ protected function internalSave(array $rules = array(), self::saved($afterSave); } - $valid = $this->validate($rules, $customMessages); + $valid = $this->validate($data, $rules, $customMessages); if ($force || $valid) { return $this->performSave($options); @@ -586,6 +594,7 @@ protected function internalSave(array $rules = array(), /** * Save the model to the database. * + * @param array $data * @param array $rules * @param array $customMessages * @param array $options @@ -595,13 +604,13 @@ protected function internalSave(array $rules = array(), * @return bool * @see Ardent::forceSave() */ - public function save(array $rules = array(), - array $customMessages = array(), - array $options = array(), - Closure $beforeSave = null, - Closure $afterSave = null - ) { - return $this->internalSave($rules, $customMessages, $options, $beforeSave, $afterSave, false); + public function save(array $data = null, + array $rules = array(), + array $customMessages = array(), + array $options = array(), + Closure $beforeSave = null, + Closure $afterSave = null) { + return $this->internalSave($data, $rules, $customMessages, $options, $beforeSave, $afterSave, false); } /**