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

UI/Fix form validation issues #15560

Merged
merged 18 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions changelog/15560.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
ui: fix form validations ignoring default values and disabling submit button
```
17 changes: 10 additions & 7 deletions ui/app/utils/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ import { isPresent } from '@ember/utils';
export const presence = (value) => isPresent(value);

export const length = (value, { nullable = false, min, max } = {}) => {
let isValid = nullable;
if (typeof value === 'string') {
const underMin = min && value.length < min;
const overMax = max && value.length > max;
isValid = underMin || overMax ? false : true;
if (!min && !max) return;
// value could be an integer if the attr has a default value of some number
const valueLength = value?.toString().length;
if (valueLength) {
const underMin = min && valueLength < min;
const overMax = max && valueLength > max;
return underMin || overMax ? false : true;
}
return isValid;
return nullable;
};

export const number = (value, { nullable = false, asString } = {}) => {
if (!value) return nullable;
// since 0 is falsy, !value is true even though 0 is valid here
hellobontempo marked this conversation as resolved.
Show resolved Hide resolved
if (!value && value !== 0) return nullable;
if (typeof value === 'string' && !asString) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion ui/lib/core/addon/components/form-field.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@
readonly={{this.isReadOnly}}
autocomplete="off"
spellcheck="false"
value={{or (get @model this.valuePath) @attr.options.defaultValue}}
value={{or @attr.options.defaultValue (get @model this.valuePath)}}
hellobontempo marked this conversation as resolved.
Show resolved Hide resolved
hellobontempo marked this conversation as resolved.
Show resolved Hide resolved
onchange={{this.onChangeWithEvent}}
onkeyup={{this.handleKeyUp}}
class="input {{if this.validationError 'has-error-border'}}"
Expand Down
2 changes: 1 addition & 1 deletion ui/lib/core/addon/components/form-field.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default class FormFieldComponent extends Component {
get validationError() {
const validations = this.args.modelValidations || {};
const state = validations[this.valuePath];
return state && !state.isValid ? state.errors.join('. ') : null;
return state && !state.isValid ? state.errors.join(' ') : null;
}

onChange() {
Expand Down
16 changes: 13 additions & 3 deletions ui/tests/unit/utils/validators-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ module('Unit | Util | validators', function (hooks) {
check(null);
assert.false(isValid, 'Invalid when nullable is false');
check('12');
assert.false(isValid, 'Invalid when not min length');
assert.false(isValid, 'Invalid when string not min length');
check('123456');
assert.false(isValid, 'Invalid when over max length');
assert.false(isValid, 'Invalid when string over max length');
check('1234');
assert.true(isValid, 'Valid when in between min and max length');
assert.true(isValid, 'Valid when string between min and max length');
check(12);
assert.false(isValid, 'Invalid when integer not min length');
check(123456);
assert.false(isValid, 'Invalid when integer over max length');
check(1234);
assert.true(isValid, 'Valid when integer between min and max length');
});

test('it should validate number', function (assert) {
Expand All @@ -47,5 +53,9 @@ module('Unit | Util | validators', function (hooks) {
assert.true(isValid, 'Valid for number as string');
check('foo');
assert.false(isValid, 'Invalid for string that is not a number');
check(0);
assert.true(isValid, 'Valid for 0 as an integer');
check('0');
assert.true(isValid, 'Valid for 0 as a string');
});
});