我似乎无法弄清楚为什么我的表单没有应用我的模型中定义的验证。有什么帮助吗?
\应用\视图\查询\ view.ctp:
<?php
echo $this->Form->create('Enquiry', array('action'=>'email','novalidate' => true));
echo $this->Form->input('message', array ('type' => 'textarea', 'class'=>'form-control'));
echo $this->Form->hidden('email', array ('value'=> $enquiry['Enquiry']['email']));
?>
\应用\模型\ Enquiry.php
<?php
App::uses('AppModel', 'Model');
class Enquiry extends AppModel {
public $actsAs = array('Acl' => array('type' => 'requester'));
public function parentNode() {
return null;
}
public $validate = array(
'message' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Please enter your enquiry',
'allowEmpty' => false,
)
),
);
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
\应用\控制器\ EnquiriesController.php
<?php
App::uses('AppController', 'Controller', 'Network/Email');
class EnquiriesController extends AppController {
public $helpers = array('GoogleMap','Html','Form','Session'); //Adding the helper
public $components = array('Paginator','Email','Session');
public function email($id, $dest=null){
if ($this->request->data) {
//Admin reply enquiry email
$Email = new CakeEmail('default');
$Email->config('default');
$Email->template('replyenq');
$Email->from(array('xxxx@gmail' => 'xxxx'))
->to($this->request->data['Enquiry']['email'])
->subject('xxxx has sent you a reply!')
->send();
//after sending, display a notification
$this->Session->setFlash(__('Reply enquiry has been successful.' , true), 'alert-box', array('class'=>'alert-success'));
//Redirect after email has been successful
return $this->redirect(array('action' => '../enquiries'));
}
else {
$this->Session->setFlash(__('Message was empty. Please ensure you enter a message'), 'alert-box', array('class'=>'alert-warning'));
return $this->redirect(array('action' => '../enquiries/view/'.$id));
}
}
}
答案 0 :(得分:4)
通常只在调用模型的save()
方法时验证数据。您的控制器会将提交的数据直接放入电子邮件中,因此不会与查询模型进行交互。
您需要从控制器手动调用$this->Enquiry->validates()
并添加逻辑来处理结果。