Overview
In this tutorial I’ll show you in about 30 minutes how to create a html form with the built-in FormHelper of CakePHP which automatically links up to the database fields to see what kind of input field should be generated. Further on we also check out the built-in validation options and how we can save our submitted form data to the database. And at the end I’ll explain how you can use CakePHP’s Model callback functions to put in our custom code before a CakePHP action.
In this tutorial I’ll be covering
- Create a Form via FormHelper
- Save data to the database
- Do validation on different fields with custom error messages
- Explain callbacks and use it to hash our password
- Do redirect after a save
This part is the follow-up on what we have seen in part 1 of my CakePHP basics serie!
Let me know if you enjoyed it!
25 Responses
Great tutorials, finally got started with CakePHP thanks to these.
One question though: the redirect seems to cancel out the validation?
I’ve been searching around but it seems there’s no easy way to redirect on succes because you need to store the error messages in the session to be able to display them after a redirect?
just put an if around the save function in your controller.
The save functions returns true when the save is successful, so then you can execute the redirect function. If it’s false (and you enter the else block) then you do nothing and the validation will be shown!
Hope this answers your question! Happy coding!
That did the trick, thanks 🙂
Great tut!
Waiting for part 3!
It will be promising series, thank you, However, when talking about forms, most tutorials, don’t paid enough attention to what I call Global Forms. For example, How to include add action’s view of the comments controller to be rendered in the view action’s view of the post controller where the submit results and validation message should be rendered in posts/view/xx!
I think that this issue is one of I called, I may call alot ;), feeling new comer with flexibility and keeping him away from hating MCV!
Hi Said, thanks for your remark! Could you clarify your issue a little bit more, because I don’t really understand what you mean!
Will definitely try this out after my re-examinations (*sadpanda*)! But what keyboard are u using? I like the sound of it ^^, and I doubt it’s your laptopkeyboard (we would hear it 5x louder).
apple qwerty keyboard 🙂 (http://store.apple.com/us/product/MB110LL/B/apple-keyboard-with-numeric-keypad)
very nice to work on!
Qwerty FTW!
Great video tutorials ! Looking forward the next videos. Thank you !
Thanx man !
You helped me a lot :))
Very clear tutorial. Thank you.
Awesome site, helped me a lot in learning CakePHP way better than then bakery CakePHP. =P
I faced a problem in redirect, I followed your tutorial and it can’t redirect, I added a Route::connect to define the path and it still doesn’t work.
FYI, I not really understand how to add the if around the save is successful >.<
Hi Frederik,
Ondanks het vreemde engels is dit helemaal geweldig! Ik ben bezig met cakePHP, en heb door deze clips een goede richting gekregen en ben helemaal enthousiast!
bedankt!
I followed your tutorial. But I typed sha1 as shal and submitted data. An error message is displayed. The data is submitted to database and everything is saved to the database, and the password field is empty. Is it an issue with cakephp or I am doing something wrong.
I did not added redirect line to the controller.
Very nice tutorial sir. Thank you.
Very nice tutorials man. Thank you very much.
This is great man.. Thanks.
Excellent!
A lot of things now are more clear in my mind….
Thanks!
Apythanos from Hellas
nice tutorials sir…but i have one problem, we i redirect to ‘/users’ then requested URL was not found on this server…how can fix this problem?
Hi Frederik,
your tutorial is awesome, help a lot. Thanks.
Have a question, insde the User model, when typing “public function beforeSave…”, there is no auto completion pops up for beforeSave. I am using phpStorm as you are, could this due to some settings?
Many Thanks! This is really helpful!
Enjoy your tutorial very much. Everything is clear and you make it so easy. Looking forward to your part 3.