Skip to content
Merged
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
17 changes: 9 additions & 8 deletions src/ValidationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,14 @@ protected function applyRule($fields, array $rule, array $onlyChecked)

$value = $this->getByPath($field, $defValue);

// Field value filtering(有通配符`*`的字段, 不应用过滤器)
if ($filters && null !== $value && !\strpos($field, '.*')) {
$value = $this->valueFiltering($value, $filters);
// save
$this->data[$field] = $value;
}

// Field value validate
if (\is_string($validator)) {
if ($validator === 'safe') {
$this->setSafe($field, $value);
Expand All @@ -268,13 +276,6 @@ protected function applyRule($fields, array $rule, array $onlyChecked)
continue;
}

// Field value filtering(有通配符`*`的字段, 不应用过滤器)
if ($filters && !\strpos($field, '.*')) {
$value = $this->valueFiltering($value, $filters);
// save
$this->data[$field] = $value;
}

// Field value verification check
if (!$this->valueValidate($field, $value, $validator, $args, $defMsg) && $this->isStopOnError()) {
break;
Expand Down Expand Up @@ -359,7 +360,7 @@ protected function valueValidate(string $field, $value, $validator, array $args,
// if $validator is a custom method of the subclass.
} elseif (\method_exists($this, $method = $validator . 'Validator')) {
$passed = $this->$method($value, ...$args);
// if $validator is a custom validator {@see $_validators}.
// if $validator is a global custom validator {@see UserValidators::$validators}.
} elseif ($callback = UserValidators::get($validator)) {
$args[] = $this->data;
$passed = $callback($value, ...$args);
Expand Down
53 changes: 29 additions & 24 deletions test/RuleValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,23 +244,27 @@ public function testCollectRules()

public function testValidatePassed()
{
$data = $this->data;
$data = $this->data;
// change value
$data['userId'] = '456';
$rules = [

$rules = [
// ['tagId,userId,freeTime', 'required'],// set message
['tagId,userId,freeTime', 'number', 'filter' => 'int'],
['tagId', 'size', 'max' => 567, 'min' => 4, 'filter' => 'int'], // 4<= tagId <=567
// ['goods', 'isList'],
['goods.pear', 'max', 60],
['insertTime', 'safe']
];
$v = RuleValidation::make($data, $rules)
->setTranslates([
'goods.pear' => '梨子'
])
->setMessages([
'freeTime.required' => 'freeTime is required!!!!'
])
->validate([], false);

$v = RuleValidation::make($data, $rules);
$v->setTranslates([
'goods.pear' => '梨子'
])
->setMessages([
'freeTime.required' => 'freeTime is required!!!!'
])
->validate([], false);

$this->assertTrue($v->isOk());
$this->assertFalse($v->failed());
Expand All @@ -274,25 +278,26 @@ public function testValidateFailed()
{
$rules = $this->someRules();
ob_start();
$v = RuleValidation::make($this->data, $rules)
->setTranslates([
'goods.pear' => '梨子'
])
->setMessages([
'freeTime.required' => 'freeTime is required!!!!'
])
->validate([], false);
$v = RuleValidation::make($this->data, $rules);
$v->setTranslates([
'goods.pear' => '梨子'
])
->setMessages([
'freeTime.required' => 'freeTime is required!!!!'
])
->validate([], false);

$out = ob_get_clean();

$needle = 'use when pre-check';
if (\version_compare(\PHPUnit\Runner\Version::id(), '7.0.0', '<') ) {
if (\version_compare(\PHPUnit\Runner\Version::id(), '7.0.0', '<')) {
$this->assertContains($needle, $out);
} else {
$this->assertStringContainsString($needle, $out);
}

$needle = 'use custom validate';
if (\version_compare(\PHPUnit\Runner\Version::id(), '7.0.0', '<') ) {
if (\version_compare(\PHPUnit\Runner\Version::id(), '7.0.0', '<')) {
$this->assertContains($needle, $out);
} else {
$this->assertStringContainsString($needle, $out);
Expand Down Expand Up @@ -478,10 +483,10 @@ public function testRange()
['num', 'range', 'min' => 1, 'max' => 100],
['id', 'range', 'min' => 1, 'max' => 100],
])
->setMessages([
'id.range' => 'range error message',
])
->validate();
->setMessages([
'id.range' => 'range error message',
])
->validate();

$this->assertFalse($v->isOk());
$this->assertCount(1, $v->getErrors());
Expand Down
57 changes: 57 additions & 0 deletions test/ValidationTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Inhere\ValidateTest;

use Inhere\Validate\Validation;
use Inhere\Validate\ValidationTrait;
use PHPUnit\Framework\TestCase;

/**
* Class ValidationTraitTest
* @package Inhere\ValidateTest
*/
class ValidationTraitTest extends TestCase
{
public function testNoDataProperty()
{
$v = new class {
use ValidationTrait;
};

// want data property
$this->expectException(\InvalidArgumentException::class);
$v->validate();

$v = Validation::check(['name' => 'inhere'], [
['name', 'requred']
]);
}

public function testBeforeAndAfter()
{
$v = Validation::make(['name' => 'inhere'], [
['name', 'string', 'min' => 3, 'filter' => 'trim|upper']
]);

$v->onBeforeValidate(function (Validation $v) {
$this->assertSame('inhere', $v->getRaw('name'));
$this->assertNull($v->getSafe('name'));

return true;
});

$v->onAfterValidate(function (Validation $v) {
$this->assertSame('INHERE', $v->getRaw('name'));
$this->assertSame('INHERE', $v->getSafe('name'));
});

$v->validate();

$this->assertTrue($v->isOk());
$this->assertTrue($v->isValidated());

$v->validate();

$this->assertTrue($v->isValidated());
}
}