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.vars as you want:  request.vars.fieldC='smthng not none'
2. modify fields properties:  if request.vars.fieldA: 
 db.table.fieldC.required=False
3. have total control on the form using `formargs`: 
SQLFORM.smartgrid(formargs={'vars':myvars}) 
 

On Wednesday, January 18, 2017 at 9:19:55 AM UTC+3, LoveWeb2py wrote:
>
> 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
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to