On Wednesday, November 11, 2015 at 4:40:17 PM UTC-5, Pierre wrote:
>
>     items = db().select(db.c_sub.ALL)
>     form = SQLFORM.factory(*[Field(item.name, type='boolean', 
> default=False, comment=item.c_main.name) for item in items])
>

The IS_SLUG validation of values you are inserting into the db.c_sub table 
is working as expected. The problem is that you are attempting to create a 
form where the slug values become the names of the form fields. When using 
SQLFORM.factory to create a form, you must use the Field() constructor, but 
the "name" argument of Field() must be a valid Python identifier (so, 
hyphens are not allowed).

If you are OK with using underscores in place of hyphens, you could do 
something like:

    Field(item.name.replace('-', '_'), label=item.name, ...)

That will display the item.name as the label on the form but replace 
hyphens with underscores in the actual input element names. Alternatively, 
you could drop the "label" argument, in which case you'll get the default 
labels, which replace the underscores with spaces.

Alternatively, you could store the slugs themselves with underscores rather 
than hyphens, either by implementing a custom validator or using something 
like:

    Field('name', requires=[CLEANUP(r'-'), IS_SLUG(check=True, 
keep_underscores=True), IS_LOWER()])

Note, the above will allow submitted values with hyphens, but it will 
remove the hyphens. It will then check to make sure the string includes no 
special characters except underscores.

Finally, you could use the FORM helper instead of SQLFORM.factory to 
generate the form, as that will allow any names you want for the INPUT 
elements.

Anthony

-- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to