Skip to content
This repository has been archived by the owner on Jul 16, 2023. It is now read-only.

save accepts Input::all() fixes "sometimes" validator #166

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down Expand Up @@ -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;
Expand Down
43 changes: 26 additions & 17 deletions src/LaravelBook/Ardent/Ardent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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();
}
}
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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);
}

/**
Expand Down