0

I'm trying to use a custom validation method in cakephp 2.2.2 but for some reason the function isn't being called. Custom validation methods worked on the same system when I used cakephp for another project.

I found a question on stack overflow with what seemed to be the same problem, but there wasn't a solution (Custom filed validation in CakePHP).

This is the code in my model:

<?php
App::uses('AppModel', 'Model');

class Person extends AppModel {

    public $validate = array(
        'dob' => array(
            'date' => array(
                'rule' => array('date'),
            ),
            'date_between' => array(
                'rule' => array('dateBetween', '01/01/1996'),
                'message' => 'You are not the right age to enter',
            ),
        ),
    );

    public function dateBetween($v1, $v2) {
        dump_var($v1);
        dump_var($v2);

        return false;
    }

}

Note: dump_var() is a var_dump() with the pre tags around it.

Community
  • 1
  • 1
nevernew
  • 650
  • 10
  • 23
  • 1
    You sure that this field `dob` is being sent by the Controller so that it passes the validation? – Paulo Rodrigues Oct 05 '12 at 12:00
  • yes, the correct date is being saved to the database, but it's not being validated by my custom function – nevernew Oct 05 '12 at 12:43
  • in the controller i am setting the data: "$this->Person->set($this->request->data);", then validating it: "$this->Person->validates();", then i write the data to the session: "$this->Session->write('tmpdata', $this->request->data);", then i redirect to a confirm page, read the data from the session: "$tmpdata = $this->Session->read('tmpdata');" and finaly save it: "$this->Person->save($tmpdata, array('validate' => false));" – nevernew Oct 05 '12 at 12:50
  • You can try using the attribute `'required' => true` on both validation. – Paulo Rodrigues Oct 05 '12 at 12:52
  • Ok, well it just seemed to randomly start working when i truncated the database table... thanks for your help any way :) – nevernew Oct 05 '12 at 13:10
  • Just as a note CakePHP has a built in method called `pr()` and `debug()` which will automatically include `
    ` tags for you.
    – David Yell Jan 23 '13 at 10:40

0 Answers0