0
votes

I am trying to add a validation rule to my Model in CakePHP to check that an ip address is unique. The problem is that I am saving the ip address in my database as an unsigned int, but the user is entering it as a string. To do this I am using a beforeSave function that changes the ip address to the int value that will be saved. Is there a way to make the isUnique rule run after the beforeSave function? Currently my validation rules look like this.

    public $validate = array(
    'ip_address' => array(
        'notEmpty' => array(
            'rule' => 'notEmpty',
            'message' => 'You must enter an IP address'
        ),
        'unique' => array(
            'rule' => 'isUnique',
            'required' => 'create',
            'message' => 'This IP address already exists'
        )
    )
);
1

1 Answers

1
votes

Do this in beforeValidate():

$this->data['alias']['ip_address'] = str_replace('.', '', $this->data['alias']['ip_address'];

And it will work fine. Why int and not string by the way? int might give you duplicates. I don't know your code to turn the ip into an int so I might be wrong.