我创建了一个验证规则,用于检查网址是否确实存在。如果我在表格验证器中将其实现为自定义规则,我可以使其正常工作。但是我想让它可重用...我尝试了几种不同的方法,我要么被告知该方法不存在或无法找到,或者我在NULL上调用成员函数 我目前的错误是:
错误:在null
上调用成员函数add()
我是MVC编程的新手,也是Cakephp的新手
根据文档(以及我对它的理解),这是我的新验证器类:
<?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;
}
]);
}
}
?>
这是我的表(我删除了所有验证器规则,但我试图为此示例工作的那个):
<?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'];
}
}
}
}
如果有人能指出我正确的方向,或者甚至向我展示如何做到这一点的工作示例,我们将不胜感激。
答案 0 :(得分:0)
只需创建Validator类的对象。
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;
}
]);
}