WA-238 Specify daily limit validation. Should be greater than 0

This commit is contained in:
apanizo 2018-04-27 16:23:50 +02:00
parent 69a486a66f
commit fdca1781cb
2 changed files with 10 additions and 2 deletions

View File

@ -8,6 +8,14 @@ export const required = (value: Field) => (value ? undefined : 'Required')
export const mustBeNumber = (value: number) => export const mustBeNumber = (value: number) =>
(Number.isNaN(Number(value)) ? 'Must be a number' : undefined) (Number.isNaN(Number(value)) ? 'Must be a number' : undefined)
export const greaterThan = (min: number) => (value: string) => {
if (Number.isNaN(Number(value)) || Number.parseFloat(value) > Number(min)) {
return undefined
}
return `Should be greater than ${min}`
}
export const minValue = (min: number) => (value: string) => { export const minValue = (min: number) => (value: string) => {
if (Number.isNaN(Number(value)) || Number.parseInt(value, 10) >= Number(min)) { if (Number.isNaN(Number(value)) || Number.parseInt(value, 10) >= Number(min)) {
return undefined return undefined

View File

@ -2,7 +2,7 @@
import * as React from 'react' import * as React from 'react'
import Field from '~/components/forms/Field' import Field from '~/components/forms/Field'
import TextField from '~/components/forms/TextField' import TextField from '~/components/forms/TextField'
import { composeValidators, mustBeNumber, required, minValue } from '~/components/forms/validator' import { composeValidators, mustBeNumber, required, greaterThan } from '~/components/forms/validator'
import Block from '~/components/layout/Block' import Block from '~/components/layout/Block'
import { FIELD_DAILY_LIMIT } from '~/routes/open/components/fields' import { FIELD_DAILY_LIMIT } from '~/routes/open/components/fields'
@ -12,7 +12,7 @@ const DailyLimit = () => (
name={FIELD_DAILY_LIMIT} name={FIELD_DAILY_LIMIT}
component={TextField} component={TextField}
type="text" type="text"
validate={composeValidators(required, mustBeNumber, minValue(1))} validate={composeValidators(required, mustBeNumber, greaterThan(0))}
placeholder="Daily Limit*" placeholder="Daily Limit*"
text="Daily Limit" text="Daily Limit"
/> />