Thread: [Cppcms-users] working with http::context.
Brought to you by:
artyom-beilis
From: augustin <aug...@ov...> - 2010-08-24 09:52:51
|
Hello, I've been trying to work with http::context but with no success. I am working with forms. During the validation process, I want to set some messages to display to the user. I have seen widgets::base_widget::error_message() but this is not what I need. I want to: 1) be able to set errors for the *whole* form, not a specific widget. 2) be able to set various messages that are valid only within a specific context (http get/post request). If I understand well, I cannot set those message within my cppcms::application namespace, since it is shared between all processes, regardless of user or sessions (?). Thus a message intended for one user might be displayed for another user. I could use the user session, but a message intended for one page (form submission() might appear on another tab, as the user browses other pages within the site (race condition?). It appeared to me that the http::context might be the best place to store temporary messages that I could retrieve and display at the end of the request. But I have tried and failed. I am not too sure if it's possible and how to go about it. I have tried to add members to the context object. If it's possible to do so, I did it the wrong way. Thanks, Augustin. -- Friends: http://www.reuniting.info/ http://activistsolutions.org/ My projects: http://astralcity.org/ http://3enjeux.overshoot.tv/ http://linux.overshoot.tv/ http://overshoot.tv/ http://charityware.info/ http://masquilier.org/ http://openteacher.info/ http://minguo.info/ http://www.wechange.org/ http://searching911.info/ . |
From: Artyom <art...@ya...> - 2010-08-24 13:07:33
|
> > I've been trying to work with http::context but with no success. > > I am working with forms. During the validation process, I want to set some > messages to display to the user. > > I have seen widgets::base_widget::error_message() but this is not what I need. > > I want to: > 1) be able to set errors for the *whole* form, not a specific widget. > 2) be able to set various messages that are valid only within a specific > context (http get/post request). If entry form fails validation you may just display any message you want outside widgets system. i.e. if(request().request_method()=="POST") { my_content.my_form.load(context()); if(!my_content.my_form.validate()) { my_content.fail_flag = true; } } And in template system itself <% if fail_flag %> <strong>FAILED</strong> <% end %> <form ... > <% form as_p my_form %> </form> See example of forms. Also you can specify a number of reasons and add some flag to form you are created. > > If I understand well, I cannot set those message within my cppcms::application > > namespace, since it is shared between all processes, regardless of user or > sessions (?). Thus a message intended for one user might be displayed for > another user. > > I could use the user session, but a message intended for one page (form > submission() might appear on another tab, as the user browses other pages > within the site (race condition?). I don't understand. What do you mean message? What do you mean shared? What race condition? > > It appeared to me that the http::context might be the best place to store > temporary messages that I could retrieve and display at the end of the > request. No, http::context just preservers specific information for single request/response. You can't change or alter it. > > But I have tried and failed. I am not too sure if it's possible and how to go > about it. I have tried to add members to the context object. If it's possible > to do so, I did it the wrong way. You can't and shouldn't derive from context. Artyom |
From: augustin <aug...@ov...> - 2010-08-24 15:13:28
|
On Tuesday 24 August 2010 09:07:21 pm Artyom wrote: > If entry form fails validation you may just display any message you want > outside widgets system. i.e. > > if(request().request_method()=="POST") { > my_content.my_form.load(context()); > if(!my_content.my_form.validate()) { > my_content.fail_flag = true; > } > } > > And in template system itself > > <% if fail_flag %> > <strong>FAILED</strong> > <% end %> > <form ... > > <% form as_p my_form %> > </form> > Thank you, Artyom. That's not exactly what I wanted to do, but you put me back on the right track. I wanted to be able to add messages for the user from within my_content.my_form.validate(). What I did is create a struct to hold messages: struct user_messages { std::list<std::string> messages; }; I add this struct to my base_content and pass it by reference to my_content.my_form.validate() so that I can populate it with actual messages according to the validation process. Then I can output it within the template using a foreach loop. There may be better ways, but it works for now. When I have more experience, I might come up with a more elegant solution. > See example of forms. Yes, I had studied it closely. Your examples are very useful and I reproduced them to learn from them. > Also you can specify a number of reasons > and add some flag to form you are created. I am not too sure what you are referring to, here. > No, http::context just preservers specific information for single > request/response. You can't change or alter it. Ok. That was my mistake. I was trying to store temporary data (i.e. data related to the current request only) somewhere in http::context instead of putting it into my class derived from cppcms::base_content, as you suggested above. Anyway, your reply was very useful, as usual, and I managed to do more or less what I wanted. Thank you. Augustin. -- Friends: http://www.reuniting.info/ http://activistsolutions.org/ My projects: http://astralcity.org/ http://3enjeux.overshoot.tv/ http://linux.overshoot.tv/ http://overshoot.tv/ http://charityware.info/ http://masquilier.org/ http://openteacher.info/ http://minguo.info/ http://www.wechange.org/ http://searching911.info/ . |