In case anyone else ever has this problem, I will post my own solution.
Web2Py traps all form submissions in web2py.js. To override this behaviour
we need to add the "no_trap" class to the form declaration:
single_payment_form = SQLFORM.factory(
Field('tenant_name', 'string', label="Tenant Name", length=
255),
Field('payment_amount', 'decimal(10,2)', label="Payment Amount",
requires=IS_DECIMAL_IN_RANGE(0, None, "Enter a positive dollar amount"),
widget=CurrencyWidget.With_Symbol('$')),
Field('payment_method', 'string', label="Payment Method",
requires=IS_IN_SET(payment_methods, zero='Please add a Payment Method to
make payments' if not payment_methods else None), length=20),
table_name = "single_payment_form",
_class="no_trap",
submit_button = "Continue")
Then we need to reimplement that behaviour with a flag to indicate whether
or not the confirmation has already happened. I used an already_confirmed
data attribute on the form.
<script type="text/javascript">
function single_payment_form_trap() {
var $component = $('#single_payment_form_component');
var $form = $('#single_payment_form_component form');
var action = $component.attr('data-w2p_remote');
var target = 'single_payment_form_component';
$form.attr('data-w2p_target', target);
$form.attr('data-already_confirmed', 'false');
var url = $form.attr('action');
if ((url === '') || (url === '#') || $.web2py.isUndefined(url)) {
/* form has no action. Use component url. */
url = action;
}
$form.submit(function (e) {
var already_confirmed = $form.attr('data-already_confirmed');
if( already_confirmed == 'false' ) {
$.web2py.disableElement($form.find($.web2py.
formInputClickSelector));
$.web2py.hide_flash();
$('#payment-confirmation-dialog').modal({
show: true,
backdrop: 'static',
keyboard: false
});
} else {
$.web2py.ajax_page('post', url, $form.serialize(), target, $form
);
}
e.preventDefault();
});
$form.on('click', $.web2py.formInputClickSelector, function (e) {
e.preventDefault();
var input_name = $(this).attr('name');
if (!$.web2py.isUndefined(input_name)) {
$('<input type="hidden" />').attr('name', input_name)
.attr('value', $(this).val()).appendTo($form);
}
$form.trigger('submit');
});
};
single_payment_form_trap();
function edit_payment() {
var $form = $('#single_payment_form_component form');
$('#payment-confirmation-dialog').modal('hide');
$.web2py.enableFormElements($form);
}
function submit_payment() {
var $form = $('#single_payment_form_component form');
$('#payment-confirmation-dialog').modal('hide');
$form.attr('data-already_confirmed', 'true');
$form.submit();
}
</script>
In the single_payment_form_trap function we shown the payment confirmation
modal if the already_confirmed data attribute is false - otherwise we
submit the form normally - this is taken almost directly from web2py.js.
I wouldn't call this a graceful solution - but it will work until an update
to web2py brakes this assumed component submission algorithm.
I hope that someone finds this useful or at least aids in someone's
understanding of exactly what happens in Web2Py when you click Submit in a
form that has been loaded in a component.
Thanks,
Eliot
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.