Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear some fields in form - Redux-Form

Tags:

I'm working on a page which has many input validations and logical bindings on it and every sprint iteration the page size increasing. So that, I have to find a beautiful and scalable solution.

Imagine, when user select a value from dropdown as 'A', some fields must be disabled, some fields must be cleared and some fields initilized with default values. I can change one related field (doesn't have validation rule like regexp or lenght constrait) value with some little code like

  this.props.dispatch(change('xForm','xField','xValue' )) 

My problem is that when I need to clear multiple fields,

It always blocked by my validation method and clear operation is failed ( Note : I supposed to be like that but not like that)

.

I tried some strategies as below but y,z,w fields have some text and it triggered validation rule and hanled errors. So that, inputs have still old values, not cleared ones.

    //Clear         this.props.dispatch(change('xForm','yField','' ))     this.props.dispatch(change('xForm','zField','' ))     this.props.dispatch(change('xForm','wField','' )) 

What are the best practises for clear inputs or assign some values to inputs in redux-form for pages which have highly dependent inputs.

I have been researching for 2 days but I couldn't find any optimal solution. (redux normalizer, redux form utils etc.)

Thanks.

like image 308
Tugrul Avatar asked May 12 '16 14:05

Tugrul


People also ask

How do you reset a particular field in Redux?

If we are talking about entering fields into a form, and then navigating away before submitting, this is the best solution. Place this in your componentDidMount() with the fields you want cleared.

How do you clear the form after submit in react Redux?

B) Simply unmount your form component For many use cases, you will want to either hide your form component after submission succeeds or navigate away to another page, which will cause redux-form 's default behavior of destroying the form data in the reducer in componentWillUnmount .

What is FieldArray in Redux form?

The FieldArray component is how you render an array of fields. It works a lot like Field . With Field , you give a name , referring to the location of the field in the Redux state, and a component to render the field, which is given the props to connect the field to the Redux state.

Is Redux form good?

Redux-form is a really great library for working with validations. You can simply develop a lot of validations for different situations. Hence it provides validation functions to validate all the values in your form at once. You may also provide individual value validation functions for each Field or FieldArray.


2 Answers

This worked for me:

resetAdvancedFilters(){         const fields = ['my','fields','do','reset','properly']         for (var i = 0; i < fields.length; i++) {             this.props.dispatch(change('formName',fields[i],null))             this.props.dispatch(untouch('formName',fields[i]))         }     } 
like image 134
chickenchilli Avatar answered Oct 04 '22 22:10

chickenchilli


Using the below, it clears the respective multiple fields and also clears the errors if any for those respective fields.

Common function to reset multiple fields.

import {change, untouch} from 'redux-form';  //reset the respective fields's value with the error if any resetFields = (formName, fieldsObj) => {       Object.keys(fieldsObj).forEach(fieldKey => {            //reset the field's value           this.props.dispatch(change(formName, fieldKey, fieldsObj[fieldKey]));            //reset the field's error           this.props.dispatch(untouch(formName, fieldKey));        }); } 

Use the above common function as,

this.resetFields('userDetails', {     firstName: '',     lastName: '',     dateOfBirth: '' }); 
like image 37
Aniruddha Shevle Avatar answered Oct 04 '22 23:10

Aniruddha Shevle