Main  /  Edit version 1  /  Edit version 2  /   /  Users Area

Difference "Tutorial: Start with forms" ver. 1 versus ver. 2


Now we learn how to use forms.
## Create form
First we create our simple form, edit `data.h` file and add header:
#include <cppcms/form.h>
Then create a form class derived from `cppcms::form`:
struct info_form : public cppcms::form {
Then we add widgets to this form:
widgets::text name;
widgets::radio sex;
widgets::select martial;
widgets::number<double> age;
widgets::submit submit;
Where, `text` is text input field, `radio` is multiple choice field, `select` is drop down list of choices, `number<>` is number of specific type and `submit` is submit button.
Now we create a constructor for our class:
info_form() :
name("name","Your Name"),
martial("mat","martial State"),
age("age","Your Age"),
First we call constructors of all our widgets. First parameter is usually `name` HTML field and the second is Description, or value in case of `submit`.
Then we must register all our widgets to this form
*this & name & sex & martial & age & submit;
This allows centralized rendering, loading and validation
of the form. Then we add different conditions and configurations to our widgets:
Adding different options of selection fields.
Define limits: name should not be empty and age should be in range of 0--120.
And now we can add it to our content class (with some other fields for future use).
struct message : public base_content {
string name,state,sex;
double age;
info_form info;
## Form in templates
First we create our output
<% if not empty name %>
<h1>Hello <% name %></h1>
<p>You are <% sex %>, <% state %></p>
<p>Your age is <% age %></p>
<% else %>
<h1>Input your details</h1>
<% end %>
Then we put our form using tag `<% form as_p info %>`:
<form method="post" action="" >
<% form as_p info %>
The flag `as_p` tells to render form `info` using paragraphs.
## Working with form in code:
First we create our content that contains required
data::message c;
Then we test, if something was send:
if(env->getRequestMethod()=="POST") {
If so we load our form and validate it:*cgi);
if( {
if the form is valid, we can do anything
we want. For example setup our content
fields with appropriate values from the form.;;;;
We clear it at the end, because we want user to
put new values;
Now we can render our template as usual:
} // if valid
} // if post
## Additional Validation
Let's assume that standard widgets checks are not good enough. We can extend our validation manually.
For example, I want to make sure that person can't be married if his age under 18. We override `validate()` virtual function that does the job:
virtual bool validate()
if(!form::validate()) return false;
if(martial.get()!="Single" && age.get()<18) {
return false;
return true;
First we validate form as usual with `form::validate()`
and then we do our tests, if something is incorrect,
we mark the filed `martial` as not valid, and this would
be visible in rendered HTML form.


## Related
- [Hello World With Templates](/wikipp/en/page/tut_hw_templates)


CppCMS is a web development framework for performance demanding applications.

Support This Project Logo

Поддержать проект

CppCMS needs You


Main Page

Valid CSS | Valid XHTML 1.0