任何人都可以弄清楚为什么会这样:
$builder
->add('networkLoIp', IntegerType::class, array(
'constraints' => array(
new ANotBlank(),
new AExpression(array(
'expression' => 'value <= this.getNetworkHiIp()'
))
)
))
->add('networkHiIp', IntegerType::class, array(
'constraints' => array(
new ANotBlank()
)
))
->setMethod('post')
;
给出如下错误:"无法获取非对象的属性"?
当我在提交后转储数据时,我可以看到我在表单中输入的值。
编辑
将表达式移动到选项后,我得到的一个非常相似的错误,即
public function configureOptions(OptionsResolver $optionsResolver)
{
$optionsResolver->setDefaults(array(
'constraints' => array(
new AExpression(array(
'expression' => "value['networkLoIp'] <= value['networkHiIp']"
))
)
));
}
"无法获取非阵列上的项目。"
对于您的特定情况,您可以使用以下内容:
$builder
->add('networkLoIp', IntegerType::class, array(
'constraints' => array(
new ANotBlank(),
new ALessThanOrEqual($this->getNetworkHiIp())
// or if the method is static and defined somewhere else
// new ALessThanOrEqual(ThatObject::getNetworkHiIp())
)
))
参考LessThanOrEqual
.
如果该字段存储IP地址,我也将使用Ip
验证器。
use SymfonyComponentFormExtensionCoreTypeIntegerType;
use AppBundleFormCreateLicence
class crateLicenciaonType extends AnstractType{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$entity= new CreateLicence();
$builder
->add('networkLoIp', IntegerType::class, array(
'constraints' => array(
new ANotBlank(),
new ALessThanOrEqual($entity->getNetworkHiIp())
// or if the method is static and defined somewhere else
// new ALessThanOrEqual(ThatObject::getNetworkHiIp())
)
))
}
您不能在非对象上应用表达式禁忌,但您可以将另一个用于相同目的或创建指向表单的对象 (DTO) 链接。