Reputation: 1
I cant seem to figure out why my form is not applying validation defined in my model. Any assistance?
\app\View\Enquiries\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']));
?>
\app\Model\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' => ''
)
);
}
\app\Controller\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));
}
}
}
Upvotes: 0
Views: 281
Reputation: 604
Data is only normally validated when calling the save()
method of a model. Your controller takes the submitted data and puts it straight into an e-mail so there is no interaction with the Enquiry model.
You will need to manually call $this->Enquiry->validates()
from the controller and add logic to deal with the result.
See also: Validating Data from the Controller
Upvotes: 4