Now with error messages!

This commit is contained in:
Oliver 2021-06-23 22:37:45 +10:00
parent aa02377665
commit 66687a6915

View File

@ -217,7 +217,13 @@ function constructField(name, parameters, options={}) {
var field_name = `id_${name}`; var field_name = `id_${name}`;
var html = `<div id='div_${field_name}' class='form-group'>`; var form_classes = 'form-group';
if (parameters.errors) {
form_classes += ' has-error';
}
var html = `<div id='div_${field_name}' class='${form_classes}'>`;
// Add a label // Add a label
html += constructLabel(name, parameters); html += constructLabel(name, parameters);
@ -226,12 +232,14 @@ function constructField(name, parameters, options={}) {
html += constructInput(name, parameters, options); html += constructInput(name, parameters, options);
if (parameters.errors) {
html += constructErrorMessage(name, parameters, options);
}
if (parameters.help_text) { if (parameters.help_text) {
html += constructHelpText(name, parameters, options); html += constructHelpText(name, parameters, options);
} }
// TODO: Add the "error message"
html += `</div>`; // controls html += `</div>`; // controls
html += `</div>`; // form-group html += `</div>`; // form-group
@ -293,6 +301,8 @@ function constructInput(name, parameters, options={}) {
html += " required=''"; html += " required=''";
} }
html += ` placeholder="${parameters.placeholder || ''}"`;
html += '>'; html += '>';
return html; return html;
@ -313,3 +323,32 @@ function constructHelpText(name, parameters, options={}) {
return html; return html;
} }
/*
* Construct an 'error message' div for the field
*
* arguments:
* - name: The name of the field
* - parameters: Field parameters returned by the OPTIONS method
*/
function constructErrorMessage(name, parameters, options={}) {
var errors_html = '';
for (var idx = 0; idx < parameters.errors.length; idx++) {
var err = parameters.errors[idx];
var html = '';
html += `<span id='error_${idx+1}_id_${name}' class='help-block'>`;
html += `<strong>${err}</strong>`;
html += `</span>`;
errors_html += html;
}
return errors_html;
}