{"id":1584,"date":"2022-08-30T15:17:45","date_gmt":"2022-08-30T15:17:45","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/27\/i-want-to-be-able-to-catch-an-error-if-there-is-one-in-a-form-with-validation-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:17:45","modified_gmt":"2022-08-30T15:17:45","slug":"i-want-to-be-able-to-catch-an-error-if-there-is-one-in-a-form-with-validation-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/i-want-to-be-able-to-catch-an-error-if-there-is-one-in-a-form-with-validation-collection-of-common-programming-errors\/","title":{"rendered":"I want to be able to catch an error if there is one in a form with Validation-Collection of common programming errors"},"content":{"rendered":"<p>Here is the code:<\/p>\n<pre><code>try {\n\n        $result = Model_User::update_user($_POST);\n\n            \/\/ message: save success\n        Message::add('success', __('Values saved.'));\n        \/\/ redirect and exit\n        $this-&gt;request-&gt;redirect('user\/profile');\n        return;\n     } catch (Exception $e) {\n        \/\/ Get errors for display in view\n        \/\/ Note how the first param is the path to the message file (e.g. \/messages\/register.php)\n        Message::add('error', __('Error: Values could not be saved.'));\n        $errors = $e-&gt;errors('register');\n        $errors = array_merge($errors, (isset($errors['_external']) ? $errors['_external'] : array()));\n        $view-&gt;set('errors', $errors);\n        \/\/ Pass on the old form values\n        $user-&gt;password = '';\n        $view-&gt;set('data', $user);\n     }\n<\/code><\/pre>\n<p>Here is the code of update_user method in Model_User:<\/p>\n<pre><code>public function update_user($fields)\n    {\n            $validation = Validation::factory($fields)\n                    -&gt;rules('password', $this-&gt;_rules['password'])\n                    -&gt;rules('password_confirm', $this-&gt;_rules['password_confirm'])\n                    -&gt;filters('password', $this-&gt;_filters['password']);\n\n            $this-&gt;validate($fields);\n            $users = CASSANDRA::selectColumnFamily('Users');\n            if ($users-&gt;get_cout($username))\n            {\n                    return $users-&gt;insert($uuid, array(\n                                            'username'      =&gt; $fields['username'],\n                                            'password'      =&gt; $fields['password'],\n                                            'email'         =&gt; $fields['email'],\n                                            'modify'        =&gt; date('YmdHis', time()),\n                                    ));\n            }\n            else\n            {\n                    return $validation;\n            }\n    }\n<\/code><\/pre>\n<p>I am now getting this error:<\/p>\n<pre><code>ErrorException [ Fatal Error ]: Call to undefined method ErrorException::errors()\n<\/code><\/pre>\n<p>Stuck on this line:<\/p>\n<pre><code>117             $errors = $e-&gt;errors('register');\n<\/code><\/pre>\n<p>Thanks in advance for any help!<\/p>\n<ol>\n<li>\n<p>You need to catch a Validation_Exception for handling validation errors.<\/p>\n<p>Only this kind of exception has an <code>errors()<\/code> method. Your code is throwing some other kind of exception, for which you need to do the error handling yourself.<\/p>\n<p>So, change<\/p>\n<p><code>} catch (Exception $e) {<\/code><\/p>\n<p>to<\/p>\n<pre><code>} catch (Validation_Exception $e) {\n    $errors = $e-&gt;errors('register');\n    ...\n} catch (Exception $e) {\n    \/\/ Do your error handling by hand\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-27 11:53:46. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Here is the code: try { $result = Model_User::update_user($_POST); \/\/ message: save success Message::add(&#8216;success&#8217;, __(&#8216;Values saved.&#8217;)); \/\/ redirect and exit $this-&gt;request-&gt;redirect(&#8216;user\/profile&#8217;); 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(&#8216;error&#8217;, __(&#8216;Error: Values could not be [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1584","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1584"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1584\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}