Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDean Miller
3,189 PointsExtra Credit: Re-Displaying the Submission
Does anyone have any ideas about how to implement the extra credit items on this exercise? Item 1 is fine but the other 2 have me stumped..
Extra Credit Instead of showing only the highest-priority error message, it might be nice to display all the error messages
- Make the $error_message variable an array instead of a simple piece of text.
- Change the validation so that it runs all the checks, even after it encounters an error.
- Change the code that displays the error message to use a foreach loop that goes through all the elements in the error message array and display them.
2 Answers
Shawn Gregory
Courses Plus Student 40,672 PointsHello,
Without giving you the exact code, here's how I would do it:
Instead of stopping a page when an error occurs, you would put the error in the errors array and then continue onto the next validation. After all the validations have been checked, you would have a conditional that checks if the errors array has information in it; if it doesn't, process the information, if it does, redisplay the form.
You would then use a foreach loop to loop through all the errors in the errors array and then simply echo out the error onto the page.
I think this is enough information to get you started. I don't believe in putting up working code here as it doesn't allow you to try it for yourself. If you try it and it doesn't work as you want it to, you can display your code and I can point you in the right direction. After all this is a teaching and learning site :D
Cheers!
Dean Miller
3,189 PointsThanks Shawn, much appreciated. I'll see how I go :-) Cheers, Dean.
Brandon Beard
9,495 PointsBrandon Beard
9,495 PointsHi I tried to do the extra credit and can not figure any of it out. I've read what you wrote multiple times but dont understand how to put the error_message into an array.