Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added some rules for hyperf/validation #6756

Closed
wants to merge 11 commits into from
2 changes: 1 addition & 1 deletion CHANGELOG-3.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

- [#6734](https://github.com/hyperf/hyperf/pull/6734) Auto complete options for as command and closure command.
- [#6746](https://github.com/hyperf/hyperf/pull/6746) Added `explain()` for `Hyperf\Database\Query\Builder`.
- [#6749](https://github.com/hyperf/hyperf/pull/6749) Added some rules for `hyperf/validation`.
- [#6749](https://github.com/hyperf/hyperf/pull/6749) [#6756](https://github.com/hyperf/hyperf/pull/6756) Added some rules for `hyperf/validation`.
- [#6752](https://github.com/hyperf/hyperf/pull/6752) Added `path` and `paths` methods to `Hyperf\Database\Seeders\Seed`.

# v3.1.21 - 2024-05-09
Expand Down
9 changes: 9 additions & 0 deletions src/validation/publish/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
'ipv4' => 'The :attribute must be a valid IPv4 address.',
'ipv6' => 'The :attribute must be a valid IPv6 address.',
'json' => 'The :attribute must be a valid JSON string.',
'list' => 'The :attribute field must be a list.',
'lt' => [
'numeric' => 'The :attribute must be less than :value',
'file' => 'The :attribute must be less than :value kb',
Expand Down Expand Up @@ -101,6 +102,14 @@
'not_regex' => 'The :attribute cannot match a given regular rule.',
'numeric' => 'The :attribute must be a number.',
'present' => 'The :attribute field must be present.',
'present_if' => 'The :attribute field must be present when :other is :value.',
'present_unless' => 'The :attribute field must be present unless :other is :value.',
'present_with' => 'The :attribute field must be present when :values is present.',
'present_with_all' => 'The :attribute field must be present when :values are present.',
'prohibited' => 'The :attribute field is prohibited.',
'prohibited_if' => 'The :attribute field is prohibited when :other is :value.',
'prohibited_unless' => 'The :attribute field is prohibited unless :other is in :values.',
'prohibits' => 'The :attribute field prohibits :other from being present.',
'regex' => 'The :attribute format is invalid.',
'required' => 'The :attribute field is required.',
'required_if' => 'The :attribute field is required when :other is :value.',
Expand Down
9 changes: 9 additions & 0 deletions src/validation/publish/zh_CN/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
'ipv4' => ':attribute 必须是一个合法的 IPv4 地址',
'ipv6' => ':attribute 必须是一个合法的 IPv6 地址',
'json' => ':attribute 必须是一个合法的 JSON 字符串',
'list' => ':attribute 必须是一个列表',
'lt' => [
'numeric' => ':attribute 必须小于 :value',
'file' => ':attribute 必须小于 :value kb',
Expand Down Expand Up @@ -101,6 +102,14 @@
'not_regex' => ':attribute 不能匹配给定的正则',
'numeric' => ':attribute 必须是数字',
'present' => ':attribute 字段必须存在',
'present_if' => '当 :other 为 :value 时,:attribute 必须存在。',
'present_unless' => '除非 :other 为 :value,否则 :attribute 必须存在。',
'present_with' => '存在 :values 时,:attribute 必须存在。',
'present_with_all' => '出现 :values 时,:attribute 必须存在。',
'prohibited' => '禁止使用 :attribute',
'prohibited_if' => '当 :other 为 :value 时,禁止使用 :attribute',
'prohibited_unless' => '除非 :other 在 :values 中,否则禁止使用 :attribute',
'prohibits' => '该 :attribute 禁止 :other 出现',
'regex' => ':attribute 格式是无效的',
'required' => ':attribute 字段是必须的',
'required_if' => ':attribute 字段是必须的当 :other 是 :value',
Expand Down
194 changes: 194 additions & 0 deletions src/validation/src/Concerns/ReplacesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@

trait ReplacesAttributes
{
/**
* Replace all place-holders for the required_if_declined rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array<int,string> $parameters
* @return string
*/
public function replaceRequiredIfDeclined($message, $attribute, $rule, $parameters)
{
$parameters[0] = $this->getDisplayableAttribute($parameters[0]);

return str_replace([':other'], $parameters, $message);
}

/**
* Replace all place-holders for the between rule.
*/
Expand Down Expand Up @@ -130,6 +146,68 @@ protected function replaceMimes(string $message, string $attribute, string $rule
return str_replace(':values', implode(', ', $parameters), $message);
}

/**
* Replace all place-holders for the present_if rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array<int,string> $parameters
* @return string
*/
protected function replacePresentIf($message, $attribute, $rule, $parameters)
{
$parameters[1] = $this->getDisplayableValue($parameters[0], Arr::get($this->data, $parameters[0]));
$parameters[0] = $this->getDisplayableAttribute($parameters[0]);

return str_replace([':other', ':value'], $parameters, $message);
}

/**
* Replace all place-holders for the present_unless rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array<int,string> $parameters
* @return string
*/
protected function replacePresentUnless($message, $attribute, $rule, $parameters)
{
return str_replace([':other', ':value'], [
$this->getDisplayableAttribute($parameters[0]),
$this->getDisplayableValue($parameters[0], $parameters[1]),
], $message);
}

/**
* Replace all place-holders for the present_with rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array<int,string> $parameters
* @return string
*/
protected function replacePresentWith($message, $attribute, $rule, $parameters)
{
return str_replace(':values', implode(' / ', $this->getAttributeList($parameters)), $message);
}

/**
* Replace all place-holders for the present_with_all rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array<int,string> $parameters
* @return string
*/
protected function replacePresentWithAll($message, $attribute, $rule, $parameters)
{
return $this->replacePresentWith($message, $attribute, $rule, $parameters);
}

/**
* Replace all place-holders for the required_with rule.
*/
Expand Down Expand Up @@ -337,4 +415,120 @@ protected function replaceStartsWith(string $message, string $attribute, string

return str_replace(':values', implode(', ', $parameters), $message);
}

/**
* Replace all place-holders for the missing_if rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array<int,string> $parameters
* @return string
*/
protected function replaceMissingIf($message, $attribute, $rule, $parameters)
{
$parameters[1] = $this->getDisplayableValue($parameters[0], Arr::get($this->data, $parameters[0]));

$parameters[0] = $this->getDisplayableAttribute($parameters[0]);

return str_replace([':other', ':value'], $parameters, $message);
}

/**
* Replace all place-holders for the missing_unless rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array<int,string> $parameters
* @return string
*/
protected function replaceMissingUnless($message, $attribute, $rule, $parameters)
{
return $this->replaceMissingIf($message, $attribute, $rule, $parameters);
}

/**
* Replace all place-holders for the missing_with rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array<int,string> $parameters
* @return string
*/
protected function replaceMissingWith($message, $attribute, $rule, $parameters)
{
return str_replace(':values', implode(' / ', $this->getAttributeList($parameters)), $message);
}

/**
* Replace all place-holders for the missing_with_all rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array<int,string> $parameters
* @return string
*/
protected function replaceMissingWithAll($message, $attribute, $rule, $parameters)
{
return $this->replaceMissingWith($message, $attribute, $rule, $parameters);
}

/**
* Replace all place-holders for the prohibited_if rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array<int,string> $parameters
* @return string
*/
protected function replaceProhibitedIf($message, $attribute, $rule, $parameters)
{
$parameters[1] = $this->getDisplayableValue($parameters[0], Arr::get($this->data, $parameters[0]));

$parameters[0] = $this->getDisplayableAttribute($parameters[0]);

return str_replace([':other', ':value'], $parameters, $message);
}

/**
* Replace all place-holders for the prohibited_unless rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array<int,string> $parameters
* @return string
*/
protected function replaceProhibitedUnless($message, $attribute, $rule, $parameters)
{
$other = $this->getDisplayableAttribute($parameters[0]);

$values = [];

foreach (array_slice($parameters, 1) as $value) {
$values[] = $this->getDisplayableValue($parameters[0], $value);
}

return str_replace([':other', ':values'], [$other, implode(', ', $values)], $message);
}

/**
* Replace all place-holders for the required_if_accepted rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array<int,string> $parameters
* @return string
*/
protected function replaceRequiredIfAccepted($message, $attribute, $rule, $parameters)
{
$parameters[0] = $this->getDisplayableAttribute($parameters[0]);

return str_replace([':other'], $parameters, $message);
}
}