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

Should form validation not be triggered when required fields are left blank? #30

Open
neilferreira opened this issue Aug 29, 2014 · 6 comments
Labels

Comments

@neilferreira
Copy link
Contributor

I just installed the datagridfield demo package. When you have a field that has required=True, even if that field is not entered the form still submits without any validation errors.

Is this a known issue?

@miohtama
Copy link
Contributor

I think this is rare use case: usually if the user needs to submit data you prepopulate the field, so that some columns are filled up and others need to be filled.

But depending how you wish to use the field this could be easily worked around having your own validators checking that you have at least 1 row of filled in stuff in the grid?

@neilferreira
Copy link
Contributor Author

Lets take this as an example

https://github.com/collective/collective.z3cform.datagridfield_demo/blob/master/collective/z3cform/datagridfield_demo/browser/simple.py#L56

If the user leaves their 'city' blank and presses submit, shouldn't there be an error saying they have skipped that value for the row? Do you have an example of where you've done manual validation to do what I'm after?

@miohtama
Copy link
Contributor

Hmm... that should happen. Smells like a bug.

@neilferreira
Copy link
Contributor Author

Can you please confirm that you get this behaviour as well? Any ideas how much effort a fix would be?

@miohtama
Copy link
Contributor

miohtama commented Sep 1, 2014

I don't have any Plones where this is installed at the moment. But I'll dig up some old version where I know it should be working (as the bug might be also caused by some internal changes in z3c.form, plone.app.z3cform, etc.)

@jensens jensens added the bug label Jul 30, 2015
@mbarde
Copy link

mbarde commented Aug 1, 2019

You can write a custom widget validator (see here) to implement described behavior:

class MyListValidator(validator.SimpleFieldValidator):
    def validate(self, value):
        if self.field.required and len(value) == 0:
            raise zope.interface.Invalid(_(u'Please enter something!'))

validator.WidgetValidatorDiscriminators(MyListValidator, field=schema.List)
# or for a single field: validator.WidgetValidatorDiscriminators(MyListValidator, field=IMyForm['field_name']

In zcml: <adapter factory=".myform.MyListValidator" />

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

No branches or pull requests

4 participants