Yii2 url validation without http,https

Muhammad Shahzad picture Muhammad Shahzad · Feb 16, 2015 · Viewed 8.1k times · Source

I have used url validation code in my model,but it gives me error. I just validate if user only put like google.com or yahoo.com not http,www required. How i can do this.

 [['thankyou_page_url','confirmation_page_url'],'url'],

Answer

Jap Mul picture Jap Mul · Feb 16, 2015

I'm not able to test it right now. But I think you need to add the defaultScheme option. The default value of defaultScheme is null which means there must be a valid scheme in the URL.

If you change it to

[['thankyou_page_url','confirmation_page_url'],'url', 'defaultScheme' => 'http'],

the http:// part will automatically be added if you don't supply anything in the input. I think if you set the defaultScheme to an empty string it will also allow URL's without a scheme. So maybe this works:

[['thankyou_page_url','confirmation_page_url'],'url', 'defaultScheme' => ''],