0

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

2 回答 2

2

发现问题,需要放

$this->Support->set($this->request->data);

在验证调用之前,显然不访问自己模型的控制器需要明确设置。不管怎么说,还是要谢谢你 :)

于 2013-03-19T23:40:33.083 回答
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

于 2013-03-19T23:25:38.093 回答