Simple form without model in CakePHP

cakephpformsvalidation

I'm trying to add form for request of additional information in product page. It's simple form with name, country, email and question fields.

I founded this tutorial , but i don't like idea that just for small form I need make new model and controller.

So i add form in view.ctp

print $this->Form->create('', array('action' => 'sendemail')); 
print $this->Form->input('name',array('label' => __('Name',true).':'));
print $this->Form->input('country',array('label' => __('Country',true).':'));
print $this->Form->input('email',array('label' => __('E-mail',true).':'));
print $this->Form->input('question',array('type' => 'textarea', 'label' => __('Your question',true).':'));
print $this->Form->end(__('Send', true));

and add function sendemail in products_controller.php

function sendemail(){          
  if(!empty($this->data)){        
    if($this->data['Product']['name'] && $this->data['Product']['country'] && $this->data['Product']['email'] && $this->data['Product']['question']){
      // sending email
      $this->Session->setFlash(__('Thanks, your qustion was send.', true), 'default', array('class' => 'message status'));
      $this->redirect($this->referer());        
    } else {
      // validation
      $this->Session->setFlash(__('Fill all fiels', true), 'default', array('class' => 'message error'));
      $this->redirect($this->referer());        
    }
  } else {      
    $this->Session->setFlash(__('Error occurred', true), 'default', array('class' => 'message error'));
    $this->redirect($this->referer());
  }    
}

So is there any way how to validate the form properly and if some field isn't fill after redirection this field will be set as error form input? Also how can I set all already filled fields back to form?

Thanks for any help!

Best Answer

You don't need a database table, but CakePHPs validation support is inherently tied to models. Your code is going to be a lot cleaner than any alternative by creating a dummy model and storing your validation rules in it.

To get around using a table, add var $useTable = false; to your model definition.

To validate without saving, call $this->MyModel->set($this->data); and then a call to $this->MyModel->validates() will perform the check and populate $this->MyModel->validationErrors for automagic population in the form.