B
B
BonBon Slick2021-02-08 20:17:34
symfony
BonBon Slick, 2021-02-08 20:17:34

One field from several should be?

This example is very old.

It is possible in theory to somehow make a custom constraint or use Callback

Which approach is preferable?
Why?
Perhaps there are some developments?

I'm sure that someone has come across such a validation and there must be developments on the git somewhere.
Better than sculpting on a new one, after all, the case is relatively frequent, it would be possible to add an off validator.

UPD. Wrote a prostelnik method, to whom it is useful. But it is extremely inconvenient to sculpt every time. such a condition can be for more fields in other queries, something else is obviously needed, a constraint.

$classMetadata->addConstraint(new Callback(['callback' => 'validate']));
    }

    public function validate(ExecutionContextInterface $context): void {
        if (true === empty($this->email()) && true === empty($this->apiToken())) {
            $context->buildViolation('one.of.fields.required')
                    ->atPath('email')
                    ->setInvalidValue($this->email())
                    ->addViolation()
            ;
            $context->buildViolation('one.of.fields.required')
                    ->atPath('apiToken')
                    ->setInvalidValue($this->apiToken())
                    ->addViolation()
            ;
        }
    }


I would be grateful for the constraint gist.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question