On Friday, December 5, 2014 4:21:20 AM UTC-5, Francisco Ribeiro wrote:
>
> Yes, Anthony you are right, there is no need to add more validators. I was 
> just thinking of a different naming convention to clear the ambiguity we 
> have now, because for checkboxes IS_NOT_EMPTY() means the same as 
> IS_EQUAL_TO(True).
>

Slight correction -- it would actually be IS_EQUAL_TO('on'), as "on" is the 
standard value given to checkbox input elements in SQLFORMs.
 

> Unfortunately, I don't think there is a way to achieve the behaviour I am 
> looking for without adding some JavaScript on the client side when the form 
> is generated in order to create a distinction between value not submitted 
> (which is what the IS_NOT_EMPTY() would use) and False.
>

Yes, you could achieve the behavior you want with a special validator that 
returns success whether it is fed "on" or None, but such a validator would 
be pointless.

Keep in mind, though, that there is not actually any distinction to be made 
on the client side -- "value not submitted" is literally the exact same 
thing as False (i.e., when the checkbox is checked, the input element's 
value is submitted, and when it is not checked, no value is submitted). 

Of course, you could use Javascript to detect when the form is being 
submitted, and in that case intercept the form submission, serialize the 
form elements, and add the checkbox name with a value such as "off", which 
would result in a value always being submitted for the checkbox (still, 
though, there would be no distinction between "off" and value not 
submitted). In that case, IS_NOT_EMPTY() would work as you would like it 
to, but it would serve no purpose, as it would always generate a successful 
validation. Much simpler just to remove the is_required attribute from 
boolean fields.

Note, the issue is not with the IS_NOT_EMPTY validator, which works exactly 
as it should. The issue is with the _validate method of the INPUT helper. 
For checkbox inputs, it uses request.vars.get(name) to retrieve the value 
of the checkbox element, and that returns None if the checkbox wasn't 
checked (i.e., the checkbox variable is not in request.vars in that case). 
If instead it used something like request.vars.get(name, 'off'), everything 
would work as you expected (though SQLFORM.accepts would also have to be 
changed to accommodate this). The problem with that approach is that the 
INPUT element itself can be used outside of SQLFORMs, and there is no 
obvious standard for an unchecked value, so the best approach is to remain 
consistent with how the browser works and simply leave the value as missing 
in case the box is not checked.

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