Fix optional registration fields toggle.

We need to account for optional fields that may get rendered
as hidden input elements due to the configuration of the
registration form when deciding whether or not to display
the optional registration fields toggle checkbox.

ENT-796
This commit is contained in:
Douglas Hall
2017-12-11 11:17:52 -05:00
parent 9b3aad31cc
commit ede8ee4a5b
2 changed files with 43 additions and 9 deletions

View File

@@ -213,7 +213,12 @@
}
]
};
var createRegisterView = function(that) {
var createRegisterView = function(that, formFields) {
var fields = formFields;
if (typeof fields === 'undefined') {
fields = FORM_DESCRIPTION.fields;
}
// Initialize the register model
model = new RegisterModel({}, {
url: FORM_DESCRIPTION.submit_url,
@@ -222,7 +227,7 @@
// Initialize the register view
view = new RegisterView({
fields: FORM_DESCRIPTION.fields,
fields: fields,
model: model,
thirdPartyAuth: THIRD_PARTY_AUTH,
platformName: PLATFORM_NAME
@@ -537,6 +542,27 @@
// The iframe has been deleted
expect($content.find('iframe').length).toEqual(0);
});
it('displays optional fields toggle', function() {
createRegisterView(this);
expect(view.$('.checkbox-optional_fields_toggle')).toBeVisible();
});
it('hides optional fields toggle when there are no visible optional fields', function() {
createRegisterView(this, [
{
placeholder: '',
name: 'hidden_optional',
label: 'Hidden Optional',
defaultValue: '',
type: 'hidden',
required: false,
instructions: 'Used for testing hidden input fields that are optional.',
restrictions: {}
}
]);
expect(view.$('.checkbox-optional_fields_toggle')).toHaveClass('hidden');
});
});
});
}).call(this, define || RequireJS.define);

View File

@@ -94,27 +94,35 @@
buildForm: function(data) {
var html = [],
i,
field,
len = data.length,
requiredFields = [],
optionalFields = [];
this.fields = data;
this.hasOptionalFields = false;
for (i = 0; i < len; i++) {
if (data[i].errorMessages) {
field = data[i];
if (field.errorMessages) {
// eslint-disable-next-line no-param-reassign
data[i].errorMessages = this.escapeStrings(data[i].errorMessages);
field.errorMessages = this.escapeStrings(field.errorMessages);
}
if (data[i].required) {
requiredFields.push(data[i]);
if (field.required) {
requiredFields.push(field);
} else {
optionalFields.push(data[i]);
if (field.type !== 'hidden') {
// For the purporse of displaying the optional field toggle,
// the form should be considered to have optional fields
// only if all of the optional fields are being rendering as
// input elements that are visible on the page.
this.hasOptionalFields = true;
}
optionalFields.push(field);
}
}
this.hasOptionalFields = optionalFields.length > 0;
html = this.renderFields(requiredFields, 'required-fields');
html.push.apply(html, this.renderFields(optionalFields, 'optional-fields'));