不确定我做错了什么,多次重读文档。这是我唯一一次尝试从外部控制器验证模型。即使我使用的是无效的电子邮件地址,它也会像验证一样正常处理。只有recaptcha工作;任何帮助将不胜感激:
在控制器中:
if(!$this->request->is('get'))
{
App::import('Vendor','recaptchalib');
$this->set('captchaContent' , recaptcha_get_html($this->publicKey));
$resp = recaptcha_check_answer( $this->privateKey,
$_SERVER['REMOTE_ADDR'],
$this->request->data['recaptcha_challenge_field'],
$this->request->data['recaptcha_response_field']);
if (!$resp->is_valid) {
$this->set('recaptcha_error','You did not enter the words correctly. Please try again.');
} elseif($this->Support->validates($this->request->data)) {
// send the message
}
}
在模型中:
class Support extends AppModel
{
public $name = 'Support';
public $useTable = false;
public $validate = array(
'email' => array('rule'=>'email','message'=>'You must enter a valid email')
);
}
在视图中:
echo $this->Form->create('Support');
echo $this->Form->input('name');
echo $this->Form->input('email');
echo $this->Form->input('message',array('type'=>'textarea'));
echo '<div style="margin-left: 150px; margin-bottom: 10px;">'.$captchaContent.'</div>';
if($recaptcha_error) echo '<p style="color:red; margin-left: 150px;">'.$recaptcha_error.'</p>';
echo $this->Form->end('Send Message');
答案 0 :(得分:2)
找到问题,需要把
$this->Support->set($this->request->data);
在验证调用之前,显然控制器没有访问自己的模型需要明确设置。无论如何,谢谢:)
答案 1 :(得分:0)
请在您的模型中尝试此操作:
public $validate = array(
'email' => array(
'rule' => array('email', true),
'message' => 'Please supply a valid email address.'
)
);
来自http://book.cakephp.org/2.0/en/models/data-validation.html