Yii Framework Forum

url validation and http:// prefixes


(K Osforever) #1

So, since I am a new member I can’t post on the forum or something in that sense, whenever I try to post something the whole forum fails on me (see screenshot) saying that i can’t post urls or something…

anyway,

I’m looking to find a way to change the url validation so that it automatically prepends htt(and)p:// on values that don’t have it before doing the validation.

the issue i have (obviously could do it vi model->beforeValidate) is that this way I have to edit all models which is too messy for my linkings. So I’m looking for a cleaner way to do it globaly. I don’t care if it is going to be server or client side, I’m comfortable with both, I think I would prefer client side even though I think it would be quite a hack to do, register custom js and all…

So, I’m happy to hear some ideas as I said, all solutions are welcome.

cheers, k


(Gustavo) #2

extend CUrlValidator and then tell the rules to use yours, like this:


CValidator::$builtInValidators['url']='MyUrlValidator' 


(Phtamas) #3



class MyUrlValidator extends CUrlValidator

{

	public $defaultScheme = 'http';

}



That’s all you need to override.

CUrlValidator::$defaultScheme


(K Osforever) #4

So, I did implement what everybody told me here but never actually came back to affirm that.

so, thanks guys. both answers are very helpful!

greetings, k


(Marianbucur17) #5

By the way… you don’t really need to extend the validator.

All you have to do is this:


array('url', 'url', 'defaultScheme' => 'http')


(Baambooli333) #6

Thanks a lot. this is the easiest solution…


(rizwan062) #7

it works …