Retrieve custom form input/select value in case of errors

50 views
Skip to first unread message

Gael Princivalle

unread,
Aug 23, 2016, 7:47:34 AM8/23/16
to web2py-users
Hello.

In a custom form when the user submit the form with errors I need to reload all user inputs and set the input value.

For example here is an input:
{{=INPUT(_type='text', _name='book_title', _id='book_title')}}

How can I do that ?
As there is available on form errors form.errors.book_title is there something like form.value.book_title ?

Thanks.

Mirek Zvolský

unread,
Aug 24, 2016, 11:20:20 AM8/24/16
to web2py-users
form.vars.book_title ? (book, chapter 7)




Dne úterý 23. srpna 2016 13:47:34 UTC+2 Gael Princivalle napsal(a):

Gael Princivalle

unread,
Aug 24, 2016, 1:19:25 PM8/24/16
to web2py-users
Thank you Mirek for that.
Like that it works:
{{=INPUT(_type='text', _name='book_title', _id='book_title', _value='form.vars.book_title')}}

But web2py don't load the default value set in the table (even before it was like that but I've add it with _value.

For having a complete solution I must do something like that:
    {{if form.vars.book_title:}}
       
{{book_title_value = form.vars.book_title}}
   
{{else:}}
       
{{book_title_value = 'My book title'}}
   
{{pass}}
    {{=INPUT(_type='text', _name='book_title', _id='book_title', _value=book_title_value)}}

Mirek Zvolský

unread,
Aug 25, 2016, 8:07:23 AM8/25/16
to web2py-users
I hope you can do your solution in controller.

You can set form.vars
  AFTER form= definition
  and BEFORE form.validate() (or before form.process() which contains .validate() call)

But you can set the value later too.
Here you have initial name Jan, and substitute name after failed validation Martin:

def test_name():
 form
= SQLFORM(db.auth_user)
 form
.vars.first_name = 'Jan'
 
if form.process().accepted:
   redirect
(URL(...))
 
elif form.errors:
 
 form.custom.widget.first_name.attributes['_value'] = 'Martin'
 
return dict(form=form)





Dne středa 24. srpna 2016 19:19:25 UTC+2 Gael Princivalle napsal(a):

Gael Princivalle

unread,
Aug 25, 2016, 4:11:03 PM8/25/16
to web2py-users
That's interesting and more clean but in my case I don't use the widget as I would like to add to the input an input-group-addon for having for example for this field 'daily-distance' the unit at the end.

                <div class='form-group'>
                    {{=LABEL(T(db.events.daily_distance.label), _for='daily_distance')}}:
                   
<div class="input-group">
                        {{if form_event.vars.daily_distance:}}
                            {{daily_distance_value = form_event.vars.daily_distance}}
                        {{else:}}
                            {{daily_distance_value = '5'}}
                        {{pass}}
                        {{=INPUT(_type='text', _name='daily_distance', _id='daily_distance', _class='form-control', _value=daily_distance_value, _onchange='update_data()')}}
                       
<span class='input-group-addon'>
                             {{=SPAN(T(db.events.daily_distance.comment), _class='comment')}}
                       
</span>
                   
</div>
                {{if form_event.errors.daily_distance:}}
                   
<div class="error_wrapper">
                       
<div id="daily_distance__error" class="error" style="display: inline-block;">
                            {{=form_event.errors.daily_distance}}
                       
</div>
                   
</div>
                {{pass}}
               
</div>

It works also with the widget:
                <div class="form-group">
                    {{=LABEL(T(db.events.daily_distance.label), _for='title')}}:
                   
<div class="input-group">
                        {{=form_event.custom.widget.daily_distance}}
                       
<span class='input-group-addon'>
                             {{=SPAN(T(db.events.daily_distance.comment), _class='comment')}}
                       
</span>
                   
</div>
               
</div>

But in case of errors the error_wrapper class is added inside the input-group, so it increase the input-group-addon height:
 


It's not nice and I don't know how I can say to web2py to add the error-wrapper after the input-group. Do you know that?

Kirill Shatalaev

unread,
Aug 26, 2016, 1:50:45 AM8/26/16
to web...@googlegroups.com


It's not nice and I don't know how I can say to web2py to add the error-wrapper after the input-group. Do you know that?


On the controller
form.accepts(....hideerror=True)

On the view you have to display errors yourself

{{if form.errors.daily_distance:}}
  ... write your own html/javascript
Reply all
Reply to author
Forward
0 new messages