feat: Add URL option for data type fields

This commit is contained in:
Hussain Nagaria 2021-04-14 19:50:08 +05:30
parent 6324756d78
commit 82b98330fd
5 changed files with 26 additions and 2 deletions

View file

@ -71,7 +71,8 @@ numeric_fieldtypes = (
data_field_options = (
'Email',
'Name',
'Phone'
'Phone',
'URL'
)
default_fields = (

View file

@ -666,6 +666,9 @@ class BaseDocument(object):
if data_field_options == "Phone":
frappe.utils.validate_phone_number(data, throw=True)
if data_field_options == "URL":
frappe.utils.validate_url(data, throw=True)
def _validate_constants(self):
if frappe.flags.in_import or self.is_new() or self.flags.ignore_validate_constants:
return

View file

@ -126,6 +126,9 @@ frappe.ui.form.ControlData = frappe.ui.form.ControlInput.extend({
this.df.invalid = email_invalid;
return v;
}
} else if (this.df.options == 'URL') {
this.df.invalid = !validate_url(v);
return v;
} else {
return v;
}

View file

@ -52,6 +52,10 @@ window.validate_name = function(txt) {
return frappe.utils.validate_type(txt, "name");
};
window.validate_url = function(txt) {
return frappe.utils.validate_type(txt, "url");
}
window.nth = function(number) {
number = cint(number);
var s = 'th';

View file

@ -19,7 +19,7 @@ from gzip import GzipFile
from typing import Generator, Iterable
from six import string_types, text_type
from six.moves.urllib.parse import quote
from six.moves.urllib.parse import quote, urlparse
from werkzeug.test import Client
import frappe
@ -161,6 +161,19 @@ def split_emails(txt):
return email_list
def validate_url(txt, throw=False):
try:
url = urlparse(txt).netloc
if not url:
raise frappe.ValidationError
except Exception as e:
if throw:
frappe.throw(
frappe._("<strong>'{0}'</strong> is not a valid URL").format(txt)
)
return False
def random_string(length):
"""generate a random string"""
import string