Re: [Cppcms-users] widgets::checkbox.value() always returns 0
Brought to you by:
artyom-beilis
From: Artyom <art...@ya...> - 2011-02-12 14:11:16
|
> > ===Template follows:=== > <input type="checkbox" checked="checked" name="agree "/>Agree our > terms?</label> You need a value="y" which is default for CppCMS. 1. checkbox tests both name and value to match so you can have different checkboxes with same name. 2. If you don't like the default form rendering you may use 1. Render by smaller parts - like indivisual widgets 2. Render at least input using <% form input checkbox %> so it takes details for you. In your case I'd recommend rendering entire form or simplt form in subforms and render them each in its kind. 3. Automatic rendering solves all problems with manual naming of widgets. So in your case I would write <table> <% form as_table some_form.inputs %> </table> <% form as_space some_form.agree %> <table> <tr> <td><input type="submit" value="Register"/></td> <td></td> </tr> </table> </form> struct form_register : public cppcms::form { form_register(cppcms::session_interface &); cppcms::widgets::email account; cppcms::widgets::password password1; cppcms::widgets::password password2; cppcms::widgets::checkbox agree; cppcms::form inputs; bool validate(); cppcms::session_interface &cppcms_sess; }; form_register() { account.message("Name"); account.non_empty(); password1.message("Password"); password1.non_empty(); password2.message("Confirm"); password2.non_empty(); password2.check_equal(password1); agree.message("Agree to terms"); inputs.add(account); inputs.add(password1); inputs.add(password2); // one redneting group add(inputs); // 2nd redneting group add(agree) } Something like that. > ===pages.cpp follows=== > > bool form_register::validate() > { > return form::validate(); > } > Don't need this, overload validate just if you need special validation. > form_register::form_register(cppcms::session_interface &se) : > cppcms_sess(se) > { > account.name("em"); > account.non_empty(); > password1.name("pw"); > password1.non_empty(); > password2.name("cfpw"); > password2.non_empty(); > password2.check_equal(password1); > agree.name("agree"); need also agree.identifiction("something") same value="something" in the form/ > *this+account+password1+password2+agree; > } > But it is much better not to give names, they generated automatically and this approach less error prone > ===application follows=== > > content_register page(session()); > if(request().request_method() == "GET"){ > render("view_register",page); > } > else{ > page.form.load(context()); > if(page.form.validate()){ > BOOSTER_LOG(debug,"account") << page.form.account.value(); //good > BOOSTER_LOG(debug,"agree") << page.form.agree.value(); //"0" > } > } > Artyom |