I would think that a validation exception would be thrown in the event
one of the validators was passed a value it doesn't support, or if the
dataclass isn't bound properly for validation.

The logic in "createUser" is typical and widely-used in validation
scenarios.  If you wanted to test for an exception, put that exception
in your "createUser" function, rather than expecting "$this-
>validator" to throw it.

Is the code in question something you've written or something actually
in the symfony codebase, because I'm not quite recognizing it...

-Eric

On Mar 25, 12:28 pm, Mark C <[email protected]> wrote:
> Hi all,
> Just want some opinions on whether the validator should throw an exception 
> instead of returning an array, before I go and update the core.
> At the moment, the validator returns an array of errors. Writing code to 
> handle these errors would look like the example below (Takes a posted user 
> and creates a user or errors e.g. if the name is blank):
> function createUser($name) {
>      // ...
>     $user = new User();
>     $user->setName($name);
>     $errors = $this->validator->validate($user)
>     if ($errors) {
>         return $errors;
>     } else {
>         return $user;
>     }
> }
> function postAction() {
>     // ...
>     $object = $this->createUser($request->getParameter('name'));
>     if ($object instanceof User)
>         return $this->render('HelloBundle:Users:delete.html.twig', array(
>             'user' => $object
>         ));
>     } else {
>          return $this->render('HelloBundle:Users:error.html.twig', array(
>             'errors' => $object
>         ));
>     }
>    
> }
> As you can see you have to handle the errors in each function called, 
> resulting in 2 if/elses. By throwing an exception you wouild only need to 
> handle it once, at the point you do something different. The code would look 
> like this:
> function createUser($name) {
>      // ...
>     $user = new User();
>     $user->setName($name);
>     $this->validator->validate($user);
>     return $user;
> }
> function postAction() {
>     //
>     try {
>         $user = $this->createUser($request->getParameter('myparam'));
>         return $this->render('HelloBundle:Users:delete.html.twig', array(
>             'user' => $user
>         ));
>     } catch (ValidationException $e) {
>         $errors = $e->getMessages();
>         return $this->render('HelloBundle:Users:error.html.twig', array(
>             'errors' => $errors
>         ));
>     }
> }
> Which seems much cleaner and make more sense to me.
> Any thoughts?
> Cheers

-- 
If you want to report a vulnerability issue on symfony, please send it to 
security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/symfony-devs?hl=en

Reply via email to