Displaying error messages with _error
When a new employee record failed to be added to the database because of some error, that error should be shown to the user to let the user know why the insertion failed so the user can try to resolve the problem.
When an update attempt failed, the error should also be shown to let the user know the state of things.
After a failed search when the employee was not found, the message should be also be displayed.
When a login attempt failed, the login page should display again with an error message telling the user why the login attempt failed.
When a method in EmployeeController generates an error, the error message is captured in the _error variable which is generated automatically, so we can use it to display the error. The facility to display this _error message is provided by the @errorDisplay annotation. The @errorDisplay indicates which method and page will handle the error.
As an example, let us edit the postFindEmployee() method of EmployeeController.
Here, we indicated that the getAllEmployees() method will display the error message. We used the enforce() function to generate an error if the condition is not met. In this case, the getAllEmployees() method should be called with a new parameter named _error, which was generated automatically, and which it can pass to the emplistall.dt page for display.
So here is the edited getAllEmployees() method.
We assigned the _error parameter with null as default in case the error message is blank.
Edit the views\emplistall.dt page so it can display the error, if there is an error.
And here is our addition to the views\styles.dt file.
Compile, run and refresh your browser to test the Find employee link and look for a non-existent record.
After clicking Find, you should get this:
Now we can add error-trapping mechanisms to the add, edit and delete methods as well.
Now let’s talk about securing our site. Let’s talk about logging in, authentication and authorization.
Last updated