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,129 +347,118 @@ function callDoors(apiAction, data, callback) { ...@@ -326,129 +347,118 @@ 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) { // not necessary b/c button type is set and is != "submit"
// submitEvent.preventDefault()
submitButton.disabled = true
mainMessage.style.display = 'block'
// objectify the form
wholeFormData = new FormData(theForm);
var valid = true var valid = true
// TODO pass mainMessage ref as arg // TODO pass mainMessage ref as arg
mainMessage.innerHTML = "Validating the form..." mainMessage.innerHTML = "Validating the form..."
// ersatz for tests var missingFields = []
// doorsUid = makeRandomString(36) var toolongFields = []
for (var i in COLS) {
var doorsUid = doorsResp[0] // console.warn("checking COLS["+i+"]", COLS[i])
var doorsMsg = doorsResp[1] var fieldName = COLS[i][0]
var mandatory = COLS[i][1]
if (doorsUid == null) { var nChars = COLS[i][2]
// TODO harmonize this case with the valid=False case var isExactN = (COLS[i][3] == 'exact')
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 toolongFields = []
for (var i in COLS) {
// console.warn("checking COLS["+i+"]", COLS[i])
var fieldName = COLS[i][0]
var mandatory = COLS[i][1]
var nChars = COLS[i][2]
var isExactN = (COLS[i][3] == 'exact')
// skip picture already done
if (fieldName == 'pic_file') continue ;
// skip picture already done // skip doors_uid done afterwards if form ok
if (fieldName == 'pic_file') continue ; if (fieldName == 'doors_uid') continue ;
var actualValue = wholeFormData.get(fieldName) var actualValue = wholeFormData.get(fieldName)
// console.log("actualValue", actualValue) // console.log("actualValue", actualValue)
// test mandatory ----------------- // test mandatory -----------------
if (mandatory && (actualValue == null || actualValue == "")) { if (mandatory && (actualValue == null || actualValue == "")) {
// todo human-readable fieldName here // todo human-readable fieldName here
missingFields.push(fieldName) missingFields.push(fieldName)
valid = false valid = false
console.log("missingField", fieldName) console.log("missingField", fieldName)
} }
// test length -------------------- // test length --------------------
else if (actualValue != null) { else if (actualValue != null) {
if (isExactN) { if (isExactN) {
// should never happen => trigger error // should never happen => trigger error
if (actualValue.length != nChars) { if (actualValue.length != nChars) {
console.error("invalid value for field " + fieldName console.error("invalid value for field " + fieldName
+ "("+actualValue+")" + "("+actualValue+")"
+ "should have exactly "+nChars+" chars") + "should have exactly "+nChars+" chars")
valid = false valid = false
console.log("wrong value") console.log("wrong value")
}
} }
else { }
if (actualValue.length > nChars) { else {
toolongFields.push([fieldName, nChars]) if (actualValue.length > nChars) {
valid = false toolongFields.push([fieldName, nChars])
valid = false
console.log("tooShort") console.log("tooShort")
}
} }
} }
// -------------------------------- }
} // end for val in COLS // --------------------------------
} // end for val in COLS
// RESULTS // RESULTS
if (valid) { if (valid) {
// 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 return true
console.log("submitted") }
return true else {
}
else {
console.warn("form is not valid") console.warn("form is not valid")
// TODO highlight invalid fields // TODO highlight invalid fields
submitButton.disabled = false submitButton.disabled = false
var errorMessage = '' var errorMessage = ''
if (missingFields.length) { if (missingFields.length) {
errorMessage += "Please fill the missing fields: " + ulListFromLabelsArray(missingFields, ["red"]) errorMessage += "Please fill the missing fields: " + ulListFromLabelsArray(missingFields, ["red"])
} }
// length is handled by each input's maxlength // length is handled by each input's maxlength
// display (TODO setTimeout and fade) // display (TODO setTimeout and fade)
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