I like the Exception better, but apparently according to this post,
http://stackoverflow.com/questions/3392495/should-i-throw-an-exception-on-user-input-errors,
an exception shouldn't be used for user input as bad user input is an
expected case.

On Mar 25, 1: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