Wednesday, April 8, 2009

Preventing Duplicate Form Submissions in CakePHP

Wednesday, April 8, 2009

Forms are a necessary part of web applications and a great way to add data to your database. But somtimes this useful tool may cause problems if your visitors submit the same form information over and over again. How do you solve this with CakePHP or in Cake way? The solution is simple. CakePHP already has one built in, and it's really neat.

Using our Posts controller example, we can make use of Model::postConditions() together with Model::hasAny():

function add() {
 if (!empty($this->data)) {
  if($this->Post->validates()) {
   if ($this->Post->hasAny($this->postConditions($this->data))) { 
      __("Duplicate form submissions are not acceptable.", true)
   } else {
    if ($this->Post->save($this->data)) {
     $this->Session->setFlash(__("Your data has been saved.", true));

Hope this is helpful for some of you.

1 comment:

  1. Nice tutorial....thanks for this post.....
    Please visit this link for learn more script.


Please feel free to post your comment about this article.

JamNite ◄Design by Pocket, BlogBulk Blogger Templates