1
0
mirror of https://github.com/flarum/core.git synced 2025-10-09 22:16:51 +02:00

Extract model validation into a trait

Also use Laravel’s ValidationException rather than our own custom one
This commit is contained in:
Toby Zerner
2015-07-05 12:25:08 +09:30
parent 04501545e3
commit 53e269fd89
11 changed files with 193 additions and 234 deletions

View File

@@ -35,13 +35,6 @@ abstract class Model extends Eloquent
*/
protected static $dateAttributes = [];
/**
* The validation rules for this model.
*
* @var array
*/
public static $rules = [];
/**
* An array of custom relation methods, grouped by subclass.
*
@@ -49,124 +42,6 @@ abstract class Model extends Eloquent
*/
protected static $relationMethods = [];
/**
* The validation factory instance.
*
* @var \Illuminate\Contracts\Validation\Factory
*/
protected static $validator;
/**
* Boot the model.
*
* @return void
*/
public static function boot()
{
parent::boot();
// Before the model is saved, validate it. If validation fails, an
// exception will be thrown, preventing the model from saving.
static::saving(function ($model) {
$model->assertValid();
});
}
/**
* Set the validation factory instance.
*
* @param \Illuminate\Contracts\Validation\Factory $validator
*/
public static function setValidator(Factory $validator)
{
static::$validator = $validator;
}
/**
* Check whether the model is valid in its current state.
*
* @return boolean
*/
public function valid()
{
return $this->makeValidator()->passes();
}
/**
* Throw an exception if the model is not valid in its current state.
*
* @return void
*
* @throws \Flarum\Core\ValidationFailureException
*/
public function assertValid()
{
$validator = $this->makeValidator();
if ($validator->fails()) {
$this->throwValidationFailureException($validator);
}
}
protected function throwValidationFailureException($validator)
{
throw (new ValidationFailureException)
->setErrors($validator->errors())
->setInput($validator->getData());
}
/**
* Make a new validator instance for this model.
*
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function makeValidator()
{
$dirty = $this->getDirty();
$rules = $this->expandUniqueRules(array_only(static::$rules, array_keys($dirty)));
// TODO: translation
$messages = [
'unique' => 'That :attribute has already been taken.',
'email' => 'The :attribute must be a valid email address.',
'alpha_num' => 'The :attribute may only contain letters and numbers.'
];
return static::$validator->make($dirty, $rules, $messages);
}
/**
* Expand 'unique' rules in a set of validation rules into a fuller form
* that Laravel's validator can understand.
*
* @param array $rules
* @return array
*/
protected function expandUniqueRules($rules)
{
foreach ($rules as $column => &$ruleset) {
if (is_string($ruleset)) {
$ruleset = explode('|', $ruleset);
}
foreach ($ruleset as &$rule) {
if (strpos($rule, 'unique') === 0) {
$parts = explode(':', $rule);
$key = $this->getKey() ?: 'NULL';
$rule = 'unique:'.$this->getTable().','.$column.','.$key.','.$this->getKeyName();
if (! empty($parts[1])) {
$wheres = explode(',', $parts[1]);
foreach ($wheres as &$where) {
$where .= ','.$this->$where;
}
$rule .= ','.implode(',', $wheres);
}
}
}
}
return $rules;
}
/**
* Get the attributes that should be converted to dates.
*
@@ -216,7 +91,7 @@ abstract class Model extends Eloquent
*
* @throws \LogicException
*/
public function getCustomRelationship($name)
protected function getCustomRelationship($name)
{
$relation = static::$relationMethods[get_called_class()][$name]($this);
@@ -225,7 +100,7 @@ abstract class Model extends Eloquent
. 'Illuminate\Database\Eloquent\Relations\Relation');
}
return $this->relations[$method] = $relation->getResults();
return $this->relations[$name] = $relation->getResults();
}
/**