2015-01-21 51 views
0

我似乎無法弄清楚爲什麼我的表單沒有應用在我的模型中定義的驗證。有任何幫助?CakePHP表單輸入沒有應用驗證

\程序\查看\諮詢\ 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('[email protected]' => '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)); 
} 
} 
} 

回答

4

只有在調用模型的save()方法時才能正常驗證數據。您的控制器將提交的數據直接放入電子郵件中,因此不會與「查詢」模型發生任何交互。

您需要從控制器手動調用$this->Enquiry->validates()並添加邏輯來處理結果。

另請參閱:Validating Data from the Controller

+0

謝謝先生 - 修好了。我不知道我是如何錯過「從控制器驗證數據」的食譜部分,但謝謝。 – heatl0rd 2015-01-21 12:52:57