From c02632800afdfa3adbfa995f4e8cca8fd8801536 Mon Sep 17 00:00:00 2001 From: Rob Clayburn Date: Thu, 8 Nov 2018 22:11:33 +0000 Subject: [PATCH] add game name and player # validation --- client/src/Games/AddGame.tsx | 63 ++++++++++++++++++++++++------- client/src/Games/PlayerSelect.tsx | 23 +++++++++-- 2 files changed, 70 insertions(+), 16 deletions(-) diff --git a/client/src/Games/AddGame.tsx b/client/src/Games/AddGame.tsx index 69b1ff18..86ee1858 100644 --- a/client/src/Games/AddGame.tsx +++ b/client/src/Games/AddGame.tsx @@ -5,10 +5,12 @@ import { RouteComponentProps, withRouter, } from 'react-router-dom'; +import { required } from 'validate-promise'; import { Button, Form, + FormFeedback, FormGroup, Input, Label, @@ -43,6 +45,28 @@ const initialData: IAddGameRequest = { players: [], }; +const contract = [ + { + key: 'name', + msg: () => 'Name is required', + promises: [{ + rule: required, + }] + }, + { + key: 'players', + msg: () => 'Must have between 5 and 10 players', + promises: [{ + rule: (value: any, row, msg): Promise => { + if (value.length < 5 || value.length > 9) { + return Promise.reject(msg()); + } + return Promise.resolve(); + }, + }] + } +]; + const AddGame: React.SFC> = ({ history }) => { return ( > = ({ history }) => { history.push(`/games/${data.addGame.id}`); }}> {(addGame) => ( - initialData={initialData}> - {({ setValue, formData }) => { + + initialData={initialData} + contract={contract} + > + {({ setValue, formData, validate, errors }) => { return

New Game

@@ -67,24 +94,34 @@ const AddGame: React.SFC> = ({ history }) => { Name: setValue('name', e.target.value)} /> + { + errors.name && + {errors.name[0]} + + } p.value)} + errors={errors.players} onChange={(e) => setValue('players', e)} />