I want to be able to catch an error if there is one in a form with Validation-Collection of common programming errors

Here is the code:

try {

        $result = Model_User::update_user($_POST);

            // message: save success
        Message::add('success', __('Values saved.'));
        // redirect and exit
        $this->request->redirect('user/profile');
        return;
     } catch (Exception $e) {
        // Get errors for display in view
        // Note how the first param is the path to the message file (e.g. /messages/register.php)
        Message::add('error', __('Error: Values could not be saved.'));
        $errors = $e->errors('register');
        $errors = array_merge($errors, (isset($errors['_external']) ? $errors['_external'] : array()));
        $view->set('errors', $errors);
        // Pass on the old form values
        $user->password = '';
        $view->set('data', $user);
     }

Here is the code of update_user method in Model_User:

public function update_user($fields)
    {
            $validation = Validation::factory($fields)
                    ->rules('password', $this->_rules['password'])
                    ->rules('password_confirm', $this->_rules['password_confirm'])
                    ->filters('password', $this->_filters['password']);

            $this->validate($fields);
            $users = CASSANDRA::selectColumnFamily('Users');
            if ($users->get_cout($username))
            {
                    return $users->insert($uuid, array(
                                            'username'      => $fields['username'],
                                            'password'      => $fields['password'],
                                            'email'         => $fields['email'],
                                            'modify'        => date('YmdHis', time()),
                                    ));
            }
            else
            {
                    return $validation;
            }
    }

I am now getting this error:

ErrorException [ Fatal Error ]: Call to undefined method ErrorException::errors()

Stuck on this line:

117             $errors = $e->errors('register');

Thanks in advance for any help!

  1. You need to catch a Validation_Exception for handling validation errors.

    Only this kind of exception has an errors() method. Your code is throwing some other kind of exception, for which you need to do the error handling yourself.

    So, change

    } catch (Exception $e) {

    to

    } catch (Validation_Exception $e) {
        $errors = $e->errors('register');
        ...
    } catch (Exception $e) {
        // Do your error handling by hand
    }
    

Originally posted 2013-11-27 11:53:46.