Last active
February 19, 2022 22:02
-
-
Save dmeents/13739fffa28e89470f45f7ee949ab89e to your computer and use it in GitHub Desktop.
Rendering a select field in Redux-Form 6 with validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import { Field, reduxForm } from 'redux-form'; | |
export const renderSelect = field => ( | |
<div> | |
<select {...field.input}/> | |
{field.touched && field.error && <div className="error">{field.error}</div>} | |
</div> | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function validate(formProps) { | |
const errors = {}; | |
if (!formProps.firstName) { | |
errors.firstName = 'Please enter a first name'; | |
} | |
if (!formProps.lastName) { | |
errors.lastName = 'Please enter a last name'; | |
} | |
if (!formProps.email) { | |
errors.email = 'Please enter an email'; | |
} | |
if (!formProps.phoneNumber) { | |
errors.phoneNumber = 'Please enter a phone number' | |
} | |
if(!formProps.sex) { | |
errors.sex = 'You must enter your sex!' | |
} | |
return errors; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { validate } from '../filepath/form_validation.js'; | |
import { renderSelect } from './form-fields.js'; | |
//render form field, missing all other components | |
//this is just the select object | |
<label>Gender:</label> | |
<Field name="sex" component={renderSelect}> | |
<option></option> | |
<option name="male">Male</option> | |
<option name="female">Female</option> | |
</Field> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi everyone,
I am using react 17.0.2
redux 4.1.2
react-redux 7.2.6
and redux-form 8.3.8
Unable to render the dropdown using Field from redux-Form.
Can someone guide me in right direction? Any help would be appreciated.