Thread: [Cppcms-users] widgets::checkbox.value() always returns 0
Brought to you by:
artyom-beilis
From: CN <cn...@gr...> - 2011-02-12 09:35:39
|
Hello! Please refer to documentation: http://cppcms.sourceforge.net/cppcms_ref_v0_99/classcppcms_1_1widgets_1_1checkbox.html#6e0e8154193ff93fb6a591263b815c04 Checkbox is defined like this: cppcms::widgets::checkbox agree; Now check the posted form values: if(request().post().find("agree") != request().post().end()) BOOSTER_LOG(debug,"agree") << request().post().find("agree")->second; and I see "on" in the log, but agree.value() always returns "0". Any idea? Best Regards, CN |
From: Artyom <art...@ya...> - 2011-02-12 10:10:23
|
Hello, Show your code... 1. How do you configure your form (full code) 2. How do you load data to your form 3. How do you render your form Because I use checkbox on daily basis and it is covered by unit test. > > Checkbox is defined like this: > > cppcms::widgets::checkbox agree; > > Now check the posted form values: > > if(request().post().find("agree") != request().post().end()) > BOOSTER_LOG(debug,"agree") << request().post().find("agree")->second; > > and I see "on" in the log, but agree.value() always returns "0". > > Any idea? 1. All CppCMS 1.x.x widgets have automatic naming that are rendered and tested in form so basically if you use request().post().find("agree") != request().post().end() Then it seems that you do not create form by <% form ... %> tag. and you write your own code. In such case you should provide a name for checkbox: agree.name("agree"); 2. In addition it is also may be that you do not put all widgets in a single form and then they not get loaded. Take a look once again on the CppCMS sources examples/form/ On how do you render and configure the form. Artyom P.S.: Which reminds me there is need for up-to-date tutorial for CppCMS forms. |
From: CN <cn...@gr...> - 2011-02-12 13:49:39
|
Many thanks for help! > Show your code... > > 1. How do you configure your form (full code) > 2. How do you load data to your form > 3. How do you render your form ===Template follows:=== <% c++ #include "pages.h" %> <% xhtml %> <% skin namespace_skin %> <% view view_register uses content_register %> <% template render() %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <link rel="stylesheet" type="text/css" href="http://example.com/css/register.css"/> <title>Register As A Member</title> </head> <body> <form method="post" action="/cgi/s/register"> <table> <tr> <td><label for="em">EMail</label></td> <td><input type="text" id="em" name="em" value="" size="40"/></td> </tr> <tr> <td><label for="pw">Password:</label></td> <td><input type="password" id="pw" name="pw" value="" size="40"/></td> </tr> <tr> <td><label for="cfpw">Confirm Password</label></td> <td><input type="password" id="cfpw" name="cfpw" value="" size="40"/></td> </tr> </table> <label><input type="checkbox" checked="checked" name="agree"/>Agree our terms?</label> <table> <tr> <td><input type="submit" value="Register"/></td> <td></td> </tr> </table> </form> </body> </html> <% end template %> <% end view %> <% end skin %> ===pages.h follows=== 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; bool validate(); cppcms::session_interface &cppcms_sess; }; struct content_register : public content_master { content_register(cppcms::session_interface &se) : form(se){}; form_register form; }; ===pages.cpp follows=== bool form_register::validate() { return form::validate(); } 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"); *this+account+password1+password2+agree; } ===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" } } ============= Best Regards CN |
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 |