340 } |
340 } |
341 return firsterrfield || formid; |
341 return firsterrfield || formid; |
342 } |
342 } |
343 |
343 |
344 |
344 |
345 function handleFormValidationResponse(formid, onsuccess, result) { |
345 function handleFormValidationResponse(formid, onsuccess, onfailure, result) { |
346 // Success |
346 // Success |
347 if (result[0]) { |
347 if (result[0]) { |
348 if (onsuccess) { |
348 if (onsuccess) { |
349 return onsuccess(result[1]); |
349 return onsuccess(result[1], formid); |
350 } else { |
350 } else { |
351 document.location.href = result[1]; |
351 document.location.href = result[1]; |
352 return ; |
352 return ; |
353 } |
353 } |
354 } |
354 } |
363 return ; |
363 return ; |
364 } |
364 } |
365 _displayValidationerrors(formid, descr[0], descr[1]); |
365 _displayValidationerrors(formid, descr[0], descr[1]); |
366 updateMessage(_("please correct errors below")); |
366 updateMessage(_("please correct errors below")); |
367 document.location.hash = '#header'; |
367 document.location.hash = '#header'; |
|
368 if (onfailure){ |
|
369 onfailure(formid); |
|
370 } |
368 return false; |
371 return false; |
369 } |
372 } |
370 |
373 |
371 |
374 |
372 /* unfreeze form buttons when the validation process is over*/ |
375 /* unfreeze form buttons when the validation process is over*/ |
424 * called on traditionnal form submission : the idea is to try |
427 * called on traditionnal form submission : the idea is to try |
425 * to post the form. If the post is successful, `validateForm` redirects |
428 * to post the form. If the post is successful, `validateForm` redirects |
426 * to the appropriate URL. Otherwise, the validation errors are displayed |
429 * to the appropriate URL. Otherwise, the validation errors are displayed |
427 * around the corresponding input fields. |
430 * around the corresponding input fields. |
428 */ |
431 */ |
429 function validateForm(formid, action, onsuccess) { |
432 function validateForm(formid, action, onsuccess, onfailure) { |
430 try { |
433 try { |
431 var zipped = formContents(formid); |
434 var zipped = formContents(formid); |
432 var d = asyncRemoteExec('validate_form', action, zipped[0], zipped[1]); |
435 var d = asyncRemoteExec('validate_form', action, zipped[0], zipped[1]); |
433 } catch (ex) { |
436 } catch (ex) { |
434 log('got exception', ex); |
437 log('got exception', ex); |
435 return false; |
438 return false; |
436 } |
439 } |
437 function _callback(result, req) { |
440 function _callback(result, req) { |
438 handleFormValidationResponse(formid, onsuccess, result); |
441 handleFormValidationResponse(formid, onsuccess, onfailure, result); |
439 } |
442 } |
440 d.addCallback(_callback); |
443 d.addCallback(_callback); |
441 return false; |
444 return false; |
442 } |
445 } |
443 |
446 |
466 } catch (ex) { |
469 } catch (ex) { |
467 log('got exception', ex); |
470 log('got exception', ex); |
468 return false; |
471 return false; |
469 } |
472 } |
470 d.addCallback(function (result, req) { |
473 d.addCallback(function (result, req) { |
471 handleFormValidationResponse(formid, noop, result); |
474 handleFormValidationResponse(formid, noop, noop, result); |
472 if (reload) { |
475 if (reload) { |
473 document.location.href = result[1]; |
476 document.location.href = result[1]; |
474 } else { |
477 } else { |
475 var fieldview = getNode(divid); |
478 var fieldview = getNode(divid); |
476 // XXX using innerHTML is very fragile and won't work if |
479 // XXX using innerHTML is very fragile and won't work if |
500 } catch (ex) { |
503 } catch (ex) { |
501 log('got exception', ex); |
504 log('got exception', ex); |
502 return false; |
505 return false; |
503 } |
506 } |
504 d.addCallback(function (result, req) { |
507 d.addCallback(function (result, req) { |
505 handleFormValidationResponse(formid, noop, result); |
508 handleFormValidationResponse(formid, noop, noop, result); |
506 var fieldview = getNode(divid); |
509 var fieldview = getNode(divid); |
507 fieldview.innerHTML = result[2]; |
510 fieldview.innerHTML = result[2]; |
508 // switch inline form off only if no error |
511 // switch inline form off only if no error |
509 if (result[0]) { |
512 if (result[0]) { |
510 // hide global error messages |
513 // hide global error messages |