Skip to content

Commit

Permalink
增加 neq(<>) 规则(验证是否不等于某个值)
Browse files Browse the repository at this point in the history
  • Loading branch information
big-dream authored and liu21st committed Oct 15, 2024
1 parent 9a82e1e commit 20d5fb4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/lang/zh-cn.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
':attribute must less than or equal :rule' => ':attribute必须小于等于 :rule',
':attribute must less than :rule' => ':attribute必须小于 :rule',
':attribute must equal :rule' => ':attribute必须等于 :rule',
':attribute must not be equal to :rule' => ':attribute必须不等于 :rule',
':attribute has exists' => ':attribute已存在',
':attribute not conform to the rules' => ':attribute不符合指定规则',
':attribute must multiple :rule' => ':attribute必须是 :rule 的倍数',
Expand Down
15 changes: 14 additions & 1 deletion src/think/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Validate
* @var array
*/
protected $alias = [
'>' => 'gt', '>=' => 'egt', '<' => 'lt', '<=' => 'elt', '=' => 'eq', 'same' => 'eq',
'>' => 'gt', '>=' => 'egt', '<' => 'lt', '<=' => 'elt', '=' => 'eq', 'same' => 'eq', '<>' => 'neq',
];

/**
Expand Down Expand Up @@ -110,6 +110,7 @@ class Validate
'elt' => ':attribute must less than or equal :rule',
'lt' => ':attribute must less than :rule',
'eq' => ':attribute must equal :rule',
'neq' => ':attribute must not be equal to :rule',
'unique' => ':attribute has exists',
'regex' => ':attribute not conform to the rules',
'method' => 'invalid Request method',
Expand Down Expand Up @@ -866,6 +867,18 @@ public function eq($value, $rule): bool
return $value == $rule;
}

/**
* 验证是否不等于某个值
* @access public
* @param mixed $value 字段值
* @param mixed $rule 验证规则
* @return bool
*/
public function neq($value, $rule): bool
{
return $value != $rule;
}

/**
* 必须验证
* @access public
Expand Down
16 changes: 16 additions & 0 deletions tests/ValidateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,20 @@ public function testMultipleOf()
$this->assertFalse($validate->multipleOf('4', 3));

}

/**
* 关系运算规则
* @return void
*/
public function testRelationalOperator()
{
$validate = new Validate();

$this->assertTrue($validate->gt('2', 1)); // >
$this->assertTrue($validate->egt('1', 1)); // >=
$this->assertTrue($validate->lt('1', 2)); // <
$this->assertTrue($validate->elt('1', 1)); // <=
$this->assertTrue($validate->eq('1', 1)); // ==
$this->assertTrue($validate->neq('1', 0)); // <>
}
}

0 comments on commit 20d5fb4

Please sign in to comment.