Thanks for replies. One more question, how did validator get values
directly by form? I currently put my year/month/day widgets under a
FieldSet widget, but I have no idea how to get that values from client
inputs. Here's my current code. I think if I need to valid that values
from client inputs then I couldn't use the __unpackargs__ solution to
get work, right?
class BirthDateValidator(validators.FormValidator):
messages = {
'invalid_date': "Invalid date.",
'date_not_in_future': "Date must be in the future.",
}
def validate_python(self, value, state):
year, month, day = [int(form_value[name]) for name in ('year',
'month', 'day')]
try:
d = date(year, month, day)
except ValueError:
raise validators.Invalid(self.message('invalid_date',
state),
value, state)
if d <= date.today():
raise
validators.Invalid(self.message('date_not_in_future', state),
value, state)
validate_partial_form = True
class BirthDateSchema(validators.Schema):
chained_validators = [BirthDateValidator()]
class SignUpSchema(validators.Schema):
birth_date = BirthDateSchema()
class BirthDateField(widgets.WidgetsList):
month = widgets.SingleSelectField(options=_get_month_options())
day = widgets.SingleSelectField(options=_get_day_options())
year = widgets.SingleSelectField(options=_get_year_options())
class SignUpField(widgets.WidgetsList):
birth_date = widgets.FieldSet(label=_('Birth date:'),
fields=BirthDateField())
On Feb 26, 7:52 am, "Olli Wang" <[EMAIL PROTECTED]> wrote:
> Hi, I was making a widget that let people choose their birth date. The
> widget is displayed by three selection html tag, each is month, day,
> and year respectively. But I stuck in how to group this three value in
> my widget and make a validator by DateConvert() first then pass to
> DateValidator, the user should see the error message from
> DateValidator. Here's my current code:
>
> class BirthDatePicker(widgets.FormField):
> """This widget Will replace the current birth date, it's still
> under construction."""
>
> template = """
> <div xmlns:py="http://purl.org/kid/ns#">
> <input type="text" value="test"/>
> <select class="birthdatepickermonth" name="birthdate_month"
> id="form_birthdate_month">
> <option value="">[${month}]</option>
> <option py:for="name, value in month_names"
> value="${value}" py:content="name"
> />
> </select>
> <select class="birthdatepickerday" name="birthdate_day"
> id="form_birthdate_day">
> <option value="">[${day}]</option>
> <option py:for="value in day_range"
> value="${value}" py:content="value"
> />
> </select>
> <select class="birthdatepickeryear" name="birthdate_year"
> id="form_birthdate_year">
> <option value="">[${year}]</option>
> <option py:for="value in year_range"
> value="${value}" py:content="value"
> />
> </select>
> </div>
> """
> params = ['year', 'month', 'day', 'year_range', 'month_names',
> 'day_range']
> year = _('Year')
> month = _('Month')
> day = _('Day')
> month_names = (
> (_('January'), 1),
> (_('Febuary'), 2),
> (_('March'), 3),
> (_('April'), 4),
> (_('May'), 5),
> (_('June'), 6),
> (_('July'), 7),
> (_('August'), 8),
> (_('September'), 9),
> (_('October'), 10),
> (_('November'), 11),
> (_('December'), 12),
> )
> day_range = range(1, 32)
>
> def __init__(self, validator=None, not_empty=True, **kw):
> super(BirthDatePicker, self).__init__(**kw)
> self.validator = validator or validators.DateTimeConverter(
> format="%m/%d/%Y", not_empty=not_empty)
>
> def update_params(self, params):
> super(BirthDatePicker, self).update_params(params)
> # Get year's range
> this_year = date.today().year
> params['year_range'] = range(this_year-5, this_year-115, -1)
>
> Any help would be appreciated. Thanks.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" 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/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---