Closed lucalenardi closed 8 years ago
It's currently not easily possible to add form-level validation. What you could do is add a new field definition to FormField.FIELD_TYPES
in your own code (FormField.FIELD_TYPES.append((....))
). That should work.
Hello matthiask, thanks for the answer.
If I understand what you suggest, that would require to create a custom field with the appropriate validation validate(self, ...)
, and sub-class the FormField
model to include it. I'd really like to avoid subclassing the original model; are there any other ways to extend the FormField.FIELD_TYPES
list?
Maybe it could become a setting(s)...
Hi Luca
The first part is correct -- you'd have to write a custom field.
Subclassing FormField
should not be necessary though. Just append a new item to the list, and do it somewhere in your code, for example in the models.py
file where you also create the FeinCMS content types (if you're also using FeinCMS, that is).
I'm afraid I'm missing something.
I'm registering Page.create_content_type(FormContent, ...)
in the project's models.py, just after FormField.FIELD_TYPES.append((my-custom-field-with-val))
.
Anyway, It is ignoring my append to FormField.FIELD_TYPES
, and not displaying my field in the admin form. How it is supposed to update the list inside the class definition?
I'm sorry, that was a dumb comment from my side. Of course modifying FIELD_TYPES
is not sufficient.
A setting for form fields would either force you to repeat all form fields, or not allow to exclude existing fields. Not too nice. Maybe modify the admin form (form_designer.admin.FormFieldAdmin
would be the place to put in some hooks) to regenerate the field type choice list instead of filling the choice list only once when the application server starts?
A setting could be imported and modified, as we actually do for any settings related to Django. Why not?
That said, actually it seems really hard to change the definition of FIELD_TYPES outside the form designer app. I've tried the following:
__init__
method to change the list;FormFieldAdmin
with a custom one and passing it a custom form with different choices.No results.
Another possible solution could involve the definition of a list of fields in a method inside models.FormField
that could be easily overridden by a proxy model to extend its behaviour. It sounds aligned to your idea of changing the admin, but in the model
domain.
@lucalenardi FIELD_TYPES was ignored because when model is created his choices was populated. This was fixed in #17 using lazy loading I think that this issue is obsolete and could be closed.
Thanks!
Hi,
what's considered the best way to add custom form validation? It could be useful to add a "honeypot" form of spam detection, but I cannot find a simple (by design) method to do it.
Thanks!