Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom form validation #12

Closed
lucalenardi opened this issue Sep 11, 2013 · 8 comments
Closed

Custom form validation #12

lucalenardi opened this issue Sep 11, 2013 · 8 comments

Comments

@lucalenardi
Copy link

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
Copy link
Member

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
Copy link
Author

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
Copy link
Member

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
Copy link
Author

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
Copy link
Member

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
Copy link
Author

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:

  • subclassing the model with a proxy one and overriding the __init__ method to change the list;
  • subclassing the admin inline 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.

@michaelkuty
Copy link
Contributor

@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
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants