mirror of
https://github.com/flarum/core.git
synced 2025-10-13 16:05:05 +02:00
Refactor translation and validation
We now use Symfony's Translation component. Yay! We get more powerful pluralisation and better a fallback mechanism. Will want to implement the caching mechanism at some point too. The API is replicated in JavaScript, which could definitely use some testing. Validators have been refactored so that they are decoupled from models completely (i.e. they simply validate arrays of user input). Language packs should include Laravel's validation messages. ref #267
This commit is contained in:
@@ -10,14 +10,14 @@
|
||||
|
||||
namespace Flarum\Core\Validator;
|
||||
|
||||
use Flarum\Database\AbstractModel;
|
||||
use Flarum\Event\ConfigureModelValidator;
|
||||
use Flarum\Event\ConfigureValidator;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\Contracts\Validation\ValidationException;
|
||||
use Illuminate\Validation\Factory;
|
||||
use Illuminate\Validation\Validator;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
|
||||
class AbstractValidator
|
||||
abstract class AbstractValidator
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
@@ -35,39 +35,63 @@ class AbstractValidator
|
||||
protected $events;
|
||||
|
||||
/**
|
||||
* @param Factory $validator
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
public function __construct(Factory $validator, Dispatcher $events)
|
||||
protected $translator;
|
||||
|
||||
/**
|
||||
* @param Factory $validator
|
||||
* @param Dispatcher $events
|
||||
* @param TranslatorInterface $translator
|
||||
*/
|
||||
public function __construct(Factory $validator, Dispatcher $events, TranslatorInterface $translator)
|
||||
{
|
||||
$this->validator = $validator;
|
||||
$this->events = $events;
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a model is valid.
|
||||
*
|
||||
* @param AbstractModel $model
|
||||
* @param array $attributes
|
||||
* @return bool
|
||||
*/
|
||||
public function valid(AbstractModel $model)
|
||||
public function valid(array $attributes)
|
||||
{
|
||||
return $this->makeValidator($model)->passes();
|
||||
return $this->makeValidator($attributes)->passes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Throw an exception if a model is not valid.
|
||||
*
|
||||
* @throws ValidationException
|
||||
* @param array $attributes
|
||||
*/
|
||||
public function assertValid(AbstractModel $model)
|
||||
public function assertValid(array $attributes)
|
||||
{
|
||||
$validator = $this->makeValidator($model);
|
||||
$validator = $this->makeValidator($attributes);
|
||||
|
||||
if ($validator->fails()) {
|
||||
$this->throwValidationException($validator);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function getRules()
|
||||
{
|
||||
return $this->rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function getMessages()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Validator $validator
|
||||
* @throws ValidationException
|
||||
@@ -80,72 +104,19 @@ class AbstractValidator
|
||||
/**
|
||||
* Make a new validator instance for this model.
|
||||
*
|
||||
* @param AbstractModel $model
|
||||
* @param array $attributes
|
||||
* @return \Illuminate\Validation\Validator
|
||||
*/
|
||||
protected function makeValidator(AbstractModel $model)
|
||||
protected function makeValidator(array $attributes)
|
||||
{
|
||||
$rules = $this->expandUniqueRules($this->rules, $model);
|
||||
$rules = array_only($this->getRules(), array_keys($attributes));
|
||||
|
||||
$validator = $this->validator->make($model->getAttributes(), $rules);
|
||||
$validator = $this->validator->make($attributes, $rules, $this->getMessages());
|
||||
|
||||
$this->events->fire(
|
||||
new ConfigureModelValidator($model, $validator)
|
||||
new ConfigureValidator($this, $validator)
|
||||
);
|
||||
|
||||
return $validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expand 'unique' rules in a set of validation rules into a fuller form
|
||||
* that Laravel's validator can understand.
|
||||
*
|
||||
* @param array $rules
|
||||
* @param AbstractModel $model
|
||||
* @return array
|
||||
*/
|
||||
protected function expandUniqueRules($rules, AbstractModel $model)
|
||||
{
|
||||
foreach ($rules as $attribute => &$ruleset) {
|
||||
if (is_string($ruleset)) {
|
||||
$ruleset = explode('|', $ruleset);
|
||||
}
|
||||
|
||||
foreach ($ruleset as &$rule) {
|
||||
if (strpos($rule, 'unique') === 0) {
|
||||
$rule = $this->expandUniqueRule($attribute, $rule, $model);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expand a 'unique' rule into a fuller form that Laravel's validator can
|
||||
* understand, based on this model's properties.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param string $rule
|
||||
* @param AbstractModel $model
|
||||
* @return string
|
||||
*/
|
||||
protected function expandUniqueRule($attribute, $rule, AbstractModel $model)
|
||||
{
|
||||
$parts = explode(':', $rule);
|
||||
$key = $model->getKey() ?: 'NULL';
|
||||
$rule = 'unique:'.$model->getTable().','.$attribute.','.$key.','.$model->getKeyName();
|
||||
|
||||
if (! empty($parts[1])) {
|
||||
$wheres = explode(',', $parts[1]);
|
||||
|
||||
foreach ($wheres as &$where) {
|
||||
$where .= ','.$this->$where;
|
||||
}
|
||||
|
||||
$rule .= ','.implode(',', $wheres);
|
||||
}
|
||||
|
||||
return $rule;
|
||||
}
|
||||
}
|
||||
|
@@ -13,15 +13,10 @@ namespace Flarum\Core\Validator;
|
||||
class DiscussionValidator extends AbstractValidator
|
||||
{
|
||||
protected $rules = [
|
||||
'title' => ['required', 'max:80'],
|
||||
'start_time' => ['required', 'date'],
|
||||
'comments_count' => ['integer'],
|
||||
'participants_count' => ['integer'],
|
||||
'start_user_id' => ['integer'],
|
||||
'start_post_id' => ['integer'],
|
||||
'last_time' => ['date'],
|
||||
'last_user_id' => ['integer'],
|
||||
'last_post_id' => ['integer'],
|
||||
'last_post_number' => ['integer'],
|
||||
'title' => [
|
||||
'required',
|
||||
'min:3',
|
||||
'max:80'
|
||||
]
|
||||
];
|
||||
}
|
||||
|
@@ -13,14 +13,9 @@ namespace Flarum\Core\Validator;
|
||||
class PostValidator extends AbstractValidator
|
||||
{
|
||||
protected $rules = [
|
||||
'discussion_id' => ['required', 'integer'],
|
||||
'time' => ['required', 'date'],
|
||||
'content' => ['required', 'max:65535'],
|
||||
'number' => ['integer'],
|
||||
'user_id' => ['integer'],
|
||||
'edit_time' => ['date'],
|
||||
'edit_user_id' => ['integer'],
|
||||
'hide_time' => ['date'],
|
||||
'hide_user_id' => ['integer']
|
||||
'content' => [
|
||||
'required',
|
||||
'max:65535'
|
||||
]
|
||||
];
|
||||
}
|
||||
|
@@ -13,12 +13,21 @@ namespace Flarum\Core\Validator;
|
||||
class UserValidator extends AbstractValidator
|
||||
{
|
||||
protected $rules = [
|
||||
'username' => ['required', 'alpha_dash', 'unique', 'min:3', 'max:30'],
|
||||
'email' => ['required', 'email', 'unique'],
|
||||
'password' => ['required'],
|
||||
'join_time' => ['date'],
|
||||
'last_seen_time' => ['date'],
|
||||
'discussions_count' => ['integer'],
|
||||
'posts_count' => ['integer']
|
||||
'username' => [
|
||||
'required',
|
||||
'alpha_dash',
|
||||
'unique:users',
|
||||
'min:3',
|
||||
'max:8'
|
||||
],
|
||||
'email' => [
|
||||
'required',
|
||||
'email',
|
||||
'unique:users'
|
||||
],
|
||||
'password' => [
|
||||
'required',
|
||||
'min:8'
|
||||
]
|
||||
];
|
||||
}
|
||||
|
Reference in New Issue
Block a user