views:

54

answers:

1

I want to ovveride the default url() validation method in CakePHP, since it does not allow the use of ~ inside urls. I thought it would be enough to declare a url() method in AppModel, but it seems that core methods have the precedence with respect to user defined ones.

I think (but I have not tried) one possible way would be to use

$validate = array('url' => array(
    'rule' => array('Userdefined', 'url'),
    'message' => 'This is not an URL!!!'
));

or something like that (what is the correct sintax?). But this is not completely satisfying.

Indeed I pass the $validate variable as a JSON object to my javascript, and then I do client validation accordingly. Basically I have rewritten part of the CakePHP validation automagic in javascript. So I really want to have

$validate = array('url' => array(
    'rule' => 'url',
    'message' => 'This is not an URL!!!'
));

in order not to break client-side validation.

EDIT: It turns out I were wrong. The problem is that methods in Validation are called differently from methods in Model, so one has to pay attention when copying/pasting.

The first difference is that $check will now be an array instead of a string, but this I already figured out. What I did not realize is that another array of parameters is passed to Validation methods in Model. Since the signature of url() was

url($check, $strict = false)

the result was that $strict always had the value true, thereby requiring full URLs with protocol prefix. Seeing that the intended URL with tilde was not validating I assumed that the problem was that CakePHP still used the old method.

+1  A: 

Why not just use the custom validation and make an url validation function with a different name?

Otherwise the manual says that you can override the Validation classes methods with functions in either the AppModel, Model, or Behaviors.

Here is the relevant link in the book. http://book.cakephp.org/view/150/Custom-Validation-Rules#Adding-your-own-Validation-Methods-152

Abba Bryant
I was wrong, I explain better in the question itself.
Andrea
glad you seem to have found the issue
Abba Bryant