Form for adding event objects

In the last part we added an event object with some code in our controller. Flow, similar to extbase has a nice way to add object with forms. In this part we just build a simple form to add some more events.

Extend the EventController

First we add a new action to our eventController. The action has not much to do. It should only display the form.

Basically this action could be also empty. It would be enough to display the form. We inject an event object here, because if the form-validation fails, the event object will be submitted back to the action. This is also the reason that we tell the action that it should not validate the object. We do this by adding the annotation @dontvalidate.

Then we assign the object, that is either null or returned by the form to the view. If it is returned by the view, we have at least the successful validated properties available and can display this properties in the form again.

 The form

Create the file and remember that the naming is important, so that Flow can automatically find the correct template.

The template is pretty much straight forward. We tell Fluid about the layout file, the same we used before. Inside the div-container we add the form using the f:form ViewHelper provided by Fluid.

Adding the action is not new to us. The interesting part here is, that we tell the form that it should handle the object “event”.

The next line is the text box where we can enter the event title. Here we tell Flow/Fluid that the title is a property. This way Flow/Fluid knows that the title is a property of the object event.

There is also a ViewHelper for the submit button. Add it before you close the form.

Save the object

Last part here is to save the object after we submit the form. We need another action in our controller. Like the action we defined in the form, we call the action createAction and add it to our controller.

The EventRepository is already available. We added it to our IndexController before and used the @inject annotation to make it available.

So all we have to do here, add the event-object as parameter to your function and add it to your repository using the add-function that is already available. If you check your database now, or if you call the listAction, you should be able to see your new object.

The last line redirect directly to the listAction. In this case we don’t need a template for the saveAction. But you can also add a Save.html template with some kind of information if the object was saved successfully.

Currently we don’t use any validation here, we add this later.



Leave a Reply

Your email address will not be published. Required fields are marked *