Okay, that is what I what I was anticipating when I asked, "Do the 
validators not check required fields?"

The behavior is still a little surprising to me, since 
validate_and_insert() is part of the DAL, and it has an explicit method of 
reporting errors.  It is also surprising to me that an error returns a 200 
code on an error, but maybe that is just a problem for my use case.

Wrapping the call with a try block and handling the exceptions seems to be 
working for me.

Thanks for the clarification!

On Monday, September 9, 2013 3:19:17 PM UTC-5, Anthony wrote:
>
> From the book:
>
> Notice that requires=... is enforced at the level of forms, required=True is 
> enforced at the level of the DAL (insert), while notnull,unique and 
> ondelete are enforced at the level of the database. While they sometimes 
> may seem redundant, it is important to maintain the distinction when 
> programming with the DAL.
>
> If you specified required=True, that is not handled by form validation or 
> .validate_and_insert(). Instead, that is enforced by the DAL during an 
> .insert(), and failure simply results in an exception being thrown, not a 
> validation error being returned.
>
> Anthony
>
> On Monday, September 9, 2013 12:07:46 PM UTC-4, Christopher Morlier wrote:
>>
>> At the moment, I am not explicitly providing any validators; so I expect 
>> only the default validators for my field types.
>>
>> Actually, the test where this arises is when I leave out a required 
>> field, and I get the error "Table: missing required field: time_stamp".  Do 
>> the validators not check required fields?
>>
>>
>> On Monday, September 9, 2013 10:28:56 AM UTC-5, Niphlod wrote:
>>>
>>> how do you specify the validators for your required fields ?
>>>
>>> On Monday, September 9, 2013 5:09:04 AM UTC+2, Christopher Morlier wrote:
>>>>
>>>> Hello,
>>>>
>>>> I am implementing a RESTful API and have the following code in my POST 
>>>> handler:
>>>>
>>>>     ret = db.recording.validate_and_insert(**fixed_fields)
>>>>     if ret.errors:
>>>>         raise HTTP(400, 'Validation failed: ' + str(ret.errors))
>>>>
>>>>     url = URL('api', args=('recording',ret.id))
>>>>     response.headers['Location'] = url
>>>>     response.status = 201
>>>>     return dict(link=A('Recording ', ret.id, _href=url))
>>>>
>>>> On success, things work as expected.  However, if I omit a required 
>>>> field, none of my code after validate_and_insert() is called.  The client 
>>>> receives a HTTP 200 response as if all is well, albeit with an appropriate 
>>>> text description of the error.
>>>>
>>>> This is not how validate_and_insert() is described in chapter 6 of the 
>>>> book.  Is there a reason it is different?
>>>>
>>>> As a work around I added a try-except block around the 
>>>>  validate_and_insert() call, expecting an HTTP exception, but discovered 
>>>> it 
>>>> is actually a RuntimeError exception.
>>>> Is this the recommended work around or is there a better solution?
>>>>
>>>> I browsed through the code, but it isn't clear to me where the 
>>>> exception is thrown.  However, I believe I noticed two typos in the 
>>>> "validate_and_insert, validate_and_update" section:"ret.error" should be 
>>>> "ret.errors" and "res.updated" should be "ret.updated".
>>>>
>>>> BTW, I am running 2.5.1-stable+timestamp.2013.06.06.15.39.19 on 
>>>> Apache/2.2.16 (Debian)
>>>>
>>>> Thanks for your help,
>>>> Chris
>>>>
>>>

-- 
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/groups/opt_out.

Reply via email to