Skip to content

Commit 7524c8e

Browse files
committed
update some. update readme
1 parent e4ae2b3 commit 7524c8e

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ class PageRequest extends Validation
159159
protected function customValidator($title)
160160
{
161161
// some logic ...
162+
// $this->getRaw('field'); 访问 data 数据
162163

163164
return true; // Or false;
164165
}
@@ -218,7 +219,9 @@ class SomeController
218219
<a name="how-to-use3"></a>
219220
### 方式3: 使用trait `ValidationTrait`
220221

221-
创建一个新的class,并使用 Trait `Inhere\Validate\ValidationTrait`。 此方式是高级自定义的使用方式, 可以方便的嵌入到其他类中
222+
创建一个新的class,并使用 Trait `Inhere\Validate\ValidationTrait`
223+
224+
> 此方式是高级自定义的使用方式, 可以方便的嵌入到其他类中。
222225
223226
如下,嵌入到一个数据模型类中, 实现一个简单的模型基类,添加数据库记录前自动进行验证
224227

@@ -306,18 +309,18 @@ $v = Validation::make($_POST,[
306309
['freeTime', 'number'],
307310
['title', 'checkTitle'],
308311
])
309-
->addValidator('checkTitle',function($title){
312+
->addValidator('checkTitle',function($title) { // 第一个参数是字段值。最后一个参数总是 $data
310313
// some logic ...
311314

312-
return true; // 成功返回 True。 如果验证失败,返回 False.
315+
return true; // 成功返回 True。验证失败,返回 False.
313316
}, '{attr} default message!')
314317
->validate();
315318
```
316319

317320
- **方式3**直接写闭包进行验证 e.g:
318321

319322
```php
320-
['status', function($status) {
323+
['status', function($status) { // 第一个参数是字段值。最后一个参数总是 $data
321324
if ($status > 3) {
322325
return true;
323326
}

src/Utils/ErrorMessageTrait.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@ trait ErrorMessageTrait
155155
* Errors Information
156156
******************************************************************************/
157157

158+
/**
159+
* @return bool
160+
*/
161+
protected function shouldStop(): bool
162+
{
163+
return $this->isFail() && $this->_stopOnError;
164+
}
165+
158166
/**
159167
* 是否有错误
160168
* @return boolean

src/ValidationTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,6 @@ public function validate(array $onlyChecked = [], bool $stopOnError = null)
263263

264264
// fix : deny repeat validate
265265
$this->_validated = true;
266-
267266
return $this;
268267
}
269268

@@ -325,6 +324,7 @@ protected function valueValidate(string $field, $value, $validator, array $args,
325324
{
326325
// if field don't exists.
327326
if (null === $value) {
327+
$this->addError($field, $this->getMessage($validator, $field, $args, $defMsg));
328328
return false;
329329
}
330330

@@ -339,6 +339,7 @@ protected function valueValidate(string $field, $value, $validator, array $args,
339339
$realName = self::getValidatorName($validator);
340340
// if $validator is a custom add callback in the property {@see $_validators}.
341341
if (isset(self::$_validators[$validator])) {
342+
$args[] = $this->data;
342343
$callback = self::$_validators[$validator];
343344
$passed = $callback($value, ...$args);
344345

0 commit comments

Comments
 (0)