Roberto Rizzi
Roberto Rizzi

Reputation: 1543

symfony validation doesn't work

I guess there is something I'm missing. I have a User entity which is validated through a yml file but every time I send a post request to the route it seems it doesn't get my request. With this I mean that the route works fine but I keep getting the error messages that the password and username should not be blank (due to the constraints i set). So it seems it's not getting my request validated against the entity.

I made sure to have this settings triggered in my config: validation: { enabled: true, enable_annotations: true }

Here is my routing.yml:

user_login_homepage:
    path:     /check
    defaults: { _controller: UserLoginBundle:Login:checkCredentials }
    methods: [POST]

Here is my validation.yml

User\UserBundle\Entity\User:
    properties:
        username:
            - NotBlank: ~
        password:
            - NotBlank: ~

Here is my controller (LoginController.php)

public function checkCredentialsAction(Request $request)
{
    $recursiveValidator = $this->get('validator');

    $user = new User();

    $errors = $recursiveValidator->validate($user);

    if (count($errors) > 0) {
        $errorsString = (string) $errors;
        return new Response($errorsString);
    }

    return new Response('Yuppy');
}

I've just tried to follow the instructions but I'm not able to have it work :( Am I missing something?

Upvotes: 1

Views: 720

Answers (1)

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35973

you are creating an empty User so It's correct the error, try this (I have imagine that username and password are passed into POST data right?):

$user = new User();
$postData = $request->request->all();
$user->setUsername($postData['username'];
$user->setPassword($postData['password'];

$errors = $recursiveValidator->validate($user);

Upvotes: 2

Related Questions