skrilled
skrilled

Reputation: 5371

cakephp not validating model as expected

not sure what i'm doing wrong, re-read the documentation several times. this is the only time i've tried to validate a model from an outside controller though. it processes as if the validation went through fine, even when i use an invalid email address though. only the recaptcha works; any help would be greatly appreciated:

in the controller:

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
    }
}

in the model:

class Support extends AppModel
{
    public $name = 'Support';
    public $useTable = false;   
    public $validate = array(
        'email' => array('rule'=>'email','message'=>'You must enter a valid email')
        );
}

in the view:

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');

Upvotes: 0

Views: 124

Answers (2)

Steven
Steven

Reputation: 303

Try this in your model instead:

public $validate = array(
    'email' => array(
        'rule'    => array('email', true),
        'message' => 'Please supply a valid email address.'
    )
);

From http://book.cakephp.org/2.0/en/models/data-validation.html

Upvotes: 0

skrilled
skrilled

Reputation: 5371

found the problem, needed to put

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

before the validation call, apparently a controller not accessing its own model needs to have this explicitly set. thanks anyway :)

Upvotes: 2

Related Questions