Commit d7fa8c53 by Owen Ryan Ang

resolution for WG-406: radiolist and checklist validation

parent f7376321
...@@ -1390,24 +1390,28 @@ const inputChecklist = (key, validation) => { ...@@ -1390,24 +1390,28 @@ const inputChecklist = (key, validation) => {
dropdown1.classList.add('dropdown'); dropdown1.classList.add('dropdown');
var dropdownContent = document.createElement('div'); var dropdownContent = document.createElement('div');
dropdownContent.classList.add('dropdown-content'); dropdownContent.classList.add('dropdown-content');
dropdownContent.setAttribute('id',`checklistContainer_${key}`);
dropdown1.appendChild(dropdownContent); dropdown1.appendChild(dropdownContent);
var isOther = false; var isOther = false;
// Create the checkboxes for each item // Create the checkboxes for each item
items.forEach(function(item, index) { items.forEach(function (item, index) {
if (item.toLowerCase() === "other" || item.toLowerCase() === "others") { if (item.toLowerCase() === "other" || item.toLowerCase() === "others"){
isOther = true; isOther = true;
} else { }else{
var div = document.createElement('div'); var div = document.createElement('div');
div.classList.add('checkbox'); div.classList.add('checkbox');
var checkbox = document.createElement('input'); var checkbox = document.createElement('input');
checkbox.type = 'checkbox'; checkbox.type = 'checkbox';
checkbox.classList.add('checkboxOption'); checkbox.name = `checkboxChoices_${key}`;
checkbox.name = 'checkboxChoices'; checkbox.classList.add("checkboxOption");
checkbox.value = item; checkbox.value = item;
if (index == 0) checkbox.setAttribute('id', `${key}`); if (index == 0) {
checkbox.classList.add("checkboxFirst");
checkbox.setAttribute('id', `${key}`);
}
div.appendChild(checkbox); div.appendChild(checkbox);
var label = document.createTextNode(item); var label = document.createTextNode(item);
div.appendChild(label); div.appendChild(label);
...@@ -1415,15 +1419,14 @@ const inputChecklist = (key, validation) => { ...@@ -1415,15 +1419,14 @@ const inputChecklist = (key, validation) => {
} }
}) })
if (isOther) { if(isOther){
// Create the checkbox dependent on an input text value // Create the checkbox dependent on an input text value
var dependentDiv = document.createElement('div'); var dependentDiv = document.createElement('div');
dependentDiv.classList.add('checkbox'); dependentDiv.classList.add('checkbox');
var dependentCheckbox = document.createElement('input'); var dependentCheckbox = document.createElement('input');
dependentCheckbox.type = 'checkbox'; dependentCheckbox.type = 'checkbox';
checkbox.classList.add('checkboxOption'); dependentCheckbox.name = `checkboxChoices_${key}`;
dependentCheckbox.name = 'checkboxChoices'; dependentCheckbox.classList.add("checkboxOption");
dependentCheckbox.id = 'checkDP';
dependentCheckbox.value = 'other'; // Set the value here based on the input text dependentCheckbox.value = 'other'; // Set the value here based on the input text
dependentDiv.appendChild(dependentCheckbox); dependentDiv.appendChild(dependentCheckbox);
...@@ -1434,14 +1437,14 @@ const inputChecklist = (key, validation) => { ...@@ -1434,14 +1437,14 @@ const inputChecklist = (key, validation) => {
// Initially hide the input text box // Initially hide the input text box
var inputTextBox = document.createElement('input'); var inputTextBox = document.createElement('input');
inputTextBox.type = 'text'; inputTextBox.type = 'text';
inputTextBox.id = 'dependentTB'; inputTextBox.id = `dependentTB_${key}`;
inputTextBox.classList.add('checkboxOther'); inputTextBox.classList.add('checkboxOther');
inputTextBox.style.display = 'none'; inputTextBox.style.display = 'none';
inputTextBox.style.padding = '0px'; inputTextBox.style.padding = '0px';
dropdownContent.appendChild(inputTextBox); dropdownContent.appendChild(inputTextBox);
// Add event listener to the "other" checkbox // Add event listener to the "other" checkbox
dependentCheckbox.addEventListener('change', function() { dropdownContent.addEventListener('change', function () {
if (dependentCheckbox.checked) { if (dependentCheckbox.checked) {
inputTextBox.style.display = 'inline-block'; inputTextBox.style.display = 'inline-block';
} else { } else {
...@@ -1456,6 +1459,16 @@ const inputChecklist = (key, validation) => { ...@@ -1456,6 +1459,16 @@ const inputChecklist = (key, validation) => {
}); });
} }
dropdownContent.addEventListener('change', function () {
const checkboxButtons = document.getElementsByName(`checkboxChoices_${key}`);
checkboxButtons.forEach(checkbox => {
if (checkbox.checked) {
dropdownContent.classList.remove('input-invalid');
dropdownContent.classList.add('input-valid');
}
});
});
return dropdown1; return dropdown1;
} catch (err) { } catch (err) {
throw err; throw err;
...@@ -1480,38 +1493,42 @@ const inputRadiolist = (key, validation) => { ...@@ -1480,38 +1493,42 @@ const inputRadiolist = (key, validation) => {
dropdown1.classList.add('dropdown'); dropdown1.classList.add('dropdown');
var dropdownContent = document.createElement('div'); var dropdownContent = document.createElement('div');
dropdownContent.classList.add('dropdown-content'); dropdownContent.classList.add('dropdown-content');
dropdownContent.setAttribute('id',`dropdownContainer_${key}`);
dropdown1.appendChild(dropdownContent); dropdown1.appendChild(dropdownContent);
var isOther = false; var isOther = false;
// Create radio buttons for each item // Create radio buttons for each item
items.forEach((item, index) => { items.forEach((item, index) => {
if (item.toLowerCase() === "other" || item.toLowerCase() === "others") { if (item.toLowerCase() === "other" || item.toLowerCase() === "others"){
isOther = true; isOther = true;
} else { }else{
var radioDiv = document.createElement('div'); var radioDiv = document.createElement('div');
radioDiv.classList.add('radio-like-checkbox'); radioDiv.classList.add('radio-like-checkbox');
var radio = document.createElement('input'); var radio = document.createElement('input');
radio.type = 'radio'; radio.type = 'radio';
radio.name = `radioChoices_${key}`; radio.name = `radioChoices_${key}`;
radio.classList.add('radioOption'); radio.classList.add("radioOption");
radio.name = `radioChoices_${key}`;
radio.value = item; radio.value = item;
if (index == 0) radio.setAttribute('id', `${key}`); if (index == 0) {
radio.classList.add('radioFirst');
radio.setAttribute('id', `${key}`);
}
var label = document.createTextNode(item); var label = document.createTextNode(item);
radioDiv.appendChild(radio); radioDiv.appendChild(radio);
radioDiv.appendChild(label); radioDiv.appendChild(label);
dropdownContent.appendChild(radioDiv); dropdownContent.appendChild(radioDiv);
} }
}); });
if (isOther) { if (isOther){
// Create the radio button dependent on an input text value // Create the radio button dependent on an input text value
var dependentDiv = document.createElement('div'); var dependentDiv = document.createElement('div');
dependentDiv.classList.add('radio-like-checkbox'); dependentDiv.classList.add('radio-like-checkbox');
var dependentRadio = document.createElement('input'); var dependentRadio = document.createElement('input');
dependentRadio.type = 'radio'; dependentRadio.type = 'radio';
dependentRadio.classList.add('radioOption');
dependentRadio.name = `radioChoices_${key}`; dependentRadio.name = `radioChoices_${key}`;
dependentRadio.classList.add("radioOption");
dependentRadio.value = 'other'; dependentRadio.value = 'other';
dependentDiv.appendChild(dependentRadio); dependentDiv.appendChild(dependentRadio);
...@@ -1521,14 +1538,14 @@ const inputRadiolist = (key, validation) => { ...@@ -1521,14 +1538,14 @@ const inputRadiolist = (key, validation) => {
var inputTextBox = document.createElement('input'); var inputTextBox = document.createElement('input');
inputTextBox.type = 'text'; inputTextBox.type = 'text';
inputTextBox.id = 'dependentTB'; inputTextBox.id = `dependentTB_${key}`;
inputTextBox.classList.add('radioOther'); inputTextBox.classList.add('radioOther');
inputTextBox.style.display = 'none'; inputTextBox.style.display = 'none';
inputTextBox.style.padding = '0px'; inputTextBox.style.padding = '0px';
dropdownContent.appendChild(inputTextBox); dropdownContent.appendChild(inputTextBox);
// Add event listener to the "other" radio button // Add event listener to the "other" radio button
dependentRadio.addEventListener('change', function() { dropdownContent.addEventListener('change', function () {
if (dependentRadio.checked) { if (dependentRadio.checked) {
inputTextBox.style.display = 'inline-block'; inputTextBox.style.display = 'inline-block';
} else { } else {
...@@ -1536,19 +1553,32 @@ const inputRadiolist = (key, validation) => { ...@@ -1536,19 +1553,32 @@ const inputRadiolist = (key, validation) => {
} }
}); });
inputTextBox.addEventListener("input", event => { inputTextBox.addEventListener('change', event => {
if (dependentRadio.checked) { if (dependentRadio.checked) {
dependentRadio.value = event.target.value; dependentRadio.value = event.target.value;
} }
}); });
} }
dropdownContent.addEventListener('change', function () {
const radioButtons = document.getElementsByName(`radioChoices_${key}`);
radioButtons.forEach(radio => {
if (radio.checked) {
dropdownContent.classList.remove('input-invalid');
dropdownContent.classList.add('input-valid');
}
});
});
return dropdown1; return dropdown1;
} catch (err) { } catch (err) {
throw err; throw err;
} }
} }
/** /**
* *
* @param {*} key * @param {*} key
...@@ -2195,6 +2225,15 @@ export function clearForm() { ...@@ -2195,6 +2225,15 @@ export function clearForm() {
closeButtons[i].click(); closeButtons[i].click();
} }
const radioOtherField = document.querySelectorAll('.radioOther');
const checkboxOtherField = document.querySelectorAll('.checkboxOther');
radioOtherField.forEach(field => {
field.style.display = 'none';
});
checkboxOtherField.forEach(field => {
field.style.display = 'none';
});
// Set the selected 'doctype' back // Set the selected 'doctype' back
const options = docTypeField.options; const options = docTypeField.options;
const { elements } = formElement const { elements } = formElement
......
...@@ -47,6 +47,10 @@ export const validateInput = (fieldID, value) => { ...@@ -47,6 +47,10 @@ export const validateInput = (fieldID, value) => {
case 'fingerprint': case 'fingerprint':
case 'file-upload': case 'file-upload':
return validateMedia(validation, fieldID); return validateMedia(validation, fieldID);
case 'radiolist':
return validateRadio(validation, fieldID);
case 'checklist':
return validateChecklist(validation, fieldID);
default: default:
return { valid: false, error: [`Collection of allowed values for field: ${fieldID} not found`]} return { valid: false, error: [`Collection of allowed values for field: ${fieldID} not found`]}
} }
...@@ -286,14 +290,18 @@ const validateSpecific = (validation, value) => { ...@@ -286,14 +290,18 @@ const validateSpecific = (validation, value) => {
/** /**
* *
* @param {*} validation
* object containing rules for validating media field.
* @param {*} fieldID * @param {*} fieldID
* Key of input field in schema. Expected to be ID of the element. * Key of input field in schema. Expected to be ID of the element.
* @returns * @returns
* validation of given key in schema * object containg:
* valid - true if no errors found after validation
* errors - list of errors found during validation
*/ */
const validateMedia = (validation, fieldID) => { const validateMedia = (validation, fieldID) => {
let errors = []; let errors = [];
const { mandatory, fieldLength } = validation const { mandatory } = validation
const inputElement = document.getElementById(`${fieldID}_attachedMedia`); const inputElement = document.getElementById(`${fieldID}_attachedMedia`);
if (mandatory && inputElement.files.length === 0) { if (mandatory && inputElement.files.length === 0) {
...@@ -305,6 +313,74 @@ const validateMedia = (validation, fieldID) => { ...@@ -305,6 +313,74 @@ const validateMedia = (validation, fieldID) => {
}; };
} }
/**
*
* @param {*} validation
* object containing rules for validating radio list.
* @param {*} fieldID
* Key of input field in schema. Expected to be ID of the element.
* @returns
* object containg:
* valid - true if no errors found after validation
* errors - list of errors found during validation
*/
const validateRadio = (validation, fieldID) => {
let errors = [];
const { mandatory } = validation
const radioButtons = document.getElementsByName(`radioChoices_${fieldID}`);
if (mandatory){
let isChecked;
radioButtons.forEach(radio => {
if (radio.checked) {
isChecked = true;
}
});
if (!isChecked) {
errors = [...errors, 'No option selected'];
}
}
return {
valid: errors.length===0,
errors
};
}
/**
*
* @param {*} validation
* object containing rules for validating radio list.
* @param {*} fieldID
* Key of input field in schema. Expected to be ID of the element.
* @returns
* object containg:
* valid - true if no errors found after validation
* errors - list of errors found during validation
*/
const validateChecklist = (validation, fieldID) => {
let errors = [];
const { mandatory } = validation
const checkboxButtons = document.getElementsByName(`checkboxChoices_${fieldID}`);
if (mandatory){
let isChecked;
checkboxButtons.forEach(checkbox => {
if (checkbox.checked) {
isChecked = true;
}
});
if (!isChecked) {
errors = [...errors, 'No option selected'];
}
}
return {
valid: errors.length===0,
errors
};
}
export const checkValidValues = (fieldID, value) => { export const checkValidValues = (fieldID, value) => {
try { try {
const validation = getValidation(fieldID); const validation = getValidation(fieldID);
......
...@@ -32,8 +32,9 @@ export async function WriteForm(e,metrics,doctype,section) { ...@@ -32,8 +32,9 @@ export async function WriteForm(e,metrics,doctype,section) {
continue; continue;
} }
// If the first radio button was found // If the first radio button was found
if (fid == 'Radio_List') { if (Nodes[i].classList.contains('radioFirst')) {
const radioButtons = document.getElementsByName('radioChoices'); var key = fid;
const radioButtons = document.getElementsByName(`radioChoices_${key}`);
let selectedValue; let selectedValue;
//check if the value is checked to find the selected value //check if the value is checked to find the selected value
radioButtons.forEach(radio => { radioButtons.forEach(radio => {
...@@ -44,13 +45,14 @@ export async function WriteForm(e,metrics,doctype,section) { ...@@ -44,13 +45,14 @@ export async function WriteForm(e,metrics,doctype,section) {
}); });
fields[Object.keys(lookup[fid]).includes('aka') ? lookup[fid].aka.replace("field", "") : fid] = selectedValue; fields[Object.keys(lookup[fid]).includes('aka') ? lookup[fid].aka.replace("field", "") : fid] = selectedValue;
i += radioButtons.length; //increment the number of radio buttons to skip the other radio button inputs. i += radioButtons.length - 1; //increment the number of radio buttons to skip the other radio button inputs.
continue; continue;
} }
// If the first checkbox was found // If the first checkbox was found
if (fid == 'Checkbox_List') { if (Nodes[i].classList.contains('checkboxFirst')) {
const checkboxButtons = document.getElementsByName('checkboxChoices'); var key = fid;
const checkboxButtons = document.getElementsByName(`checkboxChoices_${key}`);
let selectedValue = ''; let selectedValue = '';
let isFirstChecked = true; // Variable to track the first checked checkbox let isFirstChecked = true; // Variable to track the first checked checkbox
//check each checkbox if it is checked to find the values. //check each checkbox if it is checked to find the values.
...@@ -66,7 +68,7 @@ export async function WriteForm(e,metrics,doctype,section) { ...@@ -66,7 +68,7 @@ export async function WriteForm(e,metrics,doctype,section) {
}); });
fields[Object.keys(lookup[fid]).includes('aka') ? lookup[fid].aka.replace("field", "") : fid] = selectedValue; fields[Object.keys(lookup[fid]).includes('aka') ? lookup[fid].aka.replace("field", "") : fid] = selectedValue;
i += checkboxButtons.length ; //increment the number of checkboxes to skip the checkboxes input i += checkboxButtons.length - 1; //increment the number of checkboxes to skip the checkboxes input
continue; continue;
} }
fields[Object.keys(lookup[fid]).includes('aka') ? lookup[fid].aka.replace("field", "") : fid] = Nodes[i].value; fields[Object.keys(lookup[fid]).includes('aka') ? lookup[fid].aka.replace("field", "") : fid] = Nodes[i].value;
......
...@@ -35,8 +35,8 @@ export const submitForm = async (e) => { ...@@ -35,8 +35,8 @@ export const submitForm = async (e) => {
if (type === 'submit') continue if (type === 'submit') continue
if (type === 'file') continue if (type === 'file') continue
if (type === 'hidden') continue if (type === 'hidden') continue
if (id === 'Radio_List' || element.classList.contains('radioOption')) continue if (element.classList.contains('radioOption') && id.length === 0) continue
if (id === 'Checkbox_List' || element.classList.contains('checkboxOption')) continue if (element.classList.contains('checkboxOption') && id.length === 0) continue
if (id === 'DocType') { if (id === 'DocType') {
doctype = element.options[element.selectedIndex].text; doctype = element.options[element.selectedIndex].text;
continue; continue;
...@@ -56,15 +56,27 @@ export const submitForm = async (e) => { ...@@ -56,15 +56,27 @@ export const submitForm = async (e) => {
// Update display of input field if input is not valid // Update display of input field if input is not valid
if (!valid) { if (!valid) {
console.log(element);
error = true
if (type === 'select-one') {
continue
}
if (type === 'button'){ if (type === 'button'){
const fieldMedia = document.getElementById(`${id}_container`); const fieldMedia = document.getElementById(`${id}_container`);
fieldMedia.classList.remove('input-valid'); fieldMedia.classList.remove('input-valid');
fieldMedia.classList.add('input-invalid'); fieldMedia.classList.add('input-invalid');
continue continue
} }
console.log(element); if (type === 'radio'){
error = true const radioContainer = document.getElementById(`dropdownContainer_${id}`)
if (type === 'select-one') { radioContainer.classList.remove('input-valid');
radioContainer.classList.add('input-invalid');
continue
}
if (type === 'checkbox'){
const checkboxContainer = document.getElementById(`checklistContainer_${id}`)
checkboxContainer.classList.remove('input-valid');
checkboxContainer.classList.add('input-invalid');
continue continue
} }
const field = document.getElementById(id) const field = document.getElementById(id)
......
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