Skip to content

Commit dc79b61

Browse files
author
zhouweiphp
committed
加入default默认值设置,以及获取验证成功的参数
1 parent 40837c4 commit dc79b61

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

src/Rules/Defaults.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Rakit\Validation\Rules;
4+
5+
use Rakit\Validation\Rule;
6+
7+
class Defaults extends Rule
8+
{
9+
10+
protected $message = "The :attribute default is :default";
11+
12+
protected $fillable_params = ['default'];
13+
14+
public function check($value)
15+
{
16+
$this->requireParameters($this->fillable_params);
17+
18+
$default = $this->parameter('default');
19+
return $default;
20+
}
21+
22+
}

src/Validation.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Rakit\Validation\Rules\Required;
66
use Closure;
7+
use Rakit\Validation\Rules\Defaults;
78

89
class Validation
910
{
@@ -19,6 +20,8 @@ class Validation
1920
protected $aliases = [];
2021

2122
protected $messageSeparator = ':';
23+
24+
protected $validata = [];
2225

2326
public function __construct(Validator $validator, array $inputs, array $rules, array $messages = array())
2427
{
@@ -73,6 +76,12 @@ protected function validateAttribute(Attribute $attribute)
7376
$isEmptyValue = $this->isEmptyValue($value);
7477

7578
foreach($rules as $ruleValidator) {
79+
if ($isEmptyValue && $ruleValidator instanceof Defaults) {
80+
$default = $ruleValidator->check(null);
81+
$this->validata[$attributeKey] = $default;
82+
} else {
83+
$this->validata[$attributeKey] = $value;
84+
}
7685
if ($isEmptyValue AND $this->ruleIsOptional($attribute, $ruleValidator)) {
7786
continue;
7887
}
@@ -421,5 +430,9 @@ protected function resolveInputAttributes(array $inputs)
421430

422431
return $resolvedInputs;
423432
}
433+
434+
public function getValidata() {
435+
return $this->validata;
436+
}
424437

425438
}

src/Validator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ protected function registerBaseValidators()
104104
'callback' => new Rules\Callback,
105105
'before' => new Rules\Before,
106106
'after' => new Rules\After,
107+
'defaults' => new Rules\Defaults,
107108
];
108109

109110
foreach($baseValidator as $key => $validator) {

0 commit comments

Comments
 (0)