Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
167
rated 0 times [  174] [ 7]  / answers: 1 / hits: 32285  / 4 Years ago, thu, june 25, 2020, 12:00:00

I am working with reach hooks and to validate my form fields I am using react-hook-form as it is the best option for now


SO to validating my normal input fields I am doing just ref={register({ required: true })} then on submit it is checking for errors as I am importing errors from react-hook-form


But when I am doing same for select field it is not checking the error object


This is what I am doing


<label htmlFor="func" className="form_label">
Select function
</label>
<select name="func" ref={register({ required: true })}>
<option selected disabled>
Select function
</option>
<option value="5">Function 2</option>
<option value="6">Function 3</option>
</select>
{errors.func && (
<div>
<span>Function is required</span>
</div>
)}

I don't know what I am missing


My actual code is with dynamic data


so I am looping it like this


<Form.Control as="select" custom>
<option disabled selected>Select role</option>
{loading === false &&
data.get_roles.map((li) => (
<option value={li.user_type_id}>
{li.user_type}</option>
))}
</Form.Control>

React hook form


More From » reactjs

 Answers
13

try this code. I tried and it worked fine:


<label htmlFor="func" className="form_label">
Select function
</label>
<select name="func"
ref={register({
required: "select one option"
})}>
<option value=""></option>
<option value="5">Function 2</option>
<option value="6">Function 3</option>
</select>
{errors.func && <p style={{color:'red'}}> {errors.func.message}</p> }

[#50851] Saturday, June 13, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kieraelsies

Total Points: 718
Total Questions: 103
Total Answers: 104

Location: England
Member since Sun, May 21, 2023
1 Year ago
kieraelsies questions
Tue, Aug 3, 21, 00:00, 3 Years ago
Tue, Feb 23, 21, 00:00, 3 Years ago
Thu, Nov 12, 20, 00:00, 4 Years ago
Wed, Sep 9, 20, 00:00, 4 Years ago
Mon, Sep 16, 19, 00:00, 5 Years ago
;