-
-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement a readonly and a disabled validator
- Loading branch information
Showing
6 changed files
with
184 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from tests.common import DummyPostData | ||
|
||
from wtforms import Form | ||
from wtforms import StringField | ||
from wtforms.validators import disabled | ||
|
||
|
||
def test_disabled(): | ||
class F(Form): | ||
disabled = StringField(validators=[disabled()]) | ||
|
||
form = F(disabled="foobar") | ||
assert "disabled" in form.disabled.flags | ||
assert ( | ||
form.disabled() | ||
== '<input disabled id="disabled" name="disabled" type="text" value="foobar">' | ||
) | ||
assert form.validate() | ||
|
||
form = F(DummyPostData(disabled=["foobar"]), data={"disabled": "foobar"}) | ||
assert form.validate() | ||
|
||
form = F(DummyPostData(disabled=["foobar"]), data={"disabled": "foobarbaz"}) | ||
assert not form.validate() | ||
|
||
form = F(DummyPostData(disabled=["foobar"])) | ||
assert not form.validate() | ||
|
||
|
||
def test_disabled_with_default(): | ||
class F(Form): | ||
disabled = StringField(validators=[disabled()], default="foobar") | ||
|
||
form = F(DummyPostData(disabled=["foobar"])) | ||
assert form.validate() | ||
|
||
form = F(DummyPostData(disabled=["foobar"]), data={"disabled": "foobarbaz"}) | ||
assert not form.validate() | ||
|
||
form = F(DummyPostData(disabled=["foobarbaz"]), data={"disabled": "foobarbaz"}) | ||
assert form.validate() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from tests.common import DummyPostData | ||
|
||
from wtforms import Form | ||
from wtforms import StringField | ||
from wtforms.validators import readonly | ||
|
||
|
||
def test_readonly(): | ||
class F(Form): | ||
ro = StringField(validators=[readonly()]) | ||
|
||
form = F(ro="foobar") | ||
assert "readonly" in form.ro.flags | ||
assert form.ro() == '<input id="ro" name="ro" readonly type="text" value="foobar">' | ||
assert form.validate() | ||
|
||
form = F(DummyPostData(ro=["foobar"]), data={"ro": "foobar"}) | ||
assert form.validate() | ||
|
||
form = F(DummyPostData(ro=["foobar"]), data={"ro": "foobarbaz"}) | ||
assert not form.validate() | ||
|
||
form = F(DummyPostData(ro=["foobar"])) | ||
assert not form.validate() | ||
|
||
|
||
def test_readonly_with_default(): | ||
class F(Form): | ||
ro = StringField(validators=[readonly()], default="foobar") | ||
|
||
form = F(DummyPostData(ro=["foobar"])) | ||
assert form.validate() | ||
|
||
form = F(DummyPostData(ro=["foobar"]), data={"ro": "foobarbaz"}) | ||
assert not form.validate() | ||
|
||
form = F(DummyPostData(ro=["foobarbaz"]), data={"ro": "foobarbaz"}) | ||
assert form.validate() |