__field_validators__ seems to conflict with __require_fields__

18 views
Skip to first unread message

Dax Mitchell

unread,
Jul 1, 2019, 5:37:23 PM7/1/19
to TurboGears
Hi, 

We have a CrudRestControllerConfig and define our defaultCrudRestController with __form_options__.

Suppose we have a field 'my_field' and it's included in the __form_options__'s __require_fields__ directive:

...
'__require_fields__': ['my_field'],
...

Then if that field is left blank, TG2 will show an error and tell the user to 'Please enter a value'.

However, we have defined a custom validator for that field that looks something like this:

class MyValidator(FancyValidator):
    def __init__(self, *args, **kw):
        FancyValidator.__init__(self, *args, **kw)

    messages = dict(MyError=_('MyField Error'))

    def _validate_python(self, value, state):

        if value is None or not isinstance(value, str):
            raise Invalid(self.message('MyError', state), value, state)

        if not is_my_value_valid(value):
            raise Invalid(self.message('MyError', state), value, state)

If the field is left empty, the __require_fields__ mechanism doesn't seem to get invoked and the validator is not called either.  

The empty value is accepted without issue. 

Is this a known issue or am I doing something foolish?

Many thanks

Alessandro Molina

unread,
Jul 2, 2019, 4:21:33 PM7/2/19
to TurboGears
How are you providing the "__field_validators__" value? 
If you are using "__field_validators__" that expects the actual validator instance, so the "__require_fields__" make no sense at that point because you already provided "required=True" or no to the __init__ of the validator set in "__field_validators__".

I suspect you might want to use "__field_validator_types__" instead of "__field_validators__", that should initialise the instance of the validator itself and provide the "required=True" if the field is in "__require_fields__"

--
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To unsubscribe from this group and stop receiving emails from it, send an email to turbogears+...@googlegroups.com.
To post to this group, send email to turbo...@googlegroups.com.
Visit this group at https://groups.google.com/group/turbogears.
To view this discussion on the web visit https://groups.google.com/d/msgid/turbogears/e2f2cf9a-4423-4575-ab8e-c7acff5a824e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dax Mitchell

unread,
Jul 11, 2019, 9:46:17 AM7/11/19
to TurboGears
That worked, thanks very much!


On Tuesday, July 2, 2019 at 4:21:33 PM UTC-4, Alessandro Molina wrote:
How are you providing the "__field_validators__" value? 
If you are using "__field_validators__" that expects the actual validator instance, so the "__require_fields__" make no sense at that point because you already provided "required=True" or no to the __init__ of the validator set in "__field_validators__".

I suspect you might want to use "__field_validator_types__" instead of "__field_validators__", that should initialise the instance of the validator itself and provide the "required=True" if the field is in "__require_fields__"

On Mon, Jul 1, 2019 at 11:37 PM Dax Mitchell <dax.j....@gmail.com> wrote:
Hi, 

We have a CrudRestControllerConfig and define our defaultCrudRestController with __form_options__.

Suppose we have a field 'my_field' and it's included in the __form_options__'s __require_fields__ directive:

...
'__require_fields__': ['my_field'],
...

Then if that field is left blank, TG2 will show an error and tell the user to 'Please enter a value'.

However, we have defined a custom validator for that field that looks something like this:

class MyValidator(FancyValidator):
    def __init__(self, *args, **kw):
        FancyValidator.__init__(self, *args, **kw)

    messages = dict(MyError=_('MyField Error'))

    def _validate_python(self, value, state):

        if value is None or not isinstance(value, str):
            raise Invalid(self.message('MyError', state), value, state)

        if not is_my_value_valid(value):
            raise Invalid(self.message('MyError', state), value, state)

If the field is left empty, the __require_fields__ mechanism doesn't seem to get invoked and the validator is not called either.  

The empty value is accepted without issue. 

Is this a known issue or am I doing something foolish?

Many thanks

--
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To unsubscribe from this group and stop receiving emails from it, send an email to turbo...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages