Formik: getFieldValue for deep nested fields

Created on 29 Oct 2019  路  1Comment  路  Source: formium/formik

Hi ! This is a rewriting of automatically closed issue https://github.com/jaredpalmer/formik/issues/657 that contained 10+ participants

馃殌 Feature request

Current Behavior

When I'm working with deep nested elements, values[element.name] obviously does not work.

Desired Behavior

It should be easy to get form values, even when deep nested

Suggested Solution

add a getFieldValue helper that would work with deep nested fields and fieldList. It would use something similar as 'lodash's get

Describe alternatives you've considered

I had to add a custom function to do this with my deep nested field

export function getObjectValue(obj, path) {
    var fullPath = path
        .replace(/\[/g, '.')
        .replace(/]/g, '')
        .split('.')
        .filter(Boolean);

    return fullPath.every(everyFunc) ? obj : null;

    function everyFunc(step) {
        return !(step && (obj = obj[step]) === undefined);
    }
}

thank you 馃敟 !

Most helpful comment

There is a getIn function that formik exports that you can use, however it is not documented.

import { getIn } from 'formik'

function example(formik) {

    const value = getIn(formik.values, 'path.to.value');
    // do something with value.
}

This does exactly what your helper function does, though it should be officially documented.
It's also useful for accessing the touched and errors for a particular name too.

>All comments

There is a getIn function that formik exports that you can use, however it is not documented.

import { getIn } from 'formik'

function example(formik) {

    const value = getIn(formik.values, 'path.to.value');
    // do something with value.
}

This does exactly what your helper function does, though it should be officially documented.
It's also useful for accessing the touched and errors for a particular name too.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Jucesr picture Jucesr  路  3Comments

dearcodes picture dearcodes  路  3Comments

Jungwoo-An picture Jungwoo-An  路  3Comments

jaredpalmer picture jaredpalmer  路  3Comments

jaredpalmer picture jaredpalmer  路  3Comments