Many thanks Anthony!

That looks like it will work for now...I may specifically handle call that 
code just before validate_and_insert calls in the web app, so I don't get 
rid of readURL validation in a place where I need it.

On Friday, April 21, 2017 at 4:46:51 PM UTC-4, Anthony wrote:
>
> Thanks. I updated the Github issue, as there are multiple problems with 
> the current implementation of the .validate_and_ methods.
>
> For now, you should be able to get around the two issues you observe by 
> removing the validators:
>
> db.story.titleAsSlug.requires = db.story.readURL.requires = None
>
> You can probably permanently set the titleAsSlug requires to None, as it 
> will not appear in forms anyway. The default readURL validator, on the 
> other hand, may be needed if you intend to allow inserts/updates directly 
> via SQLFORM.process().
>
> The problem is that the default validator assigned to titleAsSlug 
> transforms None to '', and the default validator for readURL transforms 
> None to 'None'. These values then get inserted in the record.
>
> Anthony
>
> On Friday, April 21, 2017 at 9:48:53 AM UTC-4, Chris wrote:
>>
>> Can do, here's an example:
>>
>> # DB
>>
>> db.define_table('story',
>>     Field('title',
>>           length=512,
>>           widget=lambda field, value: SQLFORM.widgets.string.widget(field
>> ,
>>                                         value,
>>                                         _size=40),
>>           requires=[IS_NOT_EMPTY(), IS_LENGTH(minsize=1, maxsize=512)]),
>>     Field('titleAsSlug',
>>           compute=lambda(r): urls.convert_to_slug(r['title'])),
>>     Field('readURL', unique=True, label=T('URL'), required=False,
>>           widget=lambda field, value:
>>           SQLFORM.widgets.string.widget(field,
>>                                         value,
>>                                         _size=60,
>>                                         _placeholder='
>> http://www.example.com')),
>> )
>>
>> # Test
>>
>> class TestModels(unittest.TestCase):
>>     def testStoryNewCreate(self):
>>         dct_new_story = {
>>                          "title": "Unit Test"}
>>         new_story = db.story.validate_and_insert(**dct_new_story)
>>         self.assertFalse(new_story["errors"], "Error inserting a new 
>> story: " + str(new_story)) 
>>
>> The readURL field generates an error that the entry is found in the 
>> database. Unique=True is a DB constraint according to the web2py manual and 
>> NULLs in PostgreSQL and SQL in general don't violate the unique constraint. 
>> Web2py attaches an IS_NOT_IN_DB validator that converts None to "None" 
>> instead of null, which only works until there is a row with a readURL of 
>> 'None'.
>> The titleAsSlug field is set to an empty string by the validator, which 
>> is perhaps why validate_and_insert doesn't compute it where insert does.
>>
>> Hope that's helpful, thanks for anything you can do here!
>>
>>
>> On Thursday, April 20, 2017 at 11:05:02 PM UTC-4, Anthony wrote:
>>>
>>> Need to see the fields.
>>
>>

-- 
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