Monday, December 1, 2014

Re: How to create a provider of validation

Firstly, your Validation Provider is better situated in your src/ directory (I personally use src/Model/Validation/Provider), so the filename will be src/Model/Validation/Provider/Validation.php and the FQN will be App\Model\Validation\Provider\Validation.php

Then in your ProjectValidation file, you would add:

$this->validator->provider('myProvider', 'App\Model\Validation\Provider\Validation');
into your constructor.

Your '????' would then simply be 'myProvider'

As for your actual validation rule, please try not to directly use SQL. Your query is better written as:

$count = TableRegistry::get('Catalogue')
->find()
->where(['t_identifiant' => $check])
->count();

On Monday, 1 December 2014 17:02:46 UTC+2, Ernaelsten Gérard wrote:
Hello,
I try to create my own validation, But I have an error of provider because I do not know what to put.

//vendor/maitrepylos/Validation.php

<?php

namespace Maitrepylos;


use Cake\Datasource\ConnectionManager;

class Validation extends \Cake\Validation\Validation {


public static function uniqueIdentifiant($check){

$pdo = ConnectionManager::get('default');
$sql = 'SELECT count(*) as compteur FROM catalogue WHERE t_identifiant = ?';
$r = $pdo->prepare($sql);
$r->execute([$check]);
$d = $r->fetch(\PDO::FETCH_OBJ);

if($d->compteur > 0){
return false;
}

return true;



}

}


//App/Model/Validation/ProjectValidation.php

<?php
namespace App\Model\Validation;


use Cake\Datasource\ConnectionManager;
use Cake\Validation\Validator;

class ProjectValidation
{
public $validator = null;
public function __construct()
{
$this->validator = new Validator();


}

public function newFiche(){

$this->validator->requirePresence('t_titre', true)
->notEmpty('t_titre', 'Le nom de la fiche ne peut être absente')
->notEmpty('d_date_creation','La date ne peut être vide')
->add('d_date_creation',[
'checkDate'=>[
'rule'=>['date','dmy'],
'message' => 'La date doit-être au format d/m/Y'
]
])
->notEmpty('t_identifiant', 'L\'identifiant ne peut être vide')
->add('t_identifiant',[
'single' =>
[
'rule' => ['uniqueIdentifiant'],
'provider'=>'??????????????????????????', //I don't know
'message' => 'test unique'
]

]);

return $this->validator;

}


}
Thank you for your help

--
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 cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: