Posts

Showing posts from May, 2018

React form validation and test cases.

In Component: import React , { Component } from 'react' ; import './InputForm.css' ; class InputForm extends Component { constructor ( props ) { super ( props ); this . onFormSubmit = this . onFormSubmit . bind ( this ); this . validate = this . validate . bind ( this ); this . onInputChange = this . onInputChange . bind ( this ); this . state = { fields : {}, fieldErrors : {}, }; } onInputChange ( e ) { const fields = this . state . fields ; const newFields = {}; newFields [ e . target . name ] = e . target . value ; this . setState ({ fields : {... fields , ... newFields } }); } validate ( formData ) { const errors = {}; if (! formData . name || formData . name === '' || formData . name === null ) { errors . name = 'Please enter your name.' ; } return errors ; } onFormSubmit ( e