feincms / form-designer

A simple form designer for Django
https://form-designer.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
75 stars 32 forks source link

Custom form validation #12

Closed lucalenardi closed 8 years ago

lucalenardi commented 11 years ago

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!

matthiask commented 11 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.

lucalenardi commented 11 years ago

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)...

matthiask commented 11 years ago

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).

lucalenardi commented 11 years ago

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?

matthiask commented 11 years ago

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?

lucalenardi commented 11 years ago

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:

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.

michaelkuty commented 9 years ago

@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.

matthiask commented 8 years ago

Thanks!