Symfony Optional constraints not working as expected

PureSkillz83

I currently have a problem with Symfony validators Optional constraints.

I need the parentId rules to only be processed if that field has been passed (the key is in the ParameterBag), unfortunately Symfony always tries to validate even when it is not passed.

public function validateParameters(ParameterBag $request, Collection $validatorRules)
{
    $validatorErrors = $this->validator->validateValue($request->all(), $validatorRules);
    if (count($validatorErrors) !== 0) {
        throw new \Exception($validatorErrors[0]->getPropertyPath() . ' - ' . $validatorErrors[0]->getMessage());
    }
    echo 'yay!';
    exit;
}

public function create(Application $app, Request $request)
{
    // check that the validation rules pass for this service
    $this->validateParameters($request->request, new Collection([
        'parentId'    => [new Assert\Optional(), new Assert\Regex(['pattern' => '/^[0-9]\d*$/'])],
        'title'       => [new Assert\NotBlank(), new Assert\Length(['min' => 3])],
        'description' => [new Assert\NotBlank()],
    ]));
    // ...............
}

Any help or pointers would be greatly appreciated as the Symfony docs mainly talk about validating against an object directly, but I want to validate against the ParameterBag coming in.

Scott Yang

This should work

public function create(Application $app, Request $request)
{
    // check that the validation rules pass for this service
    $this->validateParameters($request->request, new Collection([
        'parentId'    => new Assert\Optional([
            new Assert\Regex(['pattern' => '/^[0-9]\d*$/'])
        ]),
        'title'       => [
            new Assert\NotBlank(), 
            new Assert\Length(['min' => 3])
        ],
        'description' => new Assert\NotBlank(),
    ]));
    // ...............
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Xcode constraints not working as expected

From Dev

Programmatic Constraints not working as expected

From Dev

Optional Chaining Not Working As Expected

From Dev

Constraints are not working as expected in landscape orientation

From Dev

Swift Type Constraints not working as expected

From Dev

SET CONSTRAINTS ALL DEFERRED not working as expected

From Dev

Symfony PdoSessionHandler not working as expected with PostgreSql

From Dev

Validation constraints not working on my Symfony3 project

From Dev

Symfony2 sessions not working as expected / session keeps timing out

From Dev

"NOT IN" not working as expected

From Dev

Validation for optional properties in Symfony

From Dev

Optional URL parameters in Symfony

From Dev

Optional text on Symfony router

From Dev

Constraints not working with a UITextView

From Dev

iOS constraints not working in UIScrollView

From Dev

WebRTC video constraints not working

From Dev

Constraints not working with ADBannerView

From Dev

Constraints Not Working Properly

From Dev

Constraints not working in TableViewCell

From Dev

Python ORMs with attribute constraints such as required / optional?

From Dev

Optional assignment in Swift not working?

From Dev

Optional parameter not working correctly

From Dev

Why is the optional binding not working

From Dev

Optional parameter not working correctly

From Dev

The constraints doesn't work as I expected

From Dev

Symfony multiple optional files with UploadedFile

From Dev

Symfony routing with dynamic optional parameters

From Dev

Symfony route with several optional params

From Dev

Symfony fosrestbundle optional parameter in route

Related Related

HotTag

Archive