Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
SRCT
go
Commits
74f51dea
Commit
74f51dea
authored
Dec 20, 2013
by
Jean Michel Rouly
Browse files
Validate forms for previously existing shorts.
parent
a7c97649
Changes
1
Show whitespace changes
Inline
Side-by-side
go/go/forms.py
View file @
74f51dea
from
django
import
forms
from
go.models
import
URL
from
django.core.validators
import
MinLengthValidator
,
MinValueValidator
,
RegexValidator
from
django.core.exceptions
import
ValidationError
class
URLForm
(
forms
.
ModelForm
):
...
...
@@ -31,12 +32,19 @@ class URLForm( forms.ModelForm ):
'Only letters are allowed.'
)
def
unique_short
(
value
):
try
:
URL
.
objects
.
get
(
short__iexact
=
value
)
except
URL
.
DoesNotExist
:
return
raise
ValidationError
(
'Short url already exists.'
)
# Custom short-url field with validators.
short
=
forms
.
CharField
(
required
=
False
,
label
=
'Short URL (Optional)'
,
widget
=
forms
.
TextInput
(
attrs
=
{}),
validators
=
[
alphanumeric
],
validators
=
[
alphanumeric
,
unique_short
],
max_length
=
20
,
min_length
=
3
,
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment