I cloned web2py and write 2 functions for that
http://code.google.com/r/pihentagy-web2py/source/list
You can use those classes like that:
Field('invoice_date','date',
requires=IS_DATE_IN_RANGE(minimum=datetime.date.today(),
format='%d/%m/%Y')),
Field('settlement_date','datetime',
requires=IS_DATETIME_IN_RANGE(minimum=datetime.datetime.now(),format='%H:%M:%S
%Y-%m-%d')),
I don't know why I do not see the newest version in googlecode (I am
not familiar with hg, but I think I managed to commit the changes).
Just in case here are they:
class IS_DATE_IN_RANGE(IS_DATE):
def __init__(self, minimum=None, maximum=None, **args):
self.minimum = minimum
self.maximum = maximum
self.error_message_low = args.pop('error_message_low', 'date
should be at least %(min)s')
self.error_message_high = args.pop('error_message_high', 'date
should be before %(max)s')
super(IS_DATE_IN_RANGE,self).__init__(**args)
def __call__(self, value):
(value, msg) = super(IS_DATE_IN_RANGE,self).__call__(value)
if msg is not None:
return (value, msg)
if self.minimum and self.minimum > value:
return (value, self.error_message_low %
{'min':self.minimum.strftime(self.format)})
if self.maximum and value >= self.maximum:
return (value, self.error_message_high %
{'max':self.maximum.strftime(self.format)})
return (value, None)
class IS_DATETIME_IN_RANGE(IS_DATETIME):
def __init__(self, minimum=None, maximum=None, **args):
self.minimum = minimum
self.maximum = maximum
self.error_message_low = args.pop('error_message_low',
'datetime should be at least %(min)s')
self.error_message_high = args.pop('error_message_high',
'datetime should be before %(max)s')
super(IS_DATETIME_IN_RANGE,self).__init__(**args)
def __call__(self, value):
(value, msg) = super(IS_DATETIME_IN_RANGE,self).__call__(value)
if msg is not None:
return (value, msg)
if self.minimum and self.minimum > value:
return (value, self.error_message_low %
{'min':self.minimum.strftime(self.format)})
if self.maximum and value >= self.maximum:
return (value, self.error_message_high %
{'max':self.maximum.strftime(self.format)})
return (value, None)
+-[ Gergely Kontra <[email protected]> ]------------------+
| |
| Mobile:(+36 20)356 9656 |
| |
+- "Olyan lángész vagyok, hogy poroltóval kellene járnom!" -+
On Mon, Jan 11, 2010 at 20:23, mdipierro <[email protected]> wrote:
>
>
> On Jan 11, 11:19 am, pihentagy <[email protected]> wrote:
>> On Jan 11, 2:57 pm, mdipierro <[email protected]> wrote:
>>
>> > You can create your own field-level validators
>>
>> > class validator:
>> > def __init__(self,error_message): self.error_message=error_message
>> > def __call__(self,value):
>> > if success: return (value,None)
>> > else: return (value,self.error_message)
>>
>> Yes of course, but I thought some "stock" date validators are worth to
>> discuss. There are IS_INT_IN_RANGE and friends, then why not
>> IS_DATE_IN_RANGE (maybe with defaults to None for both min and max
>> date).
>
> I would take a patch to include IS_DATE_IN_RANGE
>
>> > both form.accept and crud.create/crud.update take a parameter
>> > onvalidation
>>
>> > onvalidation can point to a function that takes a form, reads
>> > form.vars and writes form.errors
>>
>> > def f(form):
>> > if not form.vars.a==form.vars.b: form.errors.b="should be == a"
>> > form = crud.create(...., onvalidation = f)
>>
>> But this way the validation is not built into the form/model :(
>
> what do you mean?
>
> --
> You received this message because you are subscribed to the Google Groups
> "web2py-users" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/web2py?hl=en.
>
>
>
>
--
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/web2py?hl=en.