2

working on a react app from this tutorial, I require a library which handle form and validation and following 2 library comes in the most search results

  1. redux-form
  2. react-redux-form

First of all, not sure which need to use so tried the later one with following steps

Please check the demo here

1) use createForms() in combineReducers in reducers.js

const initialUserState = { firstName: '', lastName: '', userName: '', email: '', password: '', confirmPassword: '' }; const rootReducer = combineReducers({ ...createForms({ user: initialUserState }) }); 

2) in RegisterPage.js I have used and Component to create form

class RegisterPage extends React.Component { constructor(props) { super(props); this.state = { user: { firstName: '', lastName: '', username: '', email: '', password: '', confirmPassword: '' }, submitted: false }; this.handleChange = this.handleChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this); } handleChange(event) { const { name, value } = event.target; console.log(`handleChange called => name ${name}, value: ${value}`); const { user } = this.state; this.setState({ user: { ...user, [name]: value } }); } handleSubmit(user) { console.log("handleSubmit state", this.state); this.setState({ submitted: true }); const { user } = this.state; console.log("user", user); const { dispatch } = this.props; dispatch(userActions.register(user)); } render() { return ( <div className="col-md-6 col-md-offset-3"> <h2>Register</h2> <Form model="user" onSubmit={(user) => this.handleSubmit(user)} > <div className="form-group"> <label htmlFor="firstName">First Name</label> <Control.text id="firstName" className="form-control" model=".firstName" onChange={this.handleChange} validators={{ required: isRequired, maxLength: maxLength(50) }} validateOn="blur" /> <Errors model=".firstName" className="errors text-danger" show="touched" messages={{ required: 'Please provide First Name.', maxLength: 'Maximum length can be 50 characters.' }} /> </div> <div className="form-group"> <label htmlFor="lastName">Last Name</label> <Control.text className="form-control" model=".lastName" onChange={this.handleChange} /> </div> <div className="form-group"> <label htmlFor="username">Username</label> <Control.text className="form-control" model=".username" onChange={this.handleChange} /> </div> <div className="form-group"> <label htmlFor="firstName">Email Address</label> <Control.text className="form-control" model=".email" onChange={this.handleChange} /> </div> <div className="form-group"> <label htmlFor="password">Password</label> <Control type="password" className="form-control" model=".password" onChange={this.handleChange} /> </div> <div className="form-group"> <label htmlFor="confirmPassword">Confirm Password</label> <Control type="password" id="confirmPassword" className="form-control" model=".confirmPassword" onChange={this.handleChange} /> </div> <div className="form-group"> <button type="submit" className="btn btn-primary" >Register</button> </div> </Form> </div> ); } } const connectedRegisterPage = connect()(RegisterPage); export { connectedRegisterPage as RegisterPage }; 

and when I click on submit button, the state changed to "USER_REGISTER_REQUEST" state but it adds the additional property in user data as following

user: { confirmPassword : "" email: "" firstName: "" lastName: "" password: "" user.firstName: "foo" << added extra property username: "" 

So my collective queries are (Please allow me to ask similar question in one post, request you to not to downvote, they are all closely-related )

  1. Why do we need to define this.state = { user : {} } again in RegisterPage constructor() when we have already define as initialUserState in combineReducer. I am not completely understanding this, Please help me.

  2. How to disable submit button until all required field are not filled up? calling from angular background, we use ng-pristine, ng-touched, ng-valid and many other directives to check the form field and write an expression in ng-disabled to disabled submit button.

so does that similar functionality can be implemented in react?

  1. Also checked in console that each time focus/blur state is being called unnecessarily (type: "rrf/focus"). How to prevent them or this is normal react behavior and does not impact our page in general?

  2. Why is this adding additional data in user object while submit?

  3. Which library is more user-friendly from above?

2 Answers 2

2

First i use redux.. The easiest way to do this is to set your disable = {invalid}.

<Button type="submit" disabled={invalid}>Submit</Button>

in the props if you console.log(props);

you will find many keys for example invalid, valid, pristine, submit, submitting.

most of them has Boolean values true and false and it changes with you doing actions on the form.

when my form is not valid (invalid) invalid will be true.

but when the fields are filled the invalid variable will be false.

Here you use it.

const SyncValidationForm = (props) => { const {handleSubmit, pristine, reset, submitting, invalid} = props; console.log(props);

set invalid to props to use it.

and then just add disable = {invalid}

BUT i have a better easy solution: do something like that :

disabled="{!this.state.user.lastName || !this.state.username.email || !this.state.user.password}

this will keep returning true until all the fields are filled

Sign up to request clarification or add additional context in comments.

Comments

0

The best thing to do would be to follow the docs of those libraries. Both have clear and easy demos on how to create forms.

Redux Form docs

React Redux Form docs

I have experience with both, personally I'd chose react-redux-form for it's flexibility.


Though to answer your questions -

  1. No you do not need to use this.state at all to handle the forms because it is all handled within redux state.
  2. To disable form components, read this. It's the docs on Control component using the disabled prop
  3. Don't worry about blur and focus. Those will not affect your performance
  4. You have defined user.firstName in the model of the component. Please read this page on models.
  5. As specified above, I recommend react-redux-form

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.