1

I am upgrading my cakephp code from 2.6.7 to 3.1.5. The old version code works fine:

public $validate = array(
        'email' => array(
            'rule' => 'isUnique',
            'required' => true,
            'message' => 'Email already exist'
        ),
        'password' => array(
            'rule' => array('minLength', '6'),
            'message' => 'password must be minimum 6 characters long'
        )
    );

Now I want to convert it for cakephp latest version. i.e: 3.1.5 I found a solution for notempty rule. But what is the conversion of code. Or is there any automatic process of version upgrade for old version project?

Inigo Flores
  • 4,461
  • 1
  • 15
  • 36
Abdus Sattar Bhuiyan
  • 3,016
  • 4
  • 38
  • 72

1 Answers1

2

There is a tool to upgrade from CakePHP 2.x to CakePHP 3.x. However, I'm afraid it doesn't support validation.

Rewriting the rules for 3.x shouldn't be too complicated.

The above would look like:

public function validationDefault(Validator $validator) {

    $validator
        ->requirePresence('email')
        ->add('email', 'unique', [
            'rule' => 'validateUnique',
            'provider' => 'table',
            'message' => 'Email already exists'
         ])
        ->add('password', 'minLength', [
             'rule' => ['minLength', 6],
             'message' => 'Password must be minimum 6 characters long',
    ]);

    return $validator;
}

The above goes in your Table definition.

Make sure you read the CakePHP 3.x documentation on Validation.

Inigo Flores
  • 4,461
  • 1
  • 15
  • 36