This page runs through the quick exercise of implementing a “Hello World” application.
Let’s start with the myapp project that was created previously.
Edit the app/views/App/Index.html template to add this form, under the
<form action="/App/Hello" method="GET"> <input type="text" name="myName" /><br/> <input type="submit" value="Say hello!" /> </form>
Refresh the page to see our work.
Enter some data and submit the form.
That makes sense. Add the method to app/controllers/app.go:
Next, we have to create the view. Create a file app/views/App/Hello.html, with this content:
Finally, add the following to conf/routes file, just below the
Refresh the page, and you should see a greeting:
Lastly, let’s add some validation. The name should be required, and at least three characters.
To do this, let’s use the validation module. Edit your method in app/controllers/app.go:
Now it will send the user back to
Index() if they have not entered a valid
name. Their name and the validation error are kept in the
Flash, which is a temporary cookie.
flash.html template will show any errors or flash messages:
When we submit that form with a name that fails validation, we want the form to retain the bad name, so that the user can edit it before re-submitting. Amend the form you had added to your app/views/App/Index.html template:
Now when we submit a single letter as our name:
Success, we got an appropriate error and our input was saved for us to edit.