Best way to validate individual conditional Smartgrid Forms

119 views
Skip to first unread message

LoveWeb2py

unread,
Jan 18, 2017, 1:19:55 AM1/18/17
to web2py-users
Hello,

I've been trying to string together pieces from the book and multiple forums for working with the SmartGrid edit forms and I'm not sure the best way to validate certain fields...


I am currently using jQuery to hide fieldA if fieldB is selected, but I am running into an error with web2py...

Let's say FieldC is required and Field A are required...

If FieldA is shown, and I submit the form then I get an error because fieldC is empty.

I was looking at the onvalidation method for sqlform.smartgrid, but I'm not quite sure how to apply it or if it's necessary here.

Here is my code:

<script>
        $(document).ready(function(){
    $("select").change(function(){
        $(this).find("option:selected").each(function(){
            if($(this).attr("value")=="value1"){
                $("#table_fieldA__row").hide();
                $("#table_fieldB__row").hide();
                $("#table_fieldC__row").show();
            }
            else if($(this).attr("value")=="value2"){
                $("#table_fieldA__row").show();
                $("#table_fieldB__row").show();
                $("#table_fieldC__row").hide();


            }
            else{
                $("#table_fieldA__row").hide();
                $("#table_fieldB__row").hide();
                $("#table_fieldC__row").hide();

            }
        });
    }).change();
});
</script>

Is there something I could be doing better inside the controller or smartgrid?

    grid = SQLFORM.smartgrid(table.field, onvalidation=source_validate, maxtextlength=100)
    if grid.create_form:
        if grid.create_form.errors:
            response.flash = "Error... please check form"
    elif grid.update_form:
        if grid.update_form.errors:
            response.flash= "Error... 3"
    else:
        return dict(grid=grid)
    return dict(grid=grid)

def source_validate(form):
    #print "In onvalidation callback"
    #print form.vars
    form.errors = True  # this prevents the submission from completing

    # ...or to add messages to specific elements on the form
    form.errors.fielda = "Must specify a value!"
    form.errors.fieldb = "Please select a value2"
    form.errors.fieldc = "Must not be empty!"

Thank you for your help

黄祥

unread,
Jan 18, 2017, 2:23:44 AM1/18/17
to web2py-users
had you tried conditional fields show_if?

best regards,
stifan

LoveWeb2py

unread,
Jan 18, 2017, 2:34:33 AM1/18/17
to web2py-users
Hi Sitfan,

I wasn't sure if I could use show_if because I want to evaluate a value IS_IN_SET(['choice_a','choice_b')

I'd like to show a file upload field if choice_a is selected and a text field if choice_b is selected. 

Val K

unread,
Jan 20, 2017, 11:48:24 AM1/20/17
to web...@googlegroups.com
Yes! just keep in mind, that before call  SQLFORM.grid/smartgrid/factory or just SQLFORM or even FORM ( learn two  latter details) you can:
1. modify request.post_vars as you want:  request.post_vars.fieldC='smthng not none'
2. modify fields properties:  if request.post_vars.fieldA:  db.table.fieldC.required=False
3. have total control on the form using `formargs`: SQLFORM.smartgrid(formargs={'vars':myvars}) 
Reply all
Reply to author
Forward
0 new messages