CakePHP Basics Tutorial – Part 2


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

  1. Create a Form via FormHelper
  2. Save data to the database
  3. Do validation on different fields with custom error messages
  4. Explain callbacks and use it to hash our password
  5. 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!

Share this post

25 Responses

  1. 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?

    1. 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!

  2. 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!

  3. 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).

  4. 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 >.<

  5. 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!


  6. 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.

  7. 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?

  8. 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?

  9. Enjoy your tutorial very much. Everything is clear and you make it so easy. Looking forward to your part 3.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts