341 def form_field_value(self, field, values, load_bytes=False): |
341 def form_field_value(self, field, values, load_bytes=False): |
342 """looks for field's value in |
342 """looks for field's value in |
343 1. kw args given to render_form (including previously submitted form |
343 1. kw args given to render_form (including previously submitted form |
344 values if any) |
344 values if any) |
345 2. req.form |
345 2. req.form |
346 3. field's initial value |
346 3. default_<fieldname> attribute / method on the form |
347 """ |
347 4. field's initial value |
|
348 """ |
|
349 defaultattr = 'default_%s' % field.name |
348 if field.name in values: |
350 if field.name in values: |
349 value = values[field.name] |
351 value = values[field.name] |
350 elif field.name in self.req.form: |
352 elif field.name in self.req.form: |
351 value = self.req.form[field.name] |
353 value = self.req.form[field.name] |
|
354 elif hasattr(self, defaultattr): |
|
355 value = getattr(self, defaultattr) |
|
356 if callable(value): |
|
357 value = value() |
352 else: |
358 else: |
353 value = field.initial |
359 value = field.initial |
|
360 if callable(value): |
|
361 value = value(self) |
354 return value |
362 return value |
355 |
363 |
356 def form_field_error(self, field): |
364 def form_field_error(self, field): |
357 """return validation error for widget's field, if any""" |
365 """return validation error for widget's field, if any""" |
358 errex = self.req.data.get('formerrors') |
366 errex = self.req.data.get('formerrors') |
477 value = getattr(self, defaultattr) |
485 value = getattr(self, defaultattr) |
478 else: |
486 else: |
479 # use field's initial value |
487 # use field's initial value |
480 value = field.initial |
488 value = field.initial |
481 if callable(value): |
489 if callable(value): |
482 value = value() |
490 value = value(self) |
483 return value |
491 return value |
484 |
492 |
485 def form_field_format(self, field): |
493 def form_field_format(self, field): |
486 entity = self.edited_entity |
494 entity = self.edited_entity |
487 if field.eidparam and entity.e_schema.has_metadata(field.name, 'format') and ( |
495 if field.eidparam and entity.e_schema.has_metadata(field.name, 'format') and ( |