我正在将symfony4用于Web应用程序。我成功使用validation component来验证IP地址。但是在尝试使用我自己的自定义约束时,按照以下指南,我出现错误无法访问私有属性App \ Entity \ Terminal :: $ name :https://symfony.com/doc/current/validation/custom_constraint.html
我要验证的实体:src / Entity / Terminal.php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use App\Validator\Constraints as AcmeAssert;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\TerminalRepository")
*/
class Terminal
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @AcmeAssert\IsAHostname // error cannot access private property when this constraint is used
*/
private $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\Ip //this constraint works
*/
private $ip;
...
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
...
public function getIp(): ?string
{
return $this->ip;
}
public function setIp(?string $ip): self
{
$this->ip = $ip;
return $this;
}
...
}
我的自定义约束:src / Validator / Constraints / IsAHostname.php
namespace App\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class IsAHostname extends Constraint
{
public $message = 'The string "{{ string }}" contains illegal character.';
}
我的自定义约束验证器:src / Validator / Constraints / IsAHostnameValidator.php
namespace App\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
class IsAHostnameValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof IsAHostname) {
throw new UnexpectedTypeException($constraint, IsAHostname::class);
}
// custom constraints should ignore null and empty values to allow
// other constraints (NotBlank, NotNull, etc.) take care of that
if (null === $value || '' === $value) {
return;
}
if (!is_string($value)) {
// throw this exception if your validator cannot handle the passed type so that it can be marked as invalid
throw new UnexpectedValueException($value, 'string');
// separate multiple types using pipes
// throw new UnexpectedValueException($value, 'string|int');
}
if (!preg_match('/^[a-zA-Z0-9-]+$/', $value, $matches)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ string }}', $value)
->addViolation();
}
}
}
我怎么称呼我的控制器:
/**
* @IsGranted("ROLE_OPERATOR")
* @Route("/terminal/{id}", name="terminal_operation", methods={"POST"})
**/
public function operateTerminalController(Terminal $terminal, Request $request, OperateTerminal $operateTerminal, ValidatorInterface $validator)
{
// paramConverter use {id} in the address to find the corresponding terminal in the db and build $terminal object
$terminalInfoErrors= $validator->validate($terminal);
...
}
Ip约束工作得很好,我尝试使用不同的值,并且仅在预期时才给我错误。但是,当我添加自定义约束@AcmeAssert \ IsAHostname时,出现以下错误:
无法访问私有属性App \ Entity \ Terminal :: $ name
由于$ terminalInfoErrors = $ validator-> validate($ terminal);而抛出
如果可能的话,我希望有一些想法可以进行调试。我想这两个约束之间访问属性的方法是不同的。