N
N
Ninja Mate2016-03-29 08:13:18
JavaScript
Ninja Mate, 2016-03-29 08:13:18

How to set a "visibility" variable for a field in React?

There are Form components with inputs that are hidden under Panel, Collapse or immediately visible to the user. You need to specify all visible inputs as required for input.
Is it possible to do this somehow through a generic validation method? Something like a visibility variable and if true, then it is required for input. The difficulty is that there are about 900 inputs in the application ... How can I do this without creating 900 functions?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xtalen, 2016-03-29
@victorzadorozhnyy

<form onSubmit={this.handleSubmitTest}>

//Мы задаем один и тот же стейт на открытие фрагмента формы и на required,
//соответственно если форма открыта, то и поле необходимо
<Input type="checkbox"  onChange={()=>this.setState({check: !this.state.check})}/>

<Input type="text" placeholder="Enter UR Number" required={this.state.check}/>

<ButtonInput type="submit" value="Submit Button"/>
</form>

The browser will prompt that the field is required. If you want to do the validation yourself - intercept the onSubmit event and check event.target.myinput.required.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question