Skip to content

Commit

Permalink
fix(storefront): BCTHEME-83 added basic validation for Account Signup…
Browse files Browse the repository at this point in the history
… Date Field
  • Loading branch information
bc-alexsaiannyi committed Oct 28, 2021
1 parent 74b4671 commit 6b83d54
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
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)

## 6.1.0 (09-03-2021)
- Fixed images placeholder on hero carousel shifted on mobile when slide has content. [#2112](https://github.com/bigcommerce/cornerstone/pull/2112)
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
10 changes: 10 additions & 0 deletions templates/components/common/forms/date-options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{#unless this.value}}
<option value="">---</option>
{{#each this.items}}
<option value="{{value}}">{{label}}</option>
{{/each}}
{{else}}
{{#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}}
</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}}
</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}}
</select>
</div>
</div>
Expand Down

0 comments on commit 6b83d54

Please sign in to comment.