不知道我在做什么错,重新阅读文档多次。这是我试图从外部控制器验证模型的唯一时间。它处理就好像验证过程一样正常,即使我使用了无效的电子邮件地址。只有recaptcha工作;任何帮助将不胜感激:cakephp没有按预期验证模型
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');
接受你的答案请;)显然,一个控制器不能访问自己的模型需要明确设置这一点 - 蛋糕永远不会自动设置模型数据,你总是要自己做。 – AD7six 2013-03-20 01:02:15