Commit dc690f38 authored by Romain Loth's avatar Romain Loth

make validation occur *before* doors registration!

parent ddc64b35
...@@ -79,16 +79,16 @@ var passStatus = false ...@@ -79,16 +79,16 @@ var passStatus = false
var emailStatus = false var emailStatus = false
var captchaStatus = false var captchaStatus = false
submitButton.disabled = true submitButton.disabled = true
theForm.onkeyup = beTestedAsYouGo theForm.onkeyup = testAsYouGo
theForm.onchange = beTestedAsYouGo theForm.onchange = testAsYouGo
theForm.onblur = beTestedAsYouGo theForm.onblur = testAsYouGo
var lastEmailValueCheckedDisplayed = null var lastEmailValueCheckedDisplayed = null
// done when anything in the form changes // done when anything in the form changes
function beTestedAsYouGo() { function testAsYouGo() {
// console.log("beTestedAsYouGo Go") // console.log("testAsYouGo Go")
if (email.value != lastEmailValueCheckedDisplayed) { if (email.value != lastEmailValueCheckedDisplayed) {
// will update the emailStatus boolean // will update the emailStatus boolean
...@@ -133,33 +133,54 @@ function testDoorsUserExists(emailValue) { ...@@ -133,33 +133,54 @@ function testDoorsUserExists(emailValue) {
} }
function registerDoorsAndSubmit(e){ function registerDoorsAndSubmit(){
e.preventDefault() // ? not necessary if button type is set and != "submit"
submitButton.disabled = true
mainMessage.style.display = 'block'
mainMessage.innerHTML = "Registering with ISCPIF Doors..." mainMessage.innerHTML = "Registering with ISCPIF Doors..."
// objectify the form // all values from the form have now been validated
wholeFormData = new FormData(theForm); var emailValue = email.value
var passValue = pass1.value
var wholenameValue = ""
if (mName.value != "") {
wholenameValue = lName.value + ', ' + fName.value + ' ' + mName.value
}
else {
wholenameValue = lName.value + ', ' + fName.value
}
// KNOCKING ON THE DOORS ------------------------------------- // KNOCKING ON THE DOORS -------------------------------------
// /!\ async // /!\ async
callDoors( callDoors(
"register", "register",
[ [emailValue, passValue, wholenameValue],
// these values from the form have been checked by beTestedAsYouGo
wholeFormData.get("email"), // callback: send to DB -------------------
wholeFormData.get("password"),
wholeFormData.get("initials")
],
// VALIDATING + send to DB -------------------
function(doorsResp) { function(doorsResp) {
validateSubmit(wholeFormData, doorsResp) addUidThenSubmit(doorsResp)
} }
) )
}
function addUidThenSubmit(doorsResp) {
var doorsUid = doorsResp[0]
var doorsMsg = doorsResp[1]
if (doorsUid == null) {
mainMessage.innerHTML = "Problem with doors registration... TODO debug"
mainMessage.style.color = colorRed
submitButton.disabled = false
}
else {
// fill in the answer we got
uidInput.value = doorsUid
console.info("form was validated and registered@doors: submitting now")
//==== SEND! ====
theForm.submit()
//===============
}
} }
var doorsIcon = document.getElementById('doors_ret_icon') var doorsIcon = document.getElementById('doors_ret_icon')
...@@ -180,8 +201,8 @@ function displayDoorsStatusInLoginBox (available, emailValue) { ...@@ -180,8 +201,8 @@ function displayDoorsStatusInLoginBox (available, emailValue) {
doorsIcon.style.color = colorRed doorsIcon.style.color = colorRed
} }
// to debounce further actions in beTestedAsYouGo // to debounce further actions in testAsYouGo
// return to neutral is also in beTestedAsYouGo // return to neutral is also in testAsYouGo
lastEmailValueCheckedDisplayed = emailValue lastEmailValueCheckedDisplayed = emailValue
} }
...@@ -326,44 +347,36 @@ function callDoors(apiAction, data, callback) { ...@@ -326,44 +347,36 @@ function callDoors(apiAction, data, callback) {
function makeRandomString(nChars) { function makeRandomString(nChars) {
var rando = "" var rando = ""
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; var possible = "abcdefghijklmnopqrstuvwxyz0123456789";
var len = possible.length
for( var i=0; i < nChars; i++ ) for( var i=0; i < nChars; i++ )
rando += possible.charAt(Math.floor(Math.random() * possible.length)); rando += possible.charAt(Math.floor(Math.random() * len));
return rando return rando
} }
// doValidate() : actions to do after doors registration and before send // validateAndMsg() : bool (validates fields before doors registration and send)
// -----------------------------------------------------------------------------
// valid => return *true* which will trigger the doors registration
// and the real submit action
// //
// => validate the columns // else => message the user, unblock the button and return *false*
// valid => trigger the real submit action // -----------------------------------------------------------------------------
// else => message the user & unblock the button function validateAndMsg() {
// validate more precisely at the end
function validateSubmit(wholeFormData, doorsResp) {
var valid = true // not necessary b/c button type is set and is != "submit"
// submitEvent.preventDefault()
// TODO pass mainMessage ref as arg submitButton.disabled = true
mainMessage.innerHTML = "Validating the form..." mainMessage.style.display = 'block'
// ersatz for tests // objectify the form
// doorsUid = makeRandomString(36) wholeFormData = new FormData(theForm);
var doorsUid = doorsResp[0] var valid = true
var doorsMsg = doorsResp[1]
if (doorsUid == null) { // TODO pass mainMessage ref as arg
// TODO harmonize this case with the valid=False case mainMessage.innerHTML = "Validating the form..."
mainMessage.innerHTML = "Problem with doors registration... TODO debug"
mainMessage.style.color = colorRed
submitButton.disabled = false
}
else {
// fill in the answer we got
wholeFormData.set("doors_uid", doorsUid)
uidInput.value = doorsUid
// todo feels redundant (but if we submit via ajax, cgi-bin response won't be loaded)
// here entire validation
var missingFields = [] var missingFields = []
var toolongFields = [] var toolongFields = []
for (var i in COLS) { for (var i in COLS) {
...@@ -377,6 +390,9 @@ function validateSubmit(wholeFormData, doorsResp) { ...@@ -377,6 +390,9 @@ function validateSubmit(wholeFormData, doorsResp) {
// skip picture already done // skip picture already done
if (fieldName == 'pic_file') continue ; if (fieldName == 'pic_file') continue ;
// skip doors_uid done afterwards if form ok
if (fieldName == 'doors_uid') continue ;
var actualValue = wholeFormData.get(fieldName) var actualValue = wholeFormData.get(fieldName)
// console.log("actualValue", actualValue) // console.log("actualValue", actualValue)
...@@ -421,13 +437,9 @@ function validateSubmit(wholeFormData, doorsResp) { ...@@ -421,13 +437,9 @@ function validateSubmit(wholeFormData, doorsResp) {
// add the captchaCheck inside the form (jquery interference) // add the captchaCheck inside the form (jquery interference)
captchaCheck.value = $(captcha).realperson('getHash') captchaCheck.value = $(captcha).realperson('getHash')
mainMessage.innerHTML = "Form is valid... Submitting..." mainMessage.innerHTML = "Form is valid... Will register and submit..."
mainMessage.style.display = 'block' mainMessage.style.display = 'block'
// console.log("uidInput.value", uidInput.value)
theForm.submit()
// debug
console.log("submitted")
return true return true
} }
else { else {
...@@ -447,8 +459,6 @@ function validateSubmit(wholeFormData, doorsResp) { ...@@ -447,8 +459,6 @@ function validateSubmit(wholeFormData, doorsResp) {
mainMessage.innerHTML = errorMessage mainMessage.innerHTML = errorMessage
return false return false
} }
}
console.warn("=====> end of doValidate <=====")
} }
...@@ -625,16 +635,17 @@ var passwords = [pass1, pass2] ...@@ -625,16 +635,17 @@ var passwords = [pass1, pass2]
// £DEBUG autofill ----------->8------ // £DEBUG autofill ----------->8------
// first_name.value = "Jean" // fName.value = "Jean"
// last_name.value = "Tartampion" // lName.value = "Tartampion"
// initialsInput.value="JPP" // initialsInput.value="JPP"
// document.getElementById('country').value = "France" // document.getElementById('country').value = "France"
// email.value= makeRandomString(10)+"@om.fr" // email.value= makeRandomString(7)+"@om.fr"
// pass1.value="123456+789" // pass1.value="123456+789"
// pass2.value="123456+789" // pass2.value="123456+789"
// document.getElementById('jobtitle').value = "atitle" // document.getElementById('jobtitle').value = "atitle"
// document.getElementById('keywords').value = "Blabla" // document.getElementById('keywords').value = "Blabla"
// document.getElementById('institution').value = "CNRS" // document.getElementById('institution').value = "CNRS"
// basicEmailValidate(email.value)
// --------------------------->8------ // --------------------------->8------
......
...@@ -76,7 +76,7 @@ ...@@ -76,7 +76,7 @@
<!-- ########################### ( FORM ) ########################## --> <!-- ########################### ( FORM ) ########################## -->
<form id="comex_reg_form" enctype="multipart/form-data" <form id="comex_reg_form" enctype="multipart/form-data"
method="post" onsubmit="validateSubmit(event, 'from form', 'register')"> method="post" onsubmit="console.info('submitted')">
<!-- todo onsubmit also save to cache --> <!-- todo onsubmit also save to cache -->
...@@ -400,7 +400,7 @@ ...@@ -400,7 +400,7 @@
<!-- also remember stackoverflow.com/a/3315016/2489184 --> <!-- also remember stackoverflow.com/a/3315016/2489184 -->
<button class="btn btn-lg btn-success" style="float:right" <button class="btn btn-lg btn-success" style="float:right"
id="formsubmit" type=button id="formsubmit" type=button
onclick="registerDoorsAndSubmit(event)"> onclick="if (validateAndMsg(event)) {registerDoorsAndSubmit()}">
Submit your form Submit your form
</button> </button>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment