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