Forms CakePHP中无模型的简单表单

Forms CakePHP中无模型的简单表单,forms,cakephp,validation,Forms,Cakephp,Validation,我正在尝试在产品页面中添加请求附加信息的表单。这是一个包含姓名、国家、电子邮件和问题字段的简单表单 我创建了这个,但我不喜欢这个想法,仅仅是为了小的形式,我需要制作新的模型和控制器 所以我在view.ctp中添加了表单 print $this->Form->create('', array('action' => 'sendemail')); print $this->Form->input('name',array('label' => __('Name'

我正在尝试在产品页面中添加请求附加信息的表单。这是一个包含姓名、国家、电子邮件和问题字段的简单表单

我创建了这个,但我不喜欢这个想法,仅仅是为了小的形式,我需要制作新的模型和控制器

所以我在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));
并在
products\u 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());
  }    
}
那么,有没有办法正确验证表单,如果某个字段在重定向后未填充,该字段将设置为错误表单输入?另外,如何将所有已填充的字段设置回表单


谢谢你的帮助

您不需要数据库表,但CakePHPs验证支持本质上与模型相关。通过创建一个虚拟模型并在其中存储验证规则,您的代码将比任何替代方案都要干净得多

要使用表格,请添加
var$useTable=false到您的模型定义

要验证而不保存,请调用
$this->MyModel->set($this->data)
然后调用
$this->MyModel->validates()
将对表单中的automagic填充执行检查并填充
$this->MyModel->validationErrors