This commit is contained in:
2023-12-15 18:52:12 +08:00
parent bbe60d9ea2
commit ca6bac85e8
2 changed files with 24 additions and 4 deletions
+3 -1
View File
@@ -51,8 +51,10 @@ class BindForm implements InjectParameterInterface
if ($rule instanceof RequestFilterInterface) { if ($rule instanceof RequestFilterInterface) {
$validator->addRule($property->getName(), $rule->dispatch($object, $property->getName())); $validator->addRule($property->getName(), $rule->dispatch($object, $property->getName()));
} }
if ($rule instanceof Binding) {
$validator->setAlias($attribute->getName(), $rule->field);
}
} }
$typeProxy = $this->_typeValidator($property); $typeProxy = $this->_typeValidator($property);
$validator->addRule($property->getName(), [$typeProxy, false]); $validator->addRule($property->getName(), [$typeProxy, false]);
} }
+21 -3
View File
@@ -40,6 +40,12 @@ class Validator
protected object $formData; protected object $formData;
/**
* @var array<string, string>
*/
protected array $alias = [];
/** /**
* @var array * @var array
*/ */
@@ -71,6 +77,17 @@ class Validator
return $this->formData; return $this->formData;
} }
/**
* @param string $alias
* @param string $property
* @return void
*/
public function setAlias(string $alias, string $property): void
{
$this->alias[$alias] = $property;
}
/** /**
* @param string $name * @param string $name
* @param array $rule * @param array $rule
@@ -107,17 +124,18 @@ class Validator
foreach ($this->rules as $name => $rules) { foreach ($this->rules as $name => $rules) {
/** @var array<array<TypesProxy,string>> $typeValidator */ /** @var array<array<TypesProxy,string>> $typeValidator */
$typeValidator = array_pop($rules); $typeValidator = array_pop($rules);
if (!isset($params[$name])) { $key = $this->alias[$name];
if (!isset($params[$key])) {
if ($rules[0] instanceof RequiredValidatorFilter) { if ($rules[0] instanceof RequiredValidatorFilter) {
return $this->addError('The request field ' . $name . ' is mandatory and indispensable'); return $this->addError('The request field ' . $name . ' is mandatory and indispensable');
} }
if (!$typeValidator[0]->allowsNull) { if (!$typeValidator[0]->allowsNull) {
return $this->addError('The request field ' . $name . ' parameter cannot be null'); return $this->addError('The request field ' . $name . ' parameter cannot be null');
} }
$params[$name] = null; $params[$key] = null;
} }
if (!call_user_func($typeValidator, $this->formData, $name, $params[$name])) { if (!call_user_func($typeValidator, $this->formData, $name, $params[$key])) {
return $this->addError('The parameter type used in the request field ' . $name . ' is incorrect'); return $this->addError('The parameter type used in the request field ' . $name . ' is incorrect');
} }