mirror of
https://github.com/ansible/awx.git
synced 2026-03-02 01:08:48 -03:30
fix integer/float errors in survey
This commit is contained in:
@@ -18,6 +18,8 @@ import {
|
|||||||
noWhiteSpace,
|
noWhiteSpace,
|
||||||
combine,
|
combine,
|
||||||
maxLength,
|
maxLength,
|
||||||
|
integer,
|
||||||
|
number as numberValidator,
|
||||||
} from '../../../util/validators';
|
} from '../../../util/validators';
|
||||||
|
|
||||||
function AnswerTypeField({ i18n }) {
|
function AnswerTypeField({ i18n }) {
|
||||||
@@ -177,7 +179,15 @@ function SurveyQuestionForm({
|
|||||||
<FormField
|
<FormField
|
||||||
id="question-default"
|
id="question-default"
|
||||||
name="default"
|
name="default"
|
||||||
validate={maxLength(formik.values.max, i18n)}
|
validate={
|
||||||
|
{
|
||||||
|
text: maxLength(formik.values.max, i18n),
|
||||||
|
integer: integer(i18n),
|
||||||
|
float: numberValidator(i18n),
|
||||||
|
}[formik.values.type]
|
||||||
|
}
|
||||||
|
min={formik.values.min}
|
||||||
|
max={formik.values.max}
|
||||||
type={formik.values.type === 'text' ? 'text' : 'number'}
|
type={formik.values.type === 'text' ? 'text' : 'number'}
|
||||||
label={i18n._(t`Default answer`)}
|
label={i18n._(t`Default answer`)}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ export function noWhiteSpace(i18n) {
|
|||||||
|
|
||||||
export function integer(i18n) {
|
export function integer(i18n) {
|
||||||
return value => {
|
return value => {
|
||||||
|
console.log(value);
|
||||||
const str = String(value);
|
const str = String(value);
|
||||||
if (/[^0-9]/.test(str)) {
|
if (/[^0-9]/.test(str)) {
|
||||||
return i18n._(t`This field must be an integer`);
|
return i18n._(t`This field must be an integer`);
|
||||||
@@ -76,6 +77,16 @@ export function integer(i18n) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function number(i18n) {
|
||||||
|
return value => {
|
||||||
|
const str = String(value);
|
||||||
|
if (/^[0-9]*(\.[0-9]*)?$/.test(str)) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
return i18n._(t`This field must be a number`);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export function url(i18n) {
|
export function url(i18n) {
|
||||||
return value => {
|
return value => {
|
||||||
if (!value) {
|
if (!value) {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import {
|
|||||||
maxLength,
|
maxLength,
|
||||||
noWhiteSpace,
|
noWhiteSpace,
|
||||||
integer,
|
integer,
|
||||||
|
number,
|
||||||
url,
|
url,
|
||||||
combine,
|
combine,
|
||||||
regExp,
|
regExp,
|
||||||
@@ -112,6 +113,24 @@ describe('validators', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('number should accept number (number)', () => {
|
||||||
|
expect(number(i18n)(13)).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('number should accept number (string)', () => {
|
||||||
|
expect(number(i18n)('13')).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('number should accept decimal/float', () => {
|
||||||
|
expect(number(i18n)(13.1)).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('number should reject string containing alphanum', () => {
|
||||||
|
expect(number(i18n)('15a')).toEqual({
|
||||||
|
id: 'This field must be a number',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
test('url should reject incomplete url', () => {
|
test('url should reject incomplete url', () => {
|
||||||
expect(url(i18n)('abcd')).toEqual({
|
expect(url(i18n)('abcd')).toEqual({
|
||||||
id: 'Please enter a valid URL',
|
id: 'Please enter a valid URL',
|
||||||
|
|||||||
Reference in New Issue
Block a user