You can always change requires on the fly
def test():
if request.vars.year:
try:
y = int(request.vars.year)
except 'ValueError':
y = 0
if y > 1975:
db.cars.catalytic_converter_type.requires =
IS_IN_SET(['one','two','three'], error_message='cars later than 1975
must have at least one')
else:
db.cars.catalytic_converter_type.requires = IS_EQUAL_TO('',
error_message='cars before 1975 should not have one')
f=SQLFORM(db.cars)
if f.accepts(request.vars, session):
response.flash='accepted'
elif f.errors:
response.flash='please fix and re-submit'
return dict(f=f)
On Nov 26, 7:17 pm, "mr.freeze" <[email protected]> wrote:
> Good point. Here is my use case:
>
> db.define_table('cars',
> Field('make'),
> Field('model'),
> Field('year'),
> Field('catalytic_converter_type',requires =
> IS_IN_SET('one','two','three'),
> comment='for cars later than 1975'))
>
> I want catalytic_converter_type to be required if the car year is
> greater than 1975. I also want to show an error if
> catalytic_converter_type is selected and the year is less than 1975.
> Allowing the user to see all errors at once would be a bonus but not a
> deal breaker. onvalidation seems like the place to handle it but let
> me know if there is a better way.
>
> On Nov 26, 5:48 pm, mdipierro <[email protected]> wrote:
>
> > Consider this case:
>
> > db.define_table('numbers',Field('a','integre'),Field('b','integer'))
> > def validate(form):
> > if form.vars.a+form.vars.b>100:
> > form.errors.b="a+b must not exceed 100"
> > form=crud.create(db.numbers,onvalidation=validate)
>
> > Before the change the function validate is not called if a and b are
> > not valid integer this never triggering an exception, only validation
> > errors.
>
> > With your proposed change this would cause an error because
> > form.vars.a is None if request.vars.a does ot pass the default integer
> > validation. The function validation would be called again and issue a
> > ticket.
>
> > This is a change of behavior. I am not convinced this change is a good
> > idea.
>
> > Why do you need it?
> > Other opinions?
>
> > Massimo
>
> > On Nov 26, 11:34 am, "mr.freeze" <[email protected]> wrote:
>
> > > Changing line 1565 of html.py from...
> > > if status and onvalidation:
> > > to...
> > > if vars and onvalidation:
>
> > > On Nov 26, 11:28 am, mdipierro <[email protected]> wrote:
>
> > > > What would you suggest?
>
> > > > On Nov 26, 11:10 am, "mr.freeze" <[email protected]> wrote:
>
> > > > > I see two issues with this:
> > > > > 1) The form errors found in onvalidation will not be displayed if
> > > > > there are already form errors
> > > > > 2) The developer never gets a chance to remove form errors for certain
> > > > > conditions (think contingent form fields)
> > > > > For example, I am using IS_IN_DB on a field but I only want it to be
> > > > > required if another field is a certain value.
>
> > > > > Is there a better way?
>
>