php - symfony constraints, array field either not exist or string

for example I have array like this:
[
'id' => 1,
'name' => 'some name',
]
but also it can be without name at all, like this:
[
'id' => 1,
]
how can I validate it with a Symfony constraints?
I tried like this:
$this->validator->validate($data, new Assert\Collection([
'fields => [
'id' => [
new Assert\Required(),
new Assert\Type(['type' => 'integer']),
],
'name' => [
new Assert\Optional(),
new Assert\Type(['type' => 'integer']),
],
]
]));
and like this:
$this->validator->validate($data, new Assert\Collection([
'fields => [
'id' => [
new Assert\Optional(),
new Assert\Type(['type' => 'integer']),
],
'name' => new Assert\AtLeastOneOf([
new Assert\Optional(),
new Assert\Type(['type' => 'string']),
]),
]
]));
and it's not working, when field not exist I'm getting error that field is missing.
Thanks in advice!
Answer
Solution:
Version 1 using only constraints provided by Symfony:
$this->validator->validate($data, [
# Checks only the required fields on your collection
new Assert\Collection([
'fields => [
'id' => [
new Assert\Type(['type' => 'integer']),
],
],
'allowExtraFields' => true, // This will allow for additional optional fields
'allowMissingFields => false, // This makes sure that all fields described here are mandatory
]),
# Checks only the optional fields on your collection
new Assert\Collection([
'fields => [
'name' => [
new Assert\Type(['type' => 'string']),
],
],
'allowExtraFields' => true, // This will allow for additional required fields
'allowMissingFields => true,
])
]);
This will allow submitting['id' => ?]
and['id' => ?, 'name' => ?]
, but not['name' => ?]
.
You can also work with different groups, but then you have to specify which group you are checking for when you callvalidate()
, so that is probably not as helpful.
Version 2 your own custom constraint:
$this->validator->validate($data, [
@RequiredFields(['id']),
@Assert\Collection(...like in your example...)
]);
The constraint itself could then look something like:
class RequiredFieldsValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof RequiredFieldsConstraint) {
throw new UnexpectedTypeException($constraint, ContainsAlphanumeric::class);
}
if ($value === null) {
return;
}
if (!is_array($value)) {
throw new UnexpectedValueException($value, 'array');
}
foreach ($requiredField in $constraint->fields) {
if (!array_key_exists($requiredField, $value)) {
// build constraint error for this field
}
}
}
}
The constraint itself can then look similar to Collection.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to undefined function str_contains()
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.