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.

Submit only Registered field

See original GitHub issue

Hi, Is there an option to submit only values from registered fields?

I have a form which I initialize with data from my rest api. All the data from my API goes to the values as my form values event if I didn’t register all the field, then after submission I receive all the data in the handleSubmit method. is it possible to filter only values from field which was registered (i.e. in the form )?

Thank you

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
henbjocommented, Dec 14, 2017

I know this is an old post, but I thought I’d post a solution to this problem.

I solved this by selecting registeredFields from the form state, and piping the field names into a formValueSelector. The only thing I dislike about this solution is having to maintain the getRegisteredFields selector myself, as it will break if and when you decide to change the internal structure of the Redux Form state. Having the library provide this selector would be a good thing.

import React from 'react';
import { connect } from 'react-redux';
import { createSelector } from 'reselect';
import { reduxForm, formValueSelector, Field } from 'redux-form';

/* selectors */
const getFormState = state => state.form;
const getNamedFormState = formName => createSelector([getFormState], (formState = {}) => formState[formName]);
const registeredFieldsSelector = formName => createSelector([getNamedFormState(formName)], (namedFormState = {}) => namedFormState.registeredFields);

/* Form component */
const MyFormComponent = ({ handleSubmit, showB }) => (
  <form onSubmit={handleSubmit}>
    <Field name="A" />
    {showB && <Field name="B" />}
    <button type="submit">Submit</button>
  </form>
);

const MY_FORM_NAME = 'MyForm';
const getRegisteredFields = registeredFieldsSelector(MY_FORM_NAME);
const getFormValues = formValueSelector(MY_FORM_NAME);

const mapStateToProps = (state) => {
  // This is where the magic happens
  const registeredFields = getRegisteredFields(state);
  const registeredFieldNames = Object.values(registeredFields).map(rf => rf.name);
  const valuesForRegisteredFieldsOnly = registeredFieldNames.length
    ? getFormValues(state, ...registeredFieldNames)
    : {};
  return { valuesForRegisteredFieldsOnly };
};

const MyForm = connect(mapStateToProps)(reduxForm({ form: MY_FORM_NAME })(MyFormComponent));

/* Container component that uses MyForm */
const submitMyForm = (values, dispatch, { valuesForRegisteredFieldsOnly }) => (
  dispatch(mySubmitAction({ data: valuesForRegisteredFieldsOnly }))
);

const MyContainerComponent = () => (
  <MyForm onSubmit={submitMyForm} />
);
4reactions
henbjocommented, Jan 13, 2018

@davepacifico Yes, like I said, that’s the only drawback with the solution as far as I can see. I don’t see a good reason why the library shouldn’t export this selector. If I can find the time I’ll whip together a PR.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Submit only Registered field · Issue #2368 · redux ... - GitHub
Hi, Is there an option to submit only values from registered fields? I have a form which I initialize with data from my...
Read more >
redux-form how to submit all registered fields - Stack Overflow
Ideally I'd be able to set some options to just submit the value of all registered fields, but that doesn't exist.
Read more >
My form is able to be submitted without required fields being ...
I am receiving for submissions from my "clone daystar registration" form and the requied fields are not filled in. I thought that the...
Read more >
Creating Custom Registration Fields - iThemes Help Center
This tutorial will walk you through how to add custom fields to these pages: Membership registration form [register_form]; Front-facing profile editor ...
Read more >
Submitting Team Entries for a Track & Field Meet
Overview · Accessing the Meet Registration Page · Entering Individual Athletes. By Event; Register by Event Sample; By Athlete; Register by Athlete Sample....
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