Muhammad Shahzad
Muhammad Shahzad

Reputation: 9652

Yii2 url validation without http,https

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'],

Upvotes: 5

Views: 10317

Answers (2)

Alex S
Alex S

Reputation: 749

It's pretty simple, just remove shemes part from a pattern

['url', 'url', 'pattern'=>'/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(?::\d{1,5})?(?:$|[?\/#])/i'],

Upvotes: 0

Jap Mul
Jap Mul

Reputation: 18769

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' => ''],

Upvotes: 8

Related Questions