Are you saying you want to create a single form object on the server side 
that includes all the fields, but on the client side you want to break that 
up into several distinct forms, each of which would only submit one of the 
fields? If so, I'm not sure that's a common enough use case to warrant 
re-vamping how SQLFORM works. In this case, maybe you could do something 
like this:

fields = [f for f in request.post_vars if f in db.thing.fields] if 
request.post_vars 
else None
form = SQLFORM(db.thing, fields=fields, ...)

When the form is created, fields will be None, so all fields will be 
included, but when a form is submitted, fields will include only the 
submitted field.

Anthony

On Thursday, June 21, 2012 5:11:32 PM UTC-4, Osman Masood wrote:
>
> Hey all,
> Was just wondering about this....currently web2py re-runs the form 
> validations for every single field, even if you only update one field. For 
> example, in the models file:
>
> db.define_table('user',
> format = '%(id)s')
>
> db.define_table('thing',
> Field('name', length=100),
> Field('type', length=100),
> Field('size', length=100),
> Field('firstUserId', db.user),
> Field('secondUserId', db.user),
> Field('thirdUserId', db.user),
> format = '%(type)s'
> )
>
> So now, if I want make a form to only update thing.type, it will return a 
> form error unless I also supply the firstUserId, secondUserId, and 
> thirdUserId fields. This is because foreign field references have a 
> validation for IS_IN_DB by default. I can work around this by making their 
> input fields hidden, but isn't that a bit of a hassle? If I have a page 
> that has separate forms for thing.name, thing.type, thing.size, 
> thing.firstUserId, etc., (because they are on different parts of the page, 
> and there are forms for other tables in between), then I have to 
> redundantly enter all the hidden userId fields for each form.
>
> For example, if I want to have a form that just updates 'type', currently 
> I have to do:
> <form name="thing">
>     <input name="type" type="text" value="{{=thing.type}}" />
>     <input name="firstUserId" type="hidden" value="{{=thing.firstUserId}}" 
> />
>     <input name="secondUserId" type="hidden" value=
> "{{=thing.secondUserId}}" />
>     <input name="thirdUserId" type="hidden" value="{{=thing.thirdUserId}}" 
> />
>     <input name="_formname" type="hidden" value="thing" />
>     <input name="id" type="hidden" value="{{=thing.id}}" />
>  </form>
>
> I know there is a way around this by setting the 'fields' attribute in 
> SQLFORM, but what if I want to update firstUserId, secondUserId, or 
> thirdUserId using the same SQLFORM? Then I'd have to make a SQLFORM for 
> each of firstUserId, secondUserId, and thirdUserId. So, in total, I'd have 
> at least 4 almost-identical SQLFORMs in the same controller action.
>
> This is just an example, but you can see how in a real-world application 
> this could get lead to a lot of duplicate code and get pretty messy.
>
> If web2py only performed validations on the fields that are being updated 
> (instead of on every field), then this problem would be solved, and much 
> duplicate code would be avoided. Also, the code would run faster, 
> especially in cases where there are many fields and validations need to be 
> done on every one of them.
>
> What do you guys think?
> Thanks
>

-- 



Reply via email to