From 27b364675addf3cb04705b741cd7b2b95ba919cf Mon Sep 17 00:00:00 2001 From: Tommy Bergeron Date: Thu, 6 Feb 2014 16:58:42 -0500 Subject: [PATCH 1/3] Update Ardent.php --- src/LaravelBook/Ardent/Ardent.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LaravelBook/Ardent/Ardent.php b/src/LaravelBook/Ardent/Ardent.php index 271f543..21d1f0b 100644 --- a/src/LaravelBook/Ardent/Ardent.php +++ b/src/LaravelBook/Ardent/Ardent.php @@ -531,7 +531,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(); } } From 674620a0f35fbda69d15a3ed2d6020b717b1dee8 Mon Sep 17 00:00:00 2001 From: Tommy Bergeron Date: Thu, 6 Feb 2014 17:54:13 -0500 Subject: [PATCH 2/3] save accepts Input::all() fixes "sometimes" ->save($data, $rules, $customMessages, $beforeSave, $afterSave); that way the "sometimes" validator will work. --- src/LaravelBook/Ardent/Ardent.php | 41 +++++++++++++++++++------------ 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/src/LaravelBook/Ardent/Ardent.php b/src/LaravelBook/Ardent/Ardent.php index 21d1f0b..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); @@ -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); } /** From cfb6e5914585a26ce6befd3d8a9419038b60840f Mon Sep 17 00:00:00 2001 From: Tommy Bergeron Date: Thu, 6 Feb 2014 17:55:06 -0500 Subject: [PATCH 3/3] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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;