Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(storefront): BCTHEME-83 added basic validation for Account Signup Date Field #2126

Merged
merged 2 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Draft
- Added basic validation for Account Signup Date Field when it's empty [#2126](https://github.com/bigcommerce/cornerstone/pull/2126)
- Fixed Quick View modal "X" button focus bubble being slightly off center [#2130](https://github.com/bigcommerce/cornerstone/pull/2130)
- Apply dependency updates (jest & lighthouse). [#2132](https://github.com/bigcommerce/cornerstone/pull/2132)

Expand Down
21 changes: 19 additions & 2 deletions assets/js/theme/common/form-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createTranslationDictionary } from './utils/translations-utils';
* @param validation
* @returns {{selector: string, triggeredBy: string, validate: Function, errorMessage: string}}
*/
function buildDateValidation($formField, validation) {
function buildDateValidation($formField, validation, requiredMessage) {
// No date range restriction, skip
if (validation.min_date && validation.max_date) {
const invalidMessage = `Your chosen date must fall between ${validation.min_date} and ${validation.max_date}.`;
Expand All @@ -30,6 +30,23 @@ function buildDateValidation($formField, validation) {
errorMessage: invalidMessage,
};
}
// Required Empty Date field
if (validation.required && (!validation.min_date || !validation.max_date)) {
const formElementId = $formField.attr('id');

return {
selector: `#${formElementId} select[data-label="year"]`,
triggeredBy: `#${formElementId} select:not([data-label="year"])`,
validate: (cb, val) => {
const day = $formField.find('select[data-label="day"]').val();
const month = $formField.find('select[data-label="month"]').val();
const year = val;

cb(day && month && year);
},
errorMessage: requiredMessage,
};
}
}

/**
Expand Down Expand Up @@ -97,7 +114,7 @@ function buildValidation($validateableElement, errorMessage) {
const formFieldSelector = `#${$validateableElement.attr('id')}`;

if (validation.type === 'datechooser') {
const dateValidation = buildDateValidation($validateableElement, validation);
const dateValidation = buildDateValidation($validateableElement, validation, errorMessage);

if (dateValidation) {
fieldValidations.push(dateValidation);
Expand Down
13 changes: 13 additions & 0 deletions templates/components/common/forms/date-options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{#unless this.value}}
<option value="">---</option>
{{#each this.items}}
<option value="{{value}}">{{label}}</option>
{{/each}}
{{else}}
{{#unless this.isRequired}}
<option value="">---</option>
{{/unless}}
{{#each this.items}}
<option {{#if ../this.value '==' value}} selected {{/if}}value="{{value}}">{{label}}</option>
{{/each}}
{{/unless}}
20 changes: 9 additions & 11 deletions templates/components/common/forms/date.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<div class="form-field" id="{{id}}" data-validation="{{validation}}" {{#if private_id}}data-type="{{private_id}}"{{/if}}>
<label class="form-label" for="{{id}}" >{{label}}
{{#if required}}<small>{{lang 'common.required' }}</small>{{/if}}</label>
<label class="form-label" for="{{id}}">
{{label}}
{{#if required}}
<small>{{lang 'common.required' }}</small>
{{/if}}
</label>
<div class="form-row form-row--third">
<div class="form-field">
<select class="form-select"
Expand All @@ -11,9 +15,7 @@
aria-required="{{required}}"
{{#if private_id}}data-field-type="{{private_id}}"{{/if}}
>
{{#each month.items}}
<option {{#if ../month.value '==' value}} selected {{/if}}value="{{value}}">{{label}}</option>
{{/each}}
{{> components/common/forms/date-options month isRequired=required}}
</select>
</div>
<div class="form-field">
Expand All @@ -25,9 +27,7 @@
aria-required="{{required}}"
{{#if private_id}}data-field-type="{{private_id}}"{{/if}}
>
{{#each day.items}}
<option {{#if ../day.value '==' value}} selected {{/if}}value="{{value}}">{{label}}</option>
{{/each}}
{{> components/common/forms/date-options day isRequired=required}}
</select>
</div>
<div class="form-field">
Expand All @@ -39,9 +39,7 @@
aria-required="{{required}}"
{{#if private_id}}data-field-type="{{private_id}}"{{/if}}
>
{{#each year.items}}
<option {{#if ../year.value '==' value}} selected {{/if}}value="{{value}}">{{label}}</option>
{{/each}}
{{> components/common/forms/date-options year isRequired=required}}
</select>
</div>
</div>
Expand Down