3

When I'm using Symfony v5.1 array of collection validation I got a strange response.

This is my code

   $constraint = new Assert\Collection([
        'email' => new Assert\Email(),
        'password' => new Assert\Length(['min' => 60]),
    ]);

    $violations = $validator->validate($request->request->all() , $constraint);

    foreach($violations as $violation)
    {
        $errors[] = [$violation->getPropertyPath() => $violation->getMessage()];
    }

    dd($errors);

and this is the result I got:

 array:5 [
  0 => array:1 [
    "[password]" => "This value is too short. It should have 60 characters or more."
  ]
  1 => array:1 [
    "[name]" => "This field was not expected."
  ]
  2 => array:1 [
    "[phone_number]" => "This field was not expected."
  ]
  3 => array:1 [
    "[username]" => "This field was not expected."
  ]
  4 => array:1 [
    "[role_id]" => "This field was not expected."
  ]
]

so I'm wondering why the name of the input is swapping in array[] [name] so is there is a wrong something I did?

And why Symfony is focused on entity validation and not on the request like Laravel framework?

11
  • 1
    have you taken a look at the validation component in symfony? The probably simplest way is to define validation on an object into which the json is going to be deserialized. But validation can also be done in more inconvenient ways, but the use case is a bit unknown right now, since you omitted ... well ... everything, really. Commented Jun 17, 2020 at 20:59
  • so how can implement what you found in the docs a separate way, I just want to pass the request object to another validation class to validate it then check on what return Commented Jun 17, 2020 at 21:07
  • 1
    have you tried something like this: symfonycasts.com/screencast/symfony-rest2/… a class doesn't have to be an entity. but a class is a fine thing to define validation/constraints for, I don't know how laravel manages this, but symfony isn't primarily about api ... Commented Jun 17, 2020 at 21:31
  • 1
    So, you're new to driving a car, and you have experience with a motorbike. I tell you how to operate the car door, and you tell me, "but I don't want to operate the car door, look, I don't need to operate any door with my motorbike." And although I understand, that your motorbike has no door, a car is something different and has different concepts. takes a deep breath I pointed you to 2 different approaches on how to handle this somewhat conveniently in symfony, but you want the laravel way. So my question now is: why do you use symfony when you want laravel? Commented Jun 17, 2020 at 22:25
  • 1
    Man, I just don't like the way it uses it in the validation, I don't like the annotation as it coupling the model with the validation and I want a way to separate it. but also I want to validate the request coming from the user as it may have inputs not exist in the entity. so I am asking if there another way to handle the validation so if you don't know let the others help Commented Jun 18, 2020 at 0:35

1 Answer 1

2

I found a really package implementing what I need, it just works like as Laravel and validates the request so easy https://github.com/fesor/request-objects but it is not working on Symfony 5 so I upgraded it to be compatible with v5 wit some changes https://github.com/TheGeekyM/symfony-object-request-validation.

you just call the request in the controlled

namespace App\Controlles;

public function registerUserAction(RegisterUserRequest $request)
{
    // Do Stuff! The data is already validated!
}

and this is the request with the validations

use Fesor\RequestObject\RequestObject;
use Symfony\Component\Validator\Constraints as Assert;

class RegisterUserRequest extends RequestObject
{
    public function rules()
    {
        return new Assert\Collection([
            'email' => new Assert\Email(['message' => 'Please fill in valid email']),
            'password' => new Assert\Length(['min' => 4, 'minMessage' => 'Password is to short']),
            'first_name' => new Assert\NotNull(['message' => 'Please provide your first name']),
            'last_name' => new Assert\NotNull(['message' => 'Please provide your last name'])
        ]);
    }
}

and that is it, enjoy

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.