2

I have created a validation rule that checks to see if a url actually exists. I can make it work fine if I implement it as a custom rule within my tables validators. However I would like to make it reusable... I have tried a few different ways and I either get told that the method does not exist or cannot be found, or that I am calling to a member function on NULL My current error is:

Error: Call to a member function add() on null

I am fairly new to MVC programming and very new to Cakephp

As per the documentation (and my understanding of it) here is my new validator 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;
            }
        ]);

    }
}
?>

here is my Table (I have deleted all of the validator rules but the one I am trying to get to work for this example):

<?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'];
            }
        }
    }
}

if someone could point me in the right direction or even show me a working example of how to do this it would be greatly appreciated.

David dB
  • 69
  • 1
  • 8
  • I think here is what u want. http://stackoverflow.com/questions/10958727/cakephp-import-controller good luck. – Netick Jun 04 '16 at 00:15

1 Answers1

0

Just create an object of 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;
        }
    ]);

}
Ravi Kumar
  • 176
  • 1
  • 9