Commit 584c89a3 authored by Romain Loth's avatar Romain Loth

many small corrections to the validation + deactivate doors ajax temporarily

parent 711c1cab
......@@ -83,7 +83,7 @@
<!-- ########################### ( FORM ) ########################## -->
<form id="comex_reg_form" enctype="multipart/form-data"
method="post" onsubmit="validateSubmit(this, event, 'from form', 'register')"
method="post" onsubmit="validateSubmit(event, 'from form', 'register')"
action="cgi-bin/comex_merci_pour_les_infos.py.cgi">
<!-- todo onsubmit also save to cache -->
......@@ -374,7 +374,7 @@
<!-- button instead of input.submit to validate before real submit action -->
<button class="btn btn-lg btn-success" style="float:right"
id="formsubmit"
onclick="validateSubmit(this.parent, event, 'from main submit button', 'register')">
onclick="validateSubmit(event, 'from main submit button', 'register')">
Submit your form
</button>
......@@ -385,10 +385,10 @@
<!-- FOR DEBUG: test go-between with Doors -->
<!-- <p>
<button onclick='validateSubmit(this, event, "from button", "login")'>
<button onclick='validateSubmit(event, "from button", "login")'>
test doors login
</button>
<button onclick='validateSubmit(this, event, "from button", "register")'>
<button onclick='validateSubmit(event, "from button", "register")'>
test doors register
</button>
</p> -->
......
......@@ -42,7 +42,7 @@ templating_env = Environment(loader = FileSystemLoader('../templates'),
# all columns as they are declared in form & DB as tuple:
# NAME, NOT NULL, N or MAXCHARS (if applicable)
COLS = [ ("doors_uid", True, 36),
("last_modified_date", True, 10), # 2016-11-16
("last_modified_date", True, 24), # ex 2016-11-16T17:47:07.308Z
("email", True, 255),
("initials", True, 7),
("country", True, 60),
......
......@@ -17,7 +17,8 @@ create table test_table (
-- ########################
create table comex_registrations (
doors_uid char(36) not null unique,
last_modified_date char(10) not null,
-- ISO stamp like 2016-11-16T17:47:07.308Z
last_modified_date char(24) not null,
email varchar(255) not null unique primary key,
initials varchar(7) not null,
country varchar(60) not null,
......
// the target columns in DB: tuple (name, mandatoryBool, maxChars (or nChars))
var COLS = [ ["doors_uid", true, 36, 'exact'],
["last_modified_date", true, 10, 'exact'],
var COLS = [ ["doors_uid", false, 36, 'exact'],
["last_modified_date", true, 24, 'exact'],
["email", true, 255],
["initials", true, 7],
["country", true, 60],
......@@ -21,6 +21,7 @@ var COLS = [ ["doors_uid", true, 36, 'exact'],
// vars that will be used during the interaction
// NB other vars defined in main scope but just before their respective funs
var wholeFormData
var theForm = document.getElementById('comex_reg_form')
var regTimestamp = document.getElementById('last_modified_date')
......@@ -54,21 +55,36 @@ var passStatus = false
var emailStatus = false
var captchaStatus = false
submitButton.disabled = true
theForm.onkeyup = function () {
theForm.onkeyup = beTestedAsYouGo
// done when anything in the form changes
function beTestedAsYouGo() {
if (passStatus && emailStatus && captchaStatus) {
submitButton.disabled = false
}
else {
submitButton.disabled = true
}
var now = new Date()
regTimestamp.value = now.toISOString()
}
function makePseudoDoorsUid() {
var rando = ""
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 36; i++ )
rando += possible.charAt(Math.floor(Math.random() * possible.length));
return rando
}
// validate more precisely at the end
function validateSubmit(form, e, orignStr, loginOrRegister) {
function validateSubmit(e, orignStr, loginOrRegister) {
e.preventDefault()
var valid = false
var valid = true
var doorsUid = null
var doorsErr = null
......@@ -77,7 +93,8 @@ function validateSubmit(form, e, orignStr, loginOrRegister) {
mainMessage.style.display = 'block'
mainMessage.innerHTML = "Validating the form..."
console.log("form", form)
console.log("form", theForm)
console.log("event", e)
// 2) transmit registration to doors
console.warn("=====> CORS <=====")
......@@ -91,57 +108,62 @@ function validateSubmit(form, e, orignStr, loginOrRegister) {
}
// objectify
wholeFormData = new FormData(form);
$.ajax({
contentType: "application/json",
dataType: 'json',
url: "http://localhost:8989/api/" + action,
data: JSON.stringify({
// TODO pass the real values
"login": "jpp@om.fr",
"password": "droitaubut",
"name": "JPP"
}),
type: 'POST',
success: function(data) {
console.log("doors ajax success")
console.log("response data", data)
// EXPECTED DOORS ANSWER
// {
// "status": "login ok",
// "userInfo": {
// "id": {
// "id": "78407900-6f48-44b8-ab37-503901f85458"
// },
// "password": "68d23eab21abab38542184e8fca2199d",
// "name": "JPP",
// "hashAlgorithm": "PBKDF2",
// "hashParameters": {"iterations" : 1000, "keyLength" : 128}
// }
// }
doorsUid = data.userInfo.id.id
setTimeout(
function() {
location.reload();
}, 5000);
},
error: function(result) {
console.log("doors ajax err", result);
doorsErr = result.statusText
// TESTS: no need to invalidate the form until doors server ready
// valid = false
}
});
// here entire validation
wholeFormData = new FormData(theForm);
//
// $.ajax({
// contentType: "application/json",
// dataType: 'json',
// url: "http://localhost:8989/api/" + action,
// data: JSON.stringify({
// // these values from the form have been checked by beTestedAsYouGo
// "login": wholeFormData.get("email"),
// "password": wholeFormData.get("password"),
// "name": wholeFormData.get("initials")
// }),
// type: 'POST',
// success: function(data) {
// // console.log("doors ajax success")
// // console.log("response data", data)
//
// // EXPECTED DOORS ANSWER
// // {
// // "status": "login ok",
// // "userInfo": {
// // "id": {
// // "id": "78407900-6f48-44b8-ab37-503901f85458"
// // },
// // "password": "68d23eab21abab38542184e8fca2199d",
// // "name": "JPP",
// // "hashAlgorithm": "PBKDF2",
// // "hashParameters": {"iterations" : 1000, "keyLength" : 128}
// // }
// // }
//
// doorsUid = data.userInfo.id.id
// setTimeout(
// function() {
// location.reload();
// }, 5000);
// },
// error: function(result) {
// // console.log("doors ajax err", result);
// doorsErr = result.statusText
//
// // TESTS: no need to invalidate the form until doors server ready
// // valid = false
// }
// });
doorsUid = makePseudoDoorsUid()
// 3) fill in the answer we got
wholeFormData.set("doors_uid", doorsUid)
// 4) here entire validation
var missingFields = []
var toolongFields = []
for (var i in COLS) {
console.warn("checking COLS["+i+"]", COLS[i])
// console.warn("checking COLS["+i+"]", COLS[i])
var fieldName = COLS[i][0]
var mandatory = COLS[i][1]
var nChars = COLS[i][2]
......@@ -153,11 +175,14 @@ function validateSubmit(form, e, orignStr, loginOrRegister) {
var actualValue = wholeFormData.get(fieldName)
// console.log("actualValue", actualValue)
// test mandatory -----------------
if (mandatory && actualValue == null) {
if (mandatory && actualValue == null && actualValue != "") {
// todo human-readable fieldName here
missingFields.push(fieldName)
valid = false
console.log("missingField", fieldName)
}
// test length --------------------
......@@ -170,22 +195,28 @@ function validateSubmit(form, e, orignStr, loginOrRegister) {
+ "("+actualValue+")"
+ "should have exactly "+nChars+" chars")
valid = false
console.log("wrong value")
}
}
else {
if (actualValue.length > nChars) {
toolongFields.push([fieldName, nChars])
valid = false
console.log("tooShort")
}
}
}
// --------------------------------
} // end for val in COLS
// 5) RESULTS
if (valid) {
mainMessage.innerHTML = "Form is valid... Submitting..."
mainMessage.style.display = 'block'
// form.submit()
theForm.submit()
return true
}
else {
......@@ -198,16 +229,7 @@ function validateSubmit(form, e, orignStr, loginOrRegister) {
var errorMessage = ""
// TESTS: restore after we get doors server ================================
// generate a pseudo doors ID during the tests
if (!doorsUid) {
var doorsUid = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyz\
0123456789";
for( var i=0; i < 36; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
// for now we generated a pseudo doors ID above
// if (!doorsUid) {
// // todo retrieve more info than statusText
// errorMessage += "<br/>The email/password registration had an error ("+doorsErr+"), "
......@@ -232,11 +254,6 @@ function validateSubmit(form, e, orignStr, loginOrRegister) {
}
// done when anything in the form changes
function testYourself() {
var now = new Date()
regTimestamp.value = now.toISOString()
}
var picMsg = document.getElementById('picture_message')
function testPictureBlob(fileInput) {
......@@ -415,7 +432,7 @@ $(function() {
// autocomplete position
$(function() {
var $jobtitlesInput = $('#hon_title')
var $jobtitlesInput = $('#jobtitle')
var jobtitlesList = ["Student", "Engineer", "capetown",
"PhD Student", "Dr", "Post-Doc",
......
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