How can I use vue attributes inside FORM?

86 views
Skip to first unread message

Константин Комков

unread,
Sep 22, 2020, 5:50:15 PM9/22/20
to web2py-users

I don't know but it's not working:
SELECT(*years, _name='year', _id='year',
       
requires=IS_INT_IN_RANGE(request.now.year - 1, request.now.year + 1,
                               
error_message="Year isn't correct!'"),
       
_class='form-control', **{'_v-model': 'year'})


villas

unread,
Sep 23, 2020, 10:50:01 AM9/23/20
to web2py-users
I didn't really think about what you are trying to do, but maybe this will help for your specific bug:

SELECT(*years, _name='year', _id='year', _class='form-control', requires=IS_INT_IN_RANGE(request.now.year - 1, request.now.year + 1, error_message="Year isn't correct!"), **{'_v-model': 'year'})

Константин Комков

unread,
Sep 23, 2020, 11:18:50 AM9/23/20
to web...@googlegroups.com
I do form with fields year, month and another some fields which must be loaded from table in database if year and month != ''.
I know how to make that with vue attributes, but cun't create them inside function in controller. Villas, your example is the same - not work. If I add **{'_data-test': 'test'} - when form was generated I have data-test="test" in select, **{'_v-model': 'year'} -when form was generated I don't have v-model="year" in select.

вторник, 22 сентября 2020 г., 20:50:15 UTC+3 пользователь Константин Комков написал:

villas

unread,
Sep 23, 2020, 3:41:12 PM9/23/20
to web2py-users
Strange,  copy and paste lines in the shell...

>>> years = ['2020','2021']

>>> SELECT(*years, _name='year', _id='year', _class='form-control',   requires=IS_INT_IN_RANGE(request.now.year - 1, request.now.year + 1, error_message="Year isn't correct!"), **{'_v-model': 'year'}).xml()

'<select class="form-control" id="year" name="year" v-model="year"><option value="2020">2020</option><option value="2021">2021</option></select>'

Константин Комков

unread,
Sep 23, 2020, 4:02:13 PM9/23/20
to web2py-users
Villlas, yes that variant with .xml() add attribute, but that variant with FORM not worked, and I don't know how to add it in FORM.
def my_func():
    form = FORM(DIV(DIV(LABEL('Год:', _for='year'),

                    SELECT
(*years, _name='year', _id='year',
                           
requires=IS_INT_IN_RANGE(request.now.year - 1, request.now.year + 1,

                                                   
error_message='Указан некорректный год!'),
                           
_class='form-control', **{'_v-model': 'year'}),
                   
_class='col-sm-12 col-md-4 col-lg-3'), _class='form-group row'))
   
return dict(form=form)
P.S. That variant not worked:
XML(SELECT(*years, _name='year', _id='year', _class='form-control',   requires=IS_INT_IN_RANGE(request.now.year - 1, request.now.year + 1, error_message="Year isn't correct!"), **{'_v-model': 'year'}).xml())


среда, 23 сентября 2020 г., 18:41:12 UTC+3 пользователь villas написал:

villas

unread,
Sep 23, 2020, 11:21:54 PM9/23/20
to web2py-users
It is difficult to understand what you mean, or what you are doing.  Therefore,  I am not sure how to help.  However, here are a few comments:
  • I only used .xml() to print the contents of the var 'form' in the shell, I wouldn't use it otherwise.
  • The code you wrote seems to be valid (I mean it doesn't produce a ticket),  although I do not know where the list 'years' is declared.
  • FORM is a low level method to produce forms.  If you want to use server validation (IS_INT_IN_RANGE) then you would probably be better creating a form with SQLFORM.factory().
  • I would recommend  that you spend a little more time trying some of the form examples in the book before adding any complexities of vue.

Константин Комков

unread,
Sep 24, 2020, 6:37:44 AM9/24/20
to web2py-users
I spent a lot of time for understanding that cases when we can use SQLFORM or SQLFORM.factory() 1 or 2 in 100.

1.png

2.png

3.png

4.png

I have table in database for report and on picture I show differ between what I need and what can supply SQLFORM. I know that we can exclude unnecessary fields and change labels in SQLFORM, but I don't know how to combine helps type with status and cloned it 18 times.
All code if you want:
@auth.requires_login()
def egisso_load():
   
def get_help_id(name):
        result
= None
       
if len(name) > 5:
           
try:
                result
= int(name[5:])
           
except:
               
return result
       
return result


    helps_statuses
= db().select(db.s_helps_statuses.id, db.s_helps_statuses.name,
                                 cache
=(cache.ram, 3600), cacheable=True)
    helps_types
= db(db.s_helps_types.active == 1).select(
        db
.s_helps_types.id, db.s_helps_types.name, orderby=db.s_helps_types.id, cache=(cache.ram, 3600),
        cacheable
=True)
    years
= [OPTION(request.now.year, _value=request.now.year),
             OPTION
(request.now.year - 1, _value=request.now.year - 1)]
    message
= ''

    form
= FORM(DIV(DIV(LABEL('Год:', _for='year'),

                        SELECT
(*years, _name='year', _id='year', _class='form-control',
                               requires
=IS_INT_IN_RANGE(request.now.year - 1, request.now.year + 1,
                                                        error_message
="Year isn't correct!"),

                               
**{'_v-model': 'year'}, **{"'v-test'": "test"}),

                        _class
='col-sm-12 col-md-4 col-lg-3'),

                    DIV
(LABEL('Месяц:', _for='month'),
                        SELECT
(
                            OPTION
('Январь', _value=1),
                            OPTION
('Февраль', _value=2),
                            OPTION
('Март', _value=3),
                            OPTION
('Апрель', _value=4),
                            OPTION
('Май', _value=5),
                            OPTION
('Июнь', _value=6),
                            OPTION
('Июль', _value=7),
                            OPTION
('Август', _value=8),
                            OPTION
('Сентябрь', _value=9),
                            OPTION
('Октябрь', _value=10),
                            OPTION
('Ноябрь', _value=11),
                            OPTION
('Декабрь', _value=12),
                            requires
=IS_INT_IN_RANGE(1, 13, error_message='Указан некорректный месяц!'),
                            _name
='month', _id='month', _class='form-control'),

                        _class
='col-sm-12 col-md-4 col-lg-3'),

                    _class
='form-group row'),
                DIV
(DIV(P('Виды выплат:', _class='lead mb-0'), _class='col'), _class='form-group row'),
                DIV
(DIV(INPUT(_type='checkbox', _name='chb', _id='chb', **{'_v-model': 'chb'},
                              _class
='form-check-input'),
                       LABEL
('Отметить всё как загруженное:', _for='chb', _class='form-check-label'),
                       _class
='form-check'), _class='form-group'))


    helps_statuses_list
= list()
   
for row in helps_statuses:
        helps_statuses_list
.append(OPTION(row.name, _value=row.id))
    helps_statuses_list
.insert(0, OPTION(''))
    columns_list
= list()
   
for row in helps_types:
        columns_list
.append(DIV(LABEL(row.name, _for=f'help_{row.id}'),
                            SELECT
(*helps_statuses_list, _id=f'help_{row.id}',
                                   _name
=f'help_{row.id}', _class='form-control',
                                   requires
=IS_IN_DB(db, 's_helps_statuses.id',
                                                     error_message
='Выберите значение!')),
                            _class
='col-sm-12 col-md-4 col-lg-3 pb-3'))
    form
.append(DIV(*columns_list, _class='form-group row', _id='helps_type_row'))
    form
.append(DIV(DIV(INPUT(_type='submit', _class='btn btn-dark w-100'), _class='col-sm col-md-4 col-lg-3'),
                    _class
='form-group row'))
   
if form.process().accepted:
       
for x in form.vars:
           
if x not in ('year', 'month', 'chb'):
                help_id
= get_help_id(x)
               
if help_id is not None:
                    db
.a_egisso_and_divisions.update_or_insert(
                       
(db.a_egisso_and_divisions.division == auth.user.division) &
                       
(db.a_egisso_and_divisions.year == form.vars.year) &
                       
(db.a_egisso_and_divisions.month == form.vars.month) &
                       
(db.a_egisso_and_divisions.helps_type == help_id),
                        division
=auth.user.division, helps_type=help_id, year=form.vars.year,
                        month
=form.vars.month, status=form.vars[x])
               
else:
                    message
= f'Из ключа {x} не может быть извлечено значение идентификатора!'
   
return dict(form=form, message=message)



villas

unread,
Sep 24, 2020, 1:31:22 PM9/24/20
to web2py-users
It does not seem easy to read and I'm not sure I can help without rewriting your code.
However,  in your function...
    def get_help_id(name):
        result
= None
       
if len(name) > 5:
           
try:
                result
= int(name[5:])
           
except:
               
return result
       
return result


it seems that you will be passing integers to that function (as strings).  However,  only strs more than 5 chrs long will be converted to an int.  I feel sure that this is not what you want to do.  Please double check it.

Константин Комков

unread,
Sep 24, 2020, 5:18:08 PM9/24/20
to web...@googlegroups.com
Thank you, for your attention Villas. I don't ask your rewrite my code) My code is working.
  1. Validator IS_INT_IN_RANGE good work not only in SQLFORM.
  2. Function get_help_id() getting name from form.vars - ('help_1', 'help_2', ... , 'help_17', 'help_18').
I just want to add in my form additional usability. For that task I neded add attributes in FORM and it was my question. And I don't find solution yet.
But as variant I can add attributes in javascript and then use vuejs.

Javascript:
let year = document.getElementById('year');
let month
= document.getElementById('month');
year
.setAttribute("v-model", "year");
month
.setAttribute("v-model", "month");


villas

unread,
Sep 24, 2020, 9:45:45 PM9/24/20
to web2py-users
Hi,  I'm glad your code is working.  However, as I am still not clear on what you want,  I hope someone more familiar with vuejs will be able to assist.
Best wishes.
Reply all
Reply to author
Forward
0 new messages