2
votes

i have some problems with the validation rules.

UPDATED :

Warning (512): Could not find validation handler maxLength for lastname [CORE\Cake\Model\Validator\CakeValidationRule.php, line 281]

Warning (512): Could not find validation handler maxLength for firstname [CORE\Cake\Model\Validator\CakeValidationRule.php, line 281]

Warning (512): Could not find validation handler maxLength for phone [CORE\Cake\Model\Validator\CakeValidationRule.php, line 281]

Warning (512): Could not find validation handler maxLength for address [CORE\Cake\Model\Validator\CakeValidationRule.php, line 281]

Warning (512): Could not find validation handler maxLength for zipcode [CORE\Cake\Model\Validator\CakeValidationRule.php, line 281]

Warning (512): Could not find validation handler maxLength for city [CORE\Cake\Model\Validator\CakeValidationRule.php, line 281]

Warning (512): Could not find validation handler maxLength for birth [CORE\Cake\Model\Validator\CakeValidationRule.php, line 281]

public $validate = array(
    'username' => array(
        'alphanumeric' => array(
            'rule' => array('alphanumeric'),
            'allowEmpty' => false,
            'required' => true,
            'message' => "Alphanumeric characters only.",
        ),
        'between' => array(
            'rule' => array('between',4,20),
            'message' => "The username must be between %d and %d characters.",
        ),
        'isUnique' => array(
            'rule' => 'isUnique',
            'message' => "This username is already taken.",
        ),
    ),
    'password' => array(
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            'message' => "You must specify your password.",
        ),
    ),
    'passwordconfirm' => array(
        'between' => array(
            'rule' => array('between', 8, 20),
            'required' => true,
            'message' => "The password must be between %d and %d characters.",
        ),
        'equalTo' => array(
            'rule' => array('equalToField', 'password'),
            'message' => "Passwords are not identical.",
        )
    ),
    'oldpassword' => array(
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            'required' => true,
            'message' => "You must specify your old password.",
            'on' => 'update',
        ),
    ),
    'lastname' => array(
        'maxLength' => array(
            'rule' => array('maxLength', 25),
            'required' => true,
            'message' => "Your last name can't contain more than %d characters.",
        ),
    ),
    'firstname' => array(
        'maxLength' => array(
            'rule' => array('maxLength', 25),
            'required' => true,
            'message' => "Your first name can't contain more than %d characters.",
        ),
    ),
    'mail' => array(
        'email' => array(
            'rule' => array('email'),
            'required' => true,
            'message' => "You must specify a valid Email address.",
        ),
        'isUnique' => array(
            'rule' => 'isUnique',
            'message' => "This Email is already taken.",
        ),
    ),
    'user_type' => array(
        'inList' => array(
            'rule' => array('inList', array('particular','professional','association')),
            'required' => false,
            'message'   => "Your choice is not valid.",
        ),
    ),
    'denomination' => array(
        'maxLength' => array(
            'rule' => array('maxLength', 150),
            'message' => "Your company name can't contain more than %d characters.",
            'on' => 'update',
        ),
    ),
    'siret' => array(
        'maxLength' => array(
            'rule' => array('maxLength', 45),
            'message' => "Your company registration number can't contain more than %d characters.",
            'on' => 'update',
        ),
    ),
    'phone' => array(
        'maxLength' => array(
            'rule' => array('maxLength', 20),
            'message' => "Your phone number can't contain more than %d characters.",
            'on' => 'update',
        ),
    ),
    'address' => array(
        'maxLength' => array(
            'rule' => array('maxLength', 255),
            'message' => "Your email address can't contain more than %d characters.",
            'on' => 'update',
        ),
    ),
    'zipcode' => array(
        'maxLength' => array(
            'rule' => array('maxLength', 15),
            'message' => "Your zip code can't contain more than %d characters.",
            'on' => 'update',
        ),
    ),
    'city' => array(
        'maxLength' => array(
            'rule' => array('maxLength', 150),
            'message' => "Your city can't contain more than %d characters.",
            'on' => 'update',
        ),
    ),
    'birth' => array(
        'maxLength' => array(
            'rule' => array('maxLength', 10),
            'message' => '%d characters maximum for your date of birth.',
            'allowEmpty' => true,
        ),
    ),
);

And some times, i have this error too : (if it can help)

Fatal Error
Error: Call to undefined method Validation::getDataSource() 
File: Z:\wamp\www\gc2\lib\Cake\Model\Datasource\DboSource.php   
Line: 1062

Notice: If you want to customize this error message, create app\View\Errors\fatal_error.ctp

Version of CakePHP : 2.4.3 (Sorry for my bad english, i'm french ^^)

2

2 Answers

2
votes

Check the syntax on maxLength. It should be camel cased. Because you used all lower case, it cannot find the correct method.

You wrote:

'rule' => array('maxlength', 25),

It should be

'rule' => array('maxLength', 25),

See: http://book.cakephp.org/2.0/en/models/data-validation.html#Validation::maxLength

Also, you are writing the rule with a custom name like this:

'address' => array(
    'maxLength' => array(
        'rule' => array('maxLength', 255),
        'message' => "Your email address can't contain more than %d characters.",
        'on' => 'update',
    ),
),

Try changing it to:

'address' => array(
    'rule' => array('maxLength', 255),
    'message' => "Your email address can't contain more than %d characters.",
    'on' => 'update',
),
0
votes

This rule ensures that the data stays within a maximum length requirement.

public $validate = array( 'login' => array( 'rule' => array('maxLength', 15), 'message' => 'Usernames must be no larger than 15 characters long.' ) );

more info:-cakephp.org

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