不知道我做错了什么,重新阅读文档几次。这是我唯一一次尝试从外部控制器验证模型。即使我使用无效的电子邮件地址,它也会像验证一样处理。只有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');