Thanks, Anthony, that worked. In fact, I do need to run my validators: The translated form data is actually JSON which is fed into the translation algo of my server-side backend code. Feeding it without my custom validator could be an unlikely, but possible security hole.
But I can run my validation code manually, so it works now. Meanwhile I've also found this little remark in the manual: "Statements like the one above must be inserted after the form declaration and before the form is accepted, whether or not the field ("name" in the example) is explicitly visualized in the form. ( http://web2py.com/books/default/chapter/29/07/forms-and-validators#Pre-populating-the-form). Am Donnerstag, 23. Januar 2014 16:34:09 UTC+1 schrieb Anthony: > > One question -- in case the translate button is pressed, do you need to > call .validate and run the onvalidation function, or is validation > unnecessary in that case (if the latter, you can just move all the > translate logic to before validation). > > Anthony > > On Thursday, January 23, 2014 8:01:54 AM UTC-5, Horst Horst wrote: >> >> The controller is pretty lengthy, find a stripped-down version below. >> Yes, I'm using several custom buttons which submit the form via JS, there's >> a Save, a Save As, and a Translate button, identified by a hidden, >> JS-generated form variable. >> >> The Translate button translates some form content into a different >> (equivalent) format. In this case, I'd like to simply return the form with >> the translated fields, so the user can continue working on it until he >> presses Save. The 'autocomplete' from my initial post was just an analogy >> to avoid telling the long story. >> >> I've indicated the section in the code where I'd like to change the form >> variables, but assigning to form.vars, doesn't work. (Without understanding >> anything of it, I've tried also assigning to all other members in the form >> which I found holding fields from the record (namely >> form.request_vars.piece_name, form.latest.piece_name, >> form.custom.inpval.piece_name, form.custom.dspval.piece_name), but none of >> that showed the manipulated value when returning the form either.) >> >> def update_piece(): >> piece_id = request.args(0) >> this_piece = db.piece(piece_id) or redirect(URL("user_home")) >> >> # [...] >> >> form = SQLFORM(db.piece, >> this_piece, >> showid=False, >> # The form has no submit button, to prevent >> accidental saving with Enter, >> # Save Buttons are added below >> buttons=[], >> _id=SharedData.FORM_ID, >> ) >> >> # [...] >> >> implant_translate_section_into_form(form) >> >> # [...] >> >> form.validate(detect_record_change=True, onvalidation= >> update_piece_form_hook) >> >> save_button_pressed = False >> save_as_button_pressed = False >> translate_button_pressed = False >> if request.vars: >> # The id of the button which was used for submission is passed >> via an additional form field >> # with the name BUTTON_ID_FIELD_NAME >> submit_button_id = request.vars[SharedData.BUTTON_ID_FIELD_NAME] >> if submit_button_id == SharedData.SAVE_BUTTON_ID: >> save_button_pressed = True >> elif submit_button_id == SharedData.SAVE_AS_BUTTON_ID: >> save_as_button_pressed = True >> elif submit_button_id == SharedData.TRANSLATE_BUTTON_ID: >> translate_button_pressed = True >> >> if form.record_changed: # This requires >> "form.validate(detect_record_change=True,..." >> # [...] >> elif form.accepted: >> if this_piece and save_button_pressed and can_save: >> this_piece.update_record(**dict(form.vars)) >> session.flash = T("Saved '%s'.") %form.vars.piece_name >> redirect(URL("user_home")) >> elif (not this_piece and save_button_pressed and can_save) or >> (this_piece >> and save_as_button_pressed and can_duplicate): >> form.vars.id = db.piece.insert(**dict(form.vars)) >> session.flash = T("Created '%s'.") %form.vars.piece_name >> redirect(URL("user_home")) >> elif translate_button_pressed: >> target_notation = request.vars[SharedData. >> TARGET_NOTATION_FIELD_NAME] >> >> ### >>> Here is where I'd like to change the form fields, >> let's say the record's piece_name >> ### >>> However, assigning form.vars, and then returning the >> form shows the piece_name from the record, >> ### >>> not the one which I assigned here: >> form.vars.piece_name = "Translated Name" >> response.flash = T("Translated %s to '%s'." >> %(new_name,target_notation >> )) >> else: >> response.flash = T("ERROR: Cannot save piece. Please contact >> the administrator.") >> elif form.errors: >> response.flash = T("Please fill in the fields as indicated.") >> >> # [...] >> >> save_button_label = save_as_button_label = "" # An empty label >> informs the view to show no button >> if can_duplicate and not can_save: >> save_as_button_label = "Save As My Piece" >> elif can_save: >> save_button_label = "Save Changes" >> if can_duplicate: >> save_as_button_label = "Save As New Piece" >> >> return dict(piece=this_piece, form=form, SharedData=SharedData, >> save_button_label=save_button_label, save_as_button_label >> =save_as_button_label) >> >> >> >> >> Am Donnerstag, 23. Januar 2014 02:07:19 UTC+1 schrieb Jaime Sempere: >>> >>> >>> I don't understand completely the flow of the process... do you have >>> two submitt buttons, one for "autocomplete" and the other for "final submit? >>> >>> I am not very sure, but here are my thoughts: What I would do is use >>> SQLFORM.factory something like this: >>> >>> new_post_form = SQLFORM.factory(db.posts) >>> if new_thread_form.process().accepted: >>> if (autocomplete): >>> Do your process, and save it in the form.vars >>> else: >>> Do the final update using db(****).update(****) >>> >>> >>> >>> >>> And check this topic too to return the prepolutaded form, keeping the >>> values that you processed after "if (autocomplete)" >>> >> -- 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/groups/opt_out.