Hi Anthony, I am still trying to make the validation rule but it is not working. It says "Method personId does not exist".
I created a folder: /src/Model/Validation inside I have 3 files: *BrValidation.phpLocalizedValidation.phpValidationInterface.php* *In BrValidation.php:* <?php /** * Brazillian Localized Validation class. Handles localized validation for Brazil. * * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * * @copyright Copyright (c) Cake Software Foundation, Inc. ( http://cakefoundation.org) * @link http://cakephp.org * @since Localized Plugin v 0.1 * @license http://www.opensource.org/licenses/mit-license.php MIT License */ App::uses('LocalizedValidation', 'Validation'); /** * BrValidation * */ class BrValidation extends LocalizedValidation { /** * Checks a phone number for Brazil. * * @param string $check The value to check. * @return bool Success. */ public static function phone($check) { return (bool)preg_match('/^(\+?\d{1,3}? ?)?(\(0?\d{2}\) ?)?9?\d{4}[-. ]?\d{4}$/', $check); } /** * Checks a postal code (CEP) for Brazil. * * @param string $check The value to check. * @return bool Success. */ public static function postal($check) { return (bool)preg_match('/^[0-9]{5}-?[0-9]{3}$/', $check); } /** * Checks SSN for Brazil. * * @param string $check The value to check. * @return bool Success. * @throws NotImplementedException */ public static function personId($check) { return BrValidation::cpf($check) || BrValidation::cnpj($check); } /** * Checks CPF for Brazil. * * @param string $check The value to check. * @return bool Success. */ public static function cpf($check) { $check = trim($check); // sometimes the user submits a masked CNPJ if (preg_match('/^\d\d\d.\d\d\d.\d\d\d\-\d\d/', $check)) { $check = str_replace(array('-', '.', '/'), '', $check); } elseif (!ctype_digit($check)) { return false; } if (strlen($check) != 11) { return false; } // repeated values are invalid, but algorithms fails to check it for ($i = 0; $i < 10; $i++) { if (str_repeat($i, 11) === $check) { return false; } } $dv = substr($check, -2); for ($pos = 9; $pos <= 10; $pos++) { $sum = 0; $position = $pos + 1; for ($i = 0; $i <= $pos - 1; $i++, $position--) { $sum += $check[$i] * $position; } $div = $sum % 11; if ($div < 2) { $check[$pos] = 0; } else { $check[$pos] = 11 - $div; } } $dvRight = $check[9] * 10 + $check[10]; return ($dvRight == $dv); } /** * Checks CNPJ for Brazil. * * @param string $check The value to check. * @return bool Success. */ public static function cnpj($check) { $check = trim($check); // sometimes the user submits a masked CNPJ if (preg_match('/^\d\d.\d\d\d.\d\d\d\/\d\d\d\d\-\d\d/', $check)) { $check = str_replace(array('-', '.', '/'), '', $check); } elseif (!ctype_digit($check)) { return false; } if (strlen($check) != 14) { return false; } $firstSum = ($check[0] * 5) + ($check[1] * 4) + ($check[2] * 3) + ($check[3] * 2) + ($check[4] * 9) + ($check[5] * 8) + ($check[6] * 7) + ($check[7] * 6) + ($check[8] * 5) + ($check[9] * 4) + ($check[10] * 3) + ($check[11] * 2); $firstVerificationDigit = ($firstSum % 11) < 2 ? 0 : 11 - ($firstSum % 11); $secondSum = ($check[0] * 6) + ($check[1] * 5) + ($check[2] * 4) + ($check[3] * 3) + ($check[4] * 2) + ($check[5] * 9) + ($check[6] * 8) + ($check[7] * 7) + ($check[8] * 6) + ($check[9] * 5) + ($check[10] * 4) + ($check[11] * 3) + ($check[12] * 2); $secondVerificationDigit = ($secondSum % 11) < 2 ? 0 : 11 - ($secondSum % 11); return ($check[12] == $firstVerificationDigit) && ($check[13] == $secondVerificationDigit); } } ?> *In LocalizedValidation.php:* <?php /** * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * * @copyright Copyright (c) Cake Software Foundation, Inc. ( http://cakefoundation.org) * @link http://cakephp.org * @license http://www.opensource.org/licenses/mit-license.php MIT License */ App::uses('ValidationInterface', 'Validation'); /** * Localized Validation base class. * */ abstract class LocalizedValidation implements ValidationInterface { /** * Checks that a value is a valid identification number. * In the case of US this would be the Social Security Number (SSN). * * This is necessary for CakePHP2.x validation and BC compatibility. * * @param string $check The value to check. * @return bool Success. * @deprecated Use personId() instead. */ public static function ssn($check) { return static::personId($check); } } ?> *In ValidationInterface.php:* <?php /** * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * * @copyright Copyright (c) Cake Software Foundation, Inc. ( http://cakefoundation.org) * @link http://cakephp.org * @license http://www.opensource.org/licenses/mit-license.php MIT License */ /** * ValidationInterface defining some common base validation methods. * */ interface ValidationInterface { /** * Checks a phone number. * * @param string $string The value to check. * @return bool Success. */ public static function phone($string); /** * Checks a postal code. * * @param string $string The value to check. * @return bool Success. */ public static function postal($string); /** * Checks a country specific identification number. * * @param string $string The value to check. * @return bool Success. */ public static function personId($string); } ?> *In the BuyersTable.php where I call the validation ->add('cpf','valid',['rule' => 'personId']);* <?php namespace App\Model\Table; use App\Model\Entity\Buyer; use Cake\ORM\Query; use Cake\ORM\RulesChecker; use Cake\ORM\Table; use Cake\Validation\Validator; use Cake\Validation; /** * Buyers Model */ class BuyersTable extends Table { /** * Initialize method * * @param array $config The configuration for the Table. * @return void */ public function initialize(array $config) { $this->table('buyers'); $this->displayField('id'); $this->primaryKey('id'); $this->belongsTo('Users', [ 'foreignKey' => 'user_id', 'joinType' => 'INNER' ]); $this->belongsToMany('Genres', [ 'foreignKey' => 'buyer_id', 'targetForeignKey' => 'genre_id', 'joinTable' => 'buyers_genres' ]); } /** * Default validation rules. * * @param \Cake\Validation\Validator $validator Validator instance. * @return \Cake\Validation\Validator */ public function validationDefault(Validator $validator) { $validator ->add('id', 'valid', ['rule' => 'numeric']) ->allowEmpty('id', 'create'); $validator ->requirePresence('cpf', 'create') ->notEmpty('cpf') ->add('cpf','valid',['rule' => 'personId']); $validator ->requirePresence('gender', 'create') ->notEmpty('gender'); $validator ->add('birthdate', 'valid', ['rule' => 'date']) ->requirePresence('birthdate', 'create') ->notEmpty('birthdate'); return $validator; } /** * Returns a rules checker object that will be used for validating * application integrity. * * @param \Cake\ORM\RulesChecker $rules The rules object to be modified. * @return \Cake\ORM\RulesChecker */ public function buildRules(RulesChecker $rules) { $rules->add($rules->existsIn(['user_id'], 'Users')); $rules->add($rules->isUnique(['cpf'])); return $rules; } } 2015-06-13 14:03 GMT-03:00 Anthony GRASSIOT <[email protected]>: > well it can be anywhere. Personally I baked a plugin and put my class > under src/Model/Validation/FrenchValidation.php under the > App/Model/Validation namespace for example. > Le 13 juin 2015 17:52, "Paulo Terra" <[email protected]> a écrit : > >> Hi José and Anthony, Thank you for your help, but the explanation is very >> poor. It does not say how to find the validation class, the folder it >> should be, not even an example of how it should be constructed, the method >> and if you have more then one validation to construct. >> >> Is there a better documentation about this? >> >> thanks in advance. >> >> 2015-06-13 12:27 GMT-03:00 Paulo Terra <[email protected]>: >> >>> This is exactly what I was reading at the moment. Thanks José. >>> Em 13/06/2015 12:10, "Anthony GRASSIOT" <[email protected]> >>> escreveu: >>> >>>> he's refering to this: >>>> >>>> http://book.cakephp.org/3.0/en/core-libraries/validation.html#adding-validation-providers >>>> Le 13 juin 2015 16:49, "Paulo Terra" <[email protected]> a écrit : >>>> >>>>> Thank you José for your reply. >>>>> >>>>> I need to validate brazilian ID´s CPF and CNPJ, brazilian ZIP Code and >>>>> phones. >>>>> >>>>> When you say custom validation provider do you mean the model in cake? >>>>> >>>>> >>>>> Em sábado, 13 de junho de 2015 04:13:25 UTC-3, José Lorenzo escreveu: >>>>>> >>>>>> There isn't yet. What do you need from the plugin? >>>>>> >>>>>> If it is the validation rules, you can just copy the rules from the >>>>>> old Localized plugin into your custom validation provider. >>>>>> >>>>>> On Friday, June 12, 2015 at 1:06:04 PM UTC+2, Paulo Terra wrote: >>>>>>> >>>>>>> Hi, I have been looking for a plugin to validate Person ID, Phone, >>>>>>> ZIP Code, etc, but all I can find is for old versions of CakePHP. Does >>>>>>> anybody know where I can find a working version for CakePHP 3.0? >>>>>>> >>>>>>> Thanks. >>>>>>> >>>>>>> Paulo Terra >>>>>>> >>>>>> -- >>>>> Like Us on FaceBook https://www.facebook.com/CakePHP >>>>> Find us on Twitter http://twitter.com/CakePHP >>>>> >>>>> --- >>>>> You received this message because you are subscribed to the Google >>>>> Groups "CakePHP" group. >>>>> To unsubscribe from this group and stop receiving emails from it, send >>>>> an email to [email protected]. >>>>> To post to this group, send email to [email protected]. >>>>> Visit this group at http://groups.google.com/group/cake-php. >>>>> For more options, visit https://groups.google.com/d/optout. >>>>> >>>> -- >>>> Like Us on FaceBook https://www.facebook.com/CakePHP >>>> Find us on Twitter http://twitter.com/CakePHP >>>> >>>> --- >>>> You received this message because you are subscribed to a topic in the >>>> Google Groups "CakePHP" group. >>>> To unsubscribe from this topic, visit >>>> https://groups.google.com/d/topic/cake-php/Zn05icCM6rQ/unsubscribe. >>>> To unsubscribe from this group and all its topics, send an email to >>>> [email protected]. >>>> To post to this group, send email to [email protected]. >>>> Visit this group at http://groups.google.com/group/cake-php. >>>> For more options, visit https://groups.google.com/d/optout. >>>> >>> >> -- >> Like Us on FaceBook https://www.facebook.com/CakePHP >> Find us on Twitter http://twitter.com/CakePHP >> >> --- >> You received this message because you are subscribed to the Google Groups >> "CakePHP" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To post to this group, send email to [email protected]. >> Visit this group at http://groups.google.com/group/cake-php. >> For more options, visit https://groups.google.com/d/optout. >> > -- > Like Us on FaceBook https://www.facebook.com/CakePHP > Find us on Twitter http://twitter.com/CakePHP > > --- > You received this message because you are subscribed to a topic in the > Google Groups "CakePHP" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/cake-php/Zn05icCM6rQ/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/cake-php. > For more options, visit https://groups.google.com/d/optout. > -- Like Us on FaceBook https://www.facebook.com/CakePHP Find us on Twitter http://twitter.com/CakePHP --- You received this message because you are subscribed to the Google Groups "CakePHP" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/cake-php. For more options, visit https://groups.google.com/d/optout.
