Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php symfony4自定义验证程序可以';不得进入私人财产_Php_Symfony_Validation_Symfony4 - Fatal编程技术网

Php symfony4自定义验证程序可以';不得进入私人财产

Php symfony4自定义验证程序可以';不得进入私人财产,php,symfony,validation,symfony4,Php,Symfony,Validation,Symfony4,我正在使用symfony4作为web应用程序。我成功地使用验证ip地址。但在尝试使用我自己的自定义约束时,出现错误无法访问私有财产App\Entity\Terminal::$name,请遵循以下指南: 我要验证的实体:src/entity/Terminal.php namespace-App\Entity; 使用条令\ORM\Mapping作为ORM; 使用App\Validator\Constraints作为AcmeAssert; 使用Symfony\Component\Validator\C

我正在使用symfony4作为web应用程序。我成功地使用验证ip地址。但在尝试使用我自己的自定义约束时,出现错误无法访问私有财产App\Entity\Terminal::$name,请遵循以下指南:

我要验证的实体:src/entity/Terminal.php

namespace-App\Entity;
使用条令\ORM\Mapping作为ORM;
使用App\Validator\Constraints作为AcmeAssert;
使用Symfony\Component\Validator\Constraints作为断言;
/**
*@ORM\Entity(repositoryClass=“App\Repository\TerminalRepository”)
*/
类终端
{
/**
*@ORM\Id()
*@ORM\GeneratedValue()
*@ORM\Column(type=“integer”)
*/
私人$id;
/**
*@ORM\Column(type=“string”,length=255,nullable=true)
*@AcmeAssert\isahustname//error使用此约束时无法访问私有属性
*/
私人$name;
/**
*@ORM\Column(type=“string”,length=255,nullable=true)
*@Assert\Ip//此约束有效
*/
私人$ip;
...
公共函数getName():?字符串
{                                           
返回$this->name;
}                                           
公共函数setName(?string$name):self
{                                           
$this->name=$name;
退还$this;
}
...
公共函数getIp():?字符串
{                                       
返回$this->ip;
}                                       
公共函数setIp(?字符串$ip):self
{                                       
$this->ip=$ip;
退还$this;
}    
...                                  
}
我的自定义约束:src/Validator/Constraints/isahustname.php

namespace-App\Validator\Constraints;
使用Symfony\Component\Validator\Constraint;
/**
*@注释
*/
类扩展了主机名约束
{
public$message='字符串“{{string}}”包含非法字符';
}
我的自定义约束验证器:src/validator/Constraints/isahostnamevidator.php

namespace-App\Validator\Constraints;
使用Symfony\Component\Validator\Constraint;
使用Symfony\Component\Validator\ConstraintValidator;
使用Symfony\Component\Validator\Exception\UnexpectedTypeException;
使用Symfony\Component\Validator\Exception\UnexpectedValueException;
类IsAHostnameValidator扩展了ConstraintValidator
{
公共函数验证($value,Constraint$Constraint)
{
if(!$ISAOSTNAME的约束实例){
抛出新的UnexpectedTypeException($constraint,IsAHostname::class);
}
//自定义约束应忽略null和空值以允许
//其他约束(NotBlank、NotNull等)会考虑到这一点
如果(null==$value | |'===$value){
返回;
}
如果(!是字符串($value)){
//如果验证器无法处理传递的类型,因此可以将其标记为无效,则引发此异常
抛出新的意外值异常($value,'string');
//使用管道分隔多个类型
//抛出新的意外值异常($value,'string | int');
}
如果(!preg_match('/^[a-zA-Z0-9-]+$/',$value,$matches)){
$this->context->build违例($constraint->message)
->setParameter({{string}}',$value)
->添加违例();
}
}
}
我如何称呼它为我的控制器:

/**
*@isgrated(“角色操作员”)
*@Route(“/terminal/{id}”,name=“terminal\u operation”,methods={“POST”})
**/
公共函数OperaterMinalController(终端$Terminal,请求$Request,OperaterMinal$OperaterMinal,validator接口$validator)
{
//paramConverter使用地址中的{id}在db中查找相应的终端并构建$terminal对象
$terminalInfoErrors=$validator->validate($terminal);
...
}
Ip约束工作得非常好,我尝试了不同的值,只有在预期的情况下才会给我错误。但当我添加自定义约束@AcmeAssert\isaostname时,它会给出以下错误:

无法访问私有财产App\Entity\Terminal::$name

由于$terminalInfoErrors=$validator->validate($terminal)而引发

如果可能的话,我想要一些想法来调试它。我假设访问属性的方法在这两个约束之间是不同的

  • 验证器如何访问具有此预构建Ip约束的实体的私有属性
  • 如何使自定义约束以相同的方式访问对象属性

@yivi您好,我使用getName()和setName()使用此属性。在一个服务上,我在curl命令中使用了这个name属性,因此我想在将它发送到curl之前验证它是否没有特殊字符。您是否测试了您的约束是否按照参考示例的预期工作?也就是说,没有在控制器中显式调用“validate”方法,而是让框架根据注释自动执行该操作?通常情况下,实体属性被定义为受保护的,而不是私有的,这是一件你可以尝试的事情(尽管我不知道这在这种情况下是否会有所不同)。Hello@RémiGirard,我已经实现了你的示例,我没有这个问题,你确定你没有做其他事情吗?也许你忘了什么here@ejuhjav当我尝试向数据库添加对象时,即使没有此验证,也会引发此错误。在对终端信息执行某些操作之前,我必须使用validate。我试图将属性设置为protected,但它将错误更改为类似的错误:无法访问protectedproperty@MohammedYassineCHABLI感谢穆罕默德抽出时间来尝试。信息技术