Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent validators from getting inserted twice #30

Merged
merged 1 commit into from
Oct 27, 2015

Conversation

pawl
Copy link
Contributor

@pawl pawl commented Oct 27, 2015

Related to this flask-admin issue: pallets-eco/flask-admin#1094

Edit: See this unit test for an example of the issue: https://github.com/coleifer/wtf-peewee/pull/32/files

If you override field_args with your own dict, the changes made to kwargs in convert() will also be made to the dict you used to override field_args. The objects that get copied to kwargs from field_args are only references, so you need to deepcopy to prevent the issue.

Related to this flask-admin issue: pallets-eco/flask-admin#1094

If you override ```field_args``` with your own dict, the changes made to ```kwargs``` in ```convert()``` will also be made to the dict you used to override ```field_args```. The objects that get copied to ```kwargs``` from ```field_args``` are only references, so you need to deepcopy to prevent the issue.
coleifer added a commit that referenced this pull request Oct 27, 2015
Prevent validators from getting inserted twice
@coleifer coleifer merged commit 0377315 into coleifer:master Oct 27, 2015
@coleifer
Copy link
Owner

Danke

@ak4nv
Copy link
Contributor

ak4nv commented Dec 1, 2015

I've got an error in my way with deepcode using.
TypeError: cannot deepcopy this pattern object

PolicyForm = model_form(Policy, Form,
                        field_args={
                            'pol_num': dict(validators=[optional()]),
                            'pol_reg': dict(validators=[optional()])})

Python 3.5 with latest flask, peewee, wtforms and wtfpeewee

@pawl
Copy link
Contributor Author

pawl commented Dec 1, 2015

I wasn't able to reproduce the issue in python 2 or 3 using validators.Optional() from wtforms 2.0.2: https://gist.github.com/pawl/9e12f0f8d0d482ece2a7

How are you defining optional()?

I can only reproduce the error when I do something like:

BlogForm = model_form(Blog, field_args={
    'name': dict(validators=[re.compile('eam3')]),
})

@ak4nv
Copy link
Contributor

ak4nv commented Dec 1, 2015

Sorry, I used modify code... there is full part:

from wtforms.validators import required, regexp, length

digits_only = regexp('^\d+$', message='Допустимы только цифры.')
fixed_length = lambda x: length(min=x, max=x)

PolicyForm = model_form(Policy, Form,
                        field_args={
                            'pol_num': dict(validators=[digits_only]),
                            'pol_reg': dict(validators=[digits_only, fixed_length(2)])})

But I tried with optional() from wtf.validarors and got the same error.

@pawl
Copy link
Contributor Author

pawl commented Dec 1, 2015

Hmm, I'm getting the same issue.

I guess I'll need to figure out a different way to prevent field_args from getting modified.

coleifer added a commit that referenced this pull request Dec 1, 2015
@ak4nv
Copy link
Contributor

ak4nv commented Dec 2, 2015

@coleifer set tag please to last commit 0.2.6(?) ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants