It seems like you are aiming for an odd user experience -- in some cases
you provide a select widget that has no options, and when the user submits
the form, it returns an error saying the field cannot be empty -- yet there
is no way to provide a value. What is the user supposed to do in that case?
Anyway, you can do it this way:
Field("value", type='string', label=field_name, default=value,
requires=IS_IN_SET(set, multiple=(1, 2), error_message='Field cannot
be empty'),
widget=SQLFORM.widgets.options.widget)
Setting multiple=(1, 2) indicates there must be at least one option
selected but less than 2 (i.e., so, exactly 1). However, by specifying the
"multiple" argument, the default widget would become the multi-select
widget, so to get a standard select widget, you must also specify that via
the "widget" argument.
Anthony
On Friday, May 12, 2017 at 2:43:00 AM UTC-4, Karoly Kantor wrote:
>
> I have a situation where the "set' in IS_IN_SET(set) is dynamic, and
> sometimes will be empty in:
>
> form = SQLFORM.factory(
> Field("value", type='string', label=field_name, default=value,
> requires=IS_IN_SET(set)),
> ... more fields ...
> )
>
> I would expect that if 'set' is empty, this would always fail validation.
> However, this one allows saving an empty value.
>
> How could I make it to always fail validation if the set is empty?
>
> I also tried this:
>
> requires= [IS_NOT_EMPTY(), IS_IN_SET(set) ]
>
> But this one brings up a simple text input field and accepts any text I
> type.
>
> Thank you.
>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.