Redux form - how to set fields as touched

Jan Omacka picture Jan Omacka · Sep 1, 2016 · Viewed 16.7k times · Source

I'm working with form that consists of multiple pages and I want to solve validation.

When I hit Submit button all fields on the present page shows error messages beneath, but if I change the page then I need to hit submit again because these fields weren't set as touched.

My problem would be solved if I could for example set all fields on the page as touched, once the form has flag anyTouched: true.

I'm using redux-form: '^6.0.0-rc.4' and I have one container where I include redux-form and multiple components consisting of fields.

Answer

davnicwil picture davnicwil · Aug 15, 2017

I think your problem was the opposite way around, but in case anyone lands here as I did looking for a way to have anyTouched set after any field in the form is touched...

In redux-form 6 and above you have to explicitly choose the behaviour you want with the form-level configurations touchOnChange and touchOnBlur - see the docs here - by default nothing is configured and so nothing happens.

const Form = reduxForm({
  form: 'my-form',
  touchOnChange: true,
  touchOnBlur: true
})(...)

These flags make it so that any given field is marked as touched (and therefore anyTouched is marked true on the form) when that field's onChange or onBlur handler is called, respectively.