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

Form builder as StreamField block #1564

Closed
mmospanenko opened this issue Jul 30, 2015 · 12 comments
Closed

Form builder as StreamField block #1564

mmospanenko opened this issue Jul 30, 2015 · 12 comments

Comments

@mmospanenko
Copy link

Is it possible?

@iho
Copy link

iho commented Aug 1, 2015

+1

1 similar comment
@SalahAdDin
Copy link
Contributor

👍

@gasman
Copy link
Collaborator

gasman commented Aug 12, 2015

I'd be very happy to see this implemented. I suppose the big open question is how form submissions can be handled - on the existing form builder, submissions are POSTed to the page's own URL, but an individual StreamField block doesn't have a URL of its own, or anything that can persistently identify it.

@iansprice
Copy link
Contributor

+1!
Looking to implement this myself and may just give it a shot. Any feedback on the structure below?

How about setting up a FormBuilder block that, regardless of page it is rendered on, submits to a single site-wide url that takes the PK as a GET param? Much like the URL that page previews rely on, perhaps a unique sequence of symbols that would almost never be replicated would

@mx-moth
Copy link
Contributor

mx-moth commented Sep 20, 2015

I would let the developer decide where the form gets submitted to. In some cases, submitting to the current page itself would be appropriate - if the page has extended the Wagtail form builder base class. In other cases, submitting to a central URL, as in your example; or even a completely different page on a different domain such as a MailChimp subscription could be appropriate. Finally, the form could be AJAXified for completely different behaviour, meaning that it never gets submitted anywhere in particular.

If the developer is left to write a template that handles submitting the form, they can write the code that suits their use case very easily:

<form action="{{self.url}}" method="post">
    {{ self.stream_body }}
    <input type="submit">
</form>
<form action="/custom-search-page/" method="get">
    {{ self.custom_search_form }}
    <input type="submit">
</form>
<form action="http://email.example.com/subscribe/" method="post">
    {{ self.body_with_subscribe_fields }}
    <input type="submit">
</form>

@didorothy
Copy link

I found myself needing this feature and so I have an implementation. Basically there is a FormFieldBlock that can be added to a StreamField. The page has to inherit from AbstractForm so that form processing happens and it must also include a mixin that pulls the FormFieldBlock "instances" out of the StreamField value to build the form. Submission then happens the same way as a normal AbstractForm. Though the way I have accomplished this may feel a bit hackish.

You can see my implementation here: https://github.com/didorothy/wagtail/tree/streamfieldforms

Some key files to look at would be:

Template rendering requires a special tag, which I don't really have an example of in the code yet.There is a test case to demonstrate that the tag works but it leaves out several actual details.

I'd submit a pull request but it needs more unit tests (I think mainly to prove that form submission and template rendering work) and I need to provide some documentation for how to set things up.

I'd like to get some feedback to see if I am on the right track. If I'm headed the right direction I will try to finish up the stuff next week and submit a pull request.

@didorothy
Copy link

I was a little later than expected but I have submitted a PR with how I propose that this can be accomplished. I am a bit new to git (though I have used SVN and Mercurial for some time) and I'm not sure I have done everything correctly for a Pull Request. Also, I ran tests with Django 1.9 and Python 3.5 on windows. I may try to set up a VM to do additional testing if needed but I would probably have to do this on another machine as the one I'm using does not have enough RAM to do so effectively.

If I need to address anything let me know.

@stuartaccent
Copy link

stuartaccent commented Oct 13, 2017

Just realise i put my comments on a pr and not a thread so:

We also required this and have a concept to our requirements. Though very early days or more hours. We have come up with the following. Documentation is currently under way but the highlights are below an maybe useful for ideas:

  • Forms can be build in the cms admin and used site wide in any streamfield.
  • You can create your own form templates to display/submit how ever you wish to do it.
  • We have included a mixin which will handle the form post if it is being submitted to a wagtail page.
  • Forms are catagorised by their class in the cms admin for easier navigation.
  • Form submissions are also listed by their form which you can filter by date and are ordered by newest first.
  • Recaptcha can be added to a form.
  • You can also add site wide regex validators for use in regex fields.

https://github.com/AccentDesign/wagtailstreamforms

Stu

@SalahAdDin
Copy link
Contributor

@stuartaccent Good work!

@stuartaccent
Copy link

stuartaccent commented Oct 13, 2017

cheers @SalahAdDin, not 100% and def needs more work, but is a good starting point for us any road.

theres a couple of snaggs like handling validation errors for multiple forms of same type in same page and multiple recaptchas in same page. though not sure if there is a use case for either of these.

may help someone else out

@BertrandBordage
Copy link
Member

As part of the Wagtail’s first Kickstarter campaign, I’m working on this!
https://www.kickstarter.com/projects/noripyt/wagtails-first-hatch

@thibaudcolas
Copy link
Member

I’m going to close this in favour of #4485, which seems to be pretty much about the same thing, and covers the most recent changes in this area.

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

No branches or pull requests

10 participants