Discover how easy it is to create public forms using 2sxc for directories, showcases and more - while still fully in control of security.
Example Story
In this example we're assuming that you have a directory of partners, and people can submit their own entry for you to review and release.
We created a demo-app for this which you can download it in the app catalog or view the code on github.
Preparations
Before we look at the public form, here's what we already did:
- create the app
- create a content type "Partners" with Name, Logo (files), Description (wysiwyg), Photos (files) and Showcase (files)
- configured each file-field with the files we expect (png/jpg or pdf)
- created two demo items
- created a template/view showing all items
Here's what it looks like before we start the public form:
![initial list](//2sxc.org/Portals/0/adam/Blog App/hu61iRwwQUC4O9Goe5T9TA/Body/initial list.png)
Adding a Button
First we'll add a button in HTML - it won't do much yet:
![button without anything yet](//2sxc.org/Portals/0/adam/Blog App/hu61iRwwQUC4O9Goe5T9TA/Body/button without anything yet.png)
Now we'll add the code, using the new $2sxc.cms.run(...) command. This will return a promise when the dialog closes, so we'll get that and show an alert when it's done:
![javascript code addPartner](//2sxc.org/Portals/0/adam/Blog App/hu61iRwwQUC4O9Goe5T9TA/Body/javascript code addPartner.png)
Clicking this button as a host/admin will work, but nothing will happen for public users. This is because various scripts etc. aren't loaded yet. To ensure this, we'll use the new @Edit.Enable(...) command:
![edit-enable in Razor](//2sxc.org/Portals/0/adam/Blog App/hu61iRwwQUC4O9Goe5T9TA/Body/edit-enable in Razor.png)
Activating the Features
Typically you'll now see an error, because the features are not activated yet:
![error features not enabled](//2sxc.org/Portals/0/adam/Blog App/hu61iRwwQUC4O9Goe5T9TA/Body/error features not enabled.png)
So to activate them, we'll do as instructed in the links in the error, which is basically following the instructions in this blog about managing features:
Let's start by going to Features Management:
![go to features management](//2sxc.org/Portals/0/adam/Blog App/hu61iRwwQUC4O9Goe5T9TA/Body/go to features management.png)
...and enable the features we need:
![enable features public forms](//2sxc.org/Portals/0/adam/Blog App/hu61iRwwQUC4O9Goe5T9TA/Body/enable features public forms.png)
...and when we've activated them, you'll see something like this:
![activated features](//2sxc.org/Portals/0/adam/Blog App/hu61iRwwQUC4O9Goe5T9TA/Body/activated features.png)
The error will now go away.
Setting Permissions
If you now use Chrome in anynomous mode you won't see an error, the button will do something, but you'll quickly run into the next error, because you haven't set permissions yet (discover more about permissions in the wiki):
![error missing permissions](//2sxc.org/Portals/0/adam/Blog App/hu61iRwwQUC4O9Goe5T9TA/Body/error missing permissions.png)
So let's go to the content-type permissions:
![content type permissions - user button](//2sxc.org/Portals/0/adam/Blog App/hu61iRwwQUC4O9Goe5T9TA/Body/content type permissions - user button.png)
We'll first have to allow Schema-Read. This means that a public user is allowed to ask the server, what fields are available:
![permissions - schema](//2sxc.org/Portals/0/adam/Blog App/hu61iRwwQUC4O9Goe5T9TA/Body/permissions - schema.png)
Next we'll enable that a public user is allowed to create data - but we'll restrict it to draft data, so that new items don't show up in the list:
![permissions - draft only](//2sxc.org/Portals/0/adam/Blog App/hu61iRwwQUC4O9Goe5T9TA/Body/permissions - draft only.png)
Once you did this, you should see these two permissions set for this content-type:
![permission of content-type](//2sxc.org/Portals/0/adam/Blog App/hu61iRwwQUC4O9Goe5T9TA/Body/permission of content-type.png)
OR you can simply add one permission with CRUD on draft (create, read, update, delete).
First Public Use
Again open this in another browser or in anonyomus mode. The form will now seem to work:
![form before file permissions](//2sxc.org/Portals/0/adam/Blog App/hu61iRwwQUC4O9Goe5T9TA/Body/form before file permissions.png)
Adding Upload Permissions
What you'll notice is that uploading fails - like this:
![upload fails](//2sxc.org/Portals/0/adam/Blog App/hu61iRwwQUC4O9Goe5T9TA/Body/upload fails.png)
...this is because upload permissions must be granted on each field that should support upload. This allows you full control of who can upload / update what information. Let's go to the fields, you'll notice a new permissions-button on fields which have an ADAM upload:
![field permissions button](//2sxc.org/Portals/0/adam/Blog App/hu61iRwwQUC4O9Goe5T9TA/Body/field permissions button.png)
For this to work, you typically need to give create & read draft permissions, because otherwise a user would be able to upload, but not see what he just uploaded. Here are the read-permissions:
![field permission read](//2sxc.org/Portals/0/adam/Blog App/hu61iRwwQUC4O9Goe5T9TA/Body/field permission read.png)
...and the upload permissions, resulting in these two permissions:
![field permissions read and upload](//2sxc.org/Portals/0/adam/Blog App/hu61iRwwQUC4O9Goe5T9TA/Body/field permissions read and upload.png)
Upload will now work like magic :)
![upload works now](//2sxc.org/Portals/0/adam/Blog App/hu61iRwwQUC4O9Goe5T9TA/Body/upload works now.png)
Repeat for other Upload fields
until it works like this:
![upload on all fields](//2sxc.org/Portals/0/adam/Blog App/hu61iRwwQUC4O9Goe5T9TA/Body/upload on all fields.png)
Custom Thanks
The example we just made will show this when you press save:
![save thank you message](//2sxc.org/Portals/0/adam/Blog App/hu61iRwwQUC4O9Goe5T9TA/Body/save thank you message.png)
...this is because of the JavaScript in the beginning. You could also do redirects or anything else as you need it.
Managing Draft Data
You'll see that in the demo app, we added something trivial to highlight items which are still draft. To the left you can see the browser in admin-mode, to the right in anonymous mode (not showing the item at all):
![side by side admin and anonymous with draft](//2sxc.org/Portals/0/adam/Blog App/hu61iRwwQUC4O9Goe5T9TA/Body/side by side admin and anonymous with draft.png)
The admin will see the publish button in his hovering toolbar, like this:
![publish hover toolbar](//2sxc.org/Portals/0/adam/Blog App/hu61iRwwQUC4O9Goe5T9TA/Body/publish hover toolbar.png)
Once it's published, both admin and visitor will see this item:
![side by side after publishing](//2sxc.org/Portals/0/adam/Blog App/hu61iRwwQUC4O9Goe5T9TA/Body/side by side after publishing.png)
TL;DR
So that's how you get it to work. Remember - you can always download the app, browse the code and github and just give it a try, and work your way up from there.
Love from Switzerland,
iJungleboy