admin管理员组文章数量:1415100
I have code that looks more or less like this:
It's not my actual code, but just to show the concept of what I'm trying to do:
I have a form where each individual field is required and then some checks are run on the values of multiple fields in combination.
class testForm extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault(
'constraints',
[new Callback(
['callback' => function (
$payload,
ExecutionContextInterface $context
)
{
$data = $context->getObject()->getData();
if (strcasecmp($data['field1'], $data['field2']))
{
$context
->buildViolation('fields must be equal')
->atPath('[field1]')
->addViolation();
}
}
]
)]
);
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('field1', TextType::class, ['constraints' => [new NotBlank]])
->add('field2', TextType::class, ['constraints' => [new NotBlank]]);
}
}
The problem I'm having with this code is that it will throw a notice when you submit with empty fields (null is not allowed as parameters for string functions like strcasecmp() since PHP 8.0).
AFAICT this is because the parent validation runs before the child validation, so it only checks if the fields have values after the upper level validation has run.
What I am looking for is a way to reverse this, i.e. run the child validation before the parent validation is called. Looking at the Symfony documentation, there's a Valid constraint that seems to do exactly that, but it only seems to work with entities (my form doesn't use an entity, but a simple DTO).
So I remain wondering about a way to either make Valid work without entities, or some other way to remedy the constraints of doing this.
本文标签: phpValidate Childform DTOs before parentStack Overflow
版权声明:本文标题:php - Validate Childform DTOs before parent - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745158700a2645328.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论