question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Field name with object path

See original GitHub issue

Is it possible to achieve sth like this currently?

<Formik
  initialValues={{ nested: { value: 'nested value' } }}
  validationSchema={schema}
  onSubmit={(values, { setSubmitting }) => {
    handleSubmit(values);
    setSubmitting(false);
  }}
>
  {() => (
    <Form>
      <Field name="nested.value" />
      <button type="submit">Save</button>
    </Form>
  )}
</Formik>

I know I could use mapPropsToValues and mapValuesToPayload, but I find it really verbose and inconvenient for such a simple and common case. If I had deeply nested object, it would be even worse.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
klis87commented, Oct 11, 2017

I tried to solve it in my custom component:

import React from 'react';
import { FormGroup, ControlLabel, FormControl, HelpBlock } from 'react-bootstrap';
import { get } from 'lodash';

const TextInput = ({ field, form: { touched, errors, values, setFieldValue }, ...props }) => (
  <FormGroup
    validationState={touched[field.name] && errors[field.name] ? 'error' : null}
  >
    <ControlLabel>{props.label}</ControlLabel>
    <FormControl
      type="text"
      {...field}
      {...props}
      value={get(values, field.name)}
      onChange={e => setFieldValue(field.name, e.target.value)}
    />
    {touched[field.name] && errors[field.name] && <HelpBlock>{errors[field.name]}</HelpBlock>}
  </FormGroup>
);

Please notice that I used _.get in value prop so reading is working for nested values. The problem is, that setFieldValue('nested.value', value) would add nested.value key to values object, instead of adjusting values.nested.value. The same problem I would have for setFieldError, setFieldTouched etc.

It seems that this library requires objects to be totally flat, but nested JSONs are really common. And flattening and “deflattening” in every form really kills productivity.

5reactions
jswitoncommented, Sep 3, 2018

First of all - thanks for your hard work!

@jaredpalmer Would be great to add also nested touched and errors proposal prototype. Because now I’m using get from lodash for them like below:

touched={get(touched, field.name)}

Where field.name corresponds to the nested object (eg. 'address.city')

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using field path names to retrieve field values - IBM
A field path name provides the path to a named Entity. You can use GetLocalFieldPathNames for a given record type and then use...
Read more >
Js or ES6 Get object field by path string when field has 2 or ...
fieldName "] you are checking if project has a key literally called "one.two.fieldName" including the dots. You can just split on dot and...
Read more >
GridViewTextBoxColumn FieldName - path to referenced object
I am trying to bind a property of one class with the fieldName property. I have this class: Public Class Test.
Read more >
Create or Edit key fields using Sales Path in Lightning
Lead and Opportunity object can have Key fields. ... Click New Path or Edit next to existing Path Name to customize 3. Click...
Read more >
Java Reflection Field Value Set setFieldValueWithPath(Object ...
Returns the value of a field identified using a path from the parent. License. Apache License. Parameter. Parameter, Description. object, Parent object. path...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found