如何实现我为 cakephp 3 创建的可重用验证器?
How do I implement a reusable validator that I have created for cakephp 3?
我创建了一个验证规则来检查 url 是否确实存在。如果我将它作为我的表验证器中的自定义规则来实现,我可以让它正常工作。但是我想让它可重用......我尝试了几种不同的方法,但我要么被告知该方法不存在或找不到,要么我正在调用 NULL 上的成员函数
我当前的错误是:
Error: Call to a member function add() on null
我对 MVC 编程还很陌生,对 Cakephp 也很陌生
根据文档(以及我对它的理解)这是我的新验证器 class:
<?php
// In src/Model/Validation/ContactValidator.php
namespace App\Model\Validation;
use Cake\Validation\Validator;
class ContactValidator extends Validator
{
public function __construct()
{
parent::__construct();
$validator
->add('validDomain','custom',[
'rule' => function($value){
$url = parse_url($value);
$host = $url['host'];
if($host != gethostbyname($host)){
return true;
}
return false;
}
]);
}
}
?>
这是我的 Table(我已经删除了所有验证器规则,但我试图在这个例子中使用的规则):
<?php
namespace App\Model\Table;
use App\Model\Entity\Agent;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use Cake\Event\Event, ArrayObject;
/**
* Agents Model
*
* @property \Cake\ORM\Association\BelongsTo $Users
* @property \Cake\ORM\Association\BelongsTo $Agencies
* @property \Cake\ORM\Association\HasMany $Pictures
* @property \Cake\ORM\Association\HasMany $Properties
* @property \Cake\ORM\Association\HasMany $SoldProperties
* @property \Cake\ORM\Association\BelongsToMany $Regions
*/
class AgentsTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->table('agents');
$this->displayField('user_id');
$this->primaryKey('user_id');
$this->addBehavior('Timestamp');
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Agencies', [
'foreignKey' => 'agency_id'
]);
$this->hasMany('Pictures', [
'foreignKey' => 'agent_id'
]);
$this->hasMany('Properties', [
'foreignKey' => 'agent_id'
]);
$this->hasMany('SoldProperties', [
'foreignKey' => 'agent_id'
]);
$this->belongsToMany('Regions', [
'foreignKey' => 'agent_id',
'targetForeignKey' => 'region_id',
'joinTable' => 'agents_regions'
]);
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator = new \App\Model\Validation\ContactValidator;
$validator
->add('agency_domain', 'valid',['rule' => 'validDomain', 'provider'=>'ContactValidator', 'message' => 'The url you have supplied does not exist!']);
return $validator;
}
public function isOwnedBy($userId)
{
return $this->exists(['user_id' => $userId]);
}
/**
* 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 beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options)
{
if(isset($data['agency_domain']))
{
$data['agency_domain']=strtolower($data['agency_domain']);
if(strpos($data['agency_domain'],"http")===false){
$data['agency_domain'] = "http://".$data['agency_domain'];
}
}
}
}
如果有人能为我指出正确的方向,甚至向我展示如何执行此操作的工作示例,我将不胜感激。
只需创建一个Validator对象class。
public function __construct()
{
parent::__construct();
$validator = new Validator();
$validator
->add('validDomain','custom',[
'rule' => function($value){
$url = parse_url($value);
$host = $url['host'];
if($host != gethostbyname($host)){
return true;
}
return false;
}
]);
}
我创建了一个验证规则来检查 url 是否确实存在。如果我将它作为我的表验证器中的自定义规则来实现,我可以让它正常工作。但是我想让它可重用......我尝试了几种不同的方法,但我要么被告知该方法不存在或找不到,要么我正在调用 NULL 上的成员函数 我当前的错误是:
Error: Call to a member function add() on null
我对 MVC 编程还很陌生,对 Cakephp 也很陌生
根据文档(以及我对它的理解)这是我的新验证器 class:
<?php
// In src/Model/Validation/ContactValidator.php
namespace App\Model\Validation;
use Cake\Validation\Validator;
class ContactValidator extends Validator
{
public function __construct()
{
parent::__construct();
$validator
->add('validDomain','custom',[
'rule' => function($value){
$url = parse_url($value);
$host = $url['host'];
if($host != gethostbyname($host)){
return true;
}
return false;
}
]);
}
}
?>
这是我的 Table(我已经删除了所有验证器规则,但我试图在这个例子中使用的规则):
<?php
namespace App\Model\Table;
use App\Model\Entity\Agent;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use Cake\Event\Event, ArrayObject;
/**
* Agents Model
*
* @property \Cake\ORM\Association\BelongsTo $Users
* @property \Cake\ORM\Association\BelongsTo $Agencies
* @property \Cake\ORM\Association\HasMany $Pictures
* @property \Cake\ORM\Association\HasMany $Properties
* @property \Cake\ORM\Association\HasMany $SoldProperties
* @property \Cake\ORM\Association\BelongsToMany $Regions
*/
class AgentsTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->table('agents');
$this->displayField('user_id');
$this->primaryKey('user_id');
$this->addBehavior('Timestamp');
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Agencies', [
'foreignKey' => 'agency_id'
]);
$this->hasMany('Pictures', [
'foreignKey' => 'agent_id'
]);
$this->hasMany('Properties', [
'foreignKey' => 'agent_id'
]);
$this->hasMany('SoldProperties', [
'foreignKey' => 'agent_id'
]);
$this->belongsToMany('Regions', [
'foreignKey' => 'agent_id',
'targetForeignKey' => 'region_id',
'joinTable' => 'agents_regions'
]);
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator = new \App\Model\Validation\ContactValidator;
$validator
->add('agency_domain', 'valid',['rule' => 'validDomain', 'provider'=>'ContactValidator', 'message' => 'The url you have supplied does not exist!']);
return $validator;
}
public function isOwnedBy($userId)
{
return $this->exists(['user_id' => $userId]);
}
/**
* 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 beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options)
{
if(isset($data['agency_domain']))
{
$data['agency_domain']=strtolower($data['agency_domain']);
if(strpos($data['agency_domain'],"http")===false){
$data['agency_domain'] = "http://".$data['agency_domain'];
}
}
}
}
如果有人能为我指出正确的方向,甚至向我展示如何执行此操作的工作示例,我将不胜感激。
只需创建一个Validator对象class。
public function __construct()
{
parent::__construct();
$validator = new Validator();
$validator
->add('validDomain','custom',[
'rule' => function($value){
$url = parse_url($value);
$host = $url['host'];
if($host != gethostbyname($host)){
return true;
}
return false;
}
]);
}