On Wednesday, November 2, 2011 4:46:45 PM UTC-4, Archibald Linx wrote:
>
> Dear Web2py users, 
>
> I have a few questions about validators, especially : when are they 
> necessary ? 
>
> For example, if I have like in chapter 3 ( 
> http://www.web2py.com/book/default/chapter/03?search=image_id ) : 
>
> db.define_table('comment', 
>    Field('image_id', db.image)) 
>
> Is "db.comment.image_id.requires = IS_IN_DB(db, db.image.id)" or even 
> "db.comment.image_id.requires = IS_IN_DB(db, db.image.id, '% 
> (title)s')" redundant ? I am asking because one could think this is 
> already declared in the table definition.
>

Yes, those validators are necessary. Specifying Field('image_id', db.image) 
doesn't prevent inserting a value that isn't actually a db.image reference 
(it just tells the model that the field is intended to be a reference 
field, which among other things, causes the representation of the field to 
be the format attribute of the referenced table), so you need to enforce it 
with validators. Also, the IS_IN_DB validator results in the form widget 
displaying a dropdown.

Note, strictly speaking, it isn't necessary to explicitly specify the 
IS_IN_DB validator for a reference field because it is assigned by default, 
though you may want to specify it if you need to set options other than the 
defaults, which are IS_IN_DB(db,'<table>.id',db.<table>._format).

Anthony

Reply via email to