1) You may have problems if you open your application in another language:
Field('gender', requires= IS_IN_SET([T('Male'), T('Female')], 
zero=T('choose one'))),
should be
Field('gender', requires= IS_IN_SET([('Male',T('Male')), 
('Female',T('Female'))], zero=T('choose one'))),

2) The requirement over "father" or "mother" may be broader that you can 
think of (maybe the father should not also be one of the person's 
descendant (-: )
3) The problem with a custom validator is that it takes only one argument, 
which is the given value.
4) What you can do is to check this restriction after form processing:
form = SQLFORM(db.person, record, deletable=True)
form.process(...)
if form.accepted and form.vars.id == record.father:
    form.errors['father'] = T('This person cannot be his own father...')
    form.accepted = False
(do the same for mother)
if form.accepted:
    ...


Le mercredi 30 mai 2012 14:22:32 UTC+2, lcamara a écrit :
>
> Hey,
>
> I'm trying to validate some things on a self reference field, among them 
> that a reference field on a table does not refer to the record where it is.
>
> For instance,
>
> db.define_table('person',
>     Field('name', length=256, unique=True, requires=IS_NOT_EMPTY()),
>     Field('gender', requires= IS_IN_SET([T('Male'), T('Female')], 
> zero=T('choose one'))),
>     Field('father', 'reference person'),
>     Field('mother', 'reference person'),
>     format = '%(name)s'
>     )
>
> db.person.father.requires = IS_EMPTY_OR(IS_IN_DB(db(db.person.gender == 
> 'Male'), db.person.id, '%(name)s', zero=T('choose one'))) 
> db.person.mother.requires = IS_EMPTY_OR(IS_IN_DB(db(db.person.gender == 
> 'Female'), db.person.id, '%(name)s', zero=T('choose one')))
>
> So in addition to the gender restriction (yeah I know it's not always 
> true, this is just an example), I'd like to add the restriction that a 
> person cannot be it's own father or mother.
> Basically what I want is something like an IS_NOT_SELF or something.
>
> Any tips on how to go about this?
>

Reply via email to