简体   繁体   English

在 React 中创建一个字段或所有字段

[英]Make a field or all fields required in React

I have an input field like this:我有一个这样的输入字段:

<Form.Field
  name="contactName"
  className=""
  control={Input}
  label="Contact Name"
  placeholder="Contact Name"
  value={this.state.contactName || ''}
  onChange={this.onChange}
/>

and I want to make it required so for that it should be add required attribute and now looks like this:我想让它成为必需的,因此应该添加required的属性,现在看起来像这样:

<Form.Field
  name="contactName"
  className=""
  control={Input}
  label="Contact Name"
  placeholder="Contact Name"
  value={this.state.contactName || ''}
  onChange={this.onChange}
  required
/>

I noticed that it also adds a red asterisk near the input's title.我注意到它还在输入标题附近添加了一个红色星号。 The problem is that the user can still click the submit button and they will not know that the entry was not created (they don't check the network to see the call failed).问题是用户仍然可以单击提交按钮,他们不会知道条目没有创建(他们不检查网络以查看呼叫失败)。

Is there a way to make it impossible to submit if the required data is not introduced?如果没有引入所需的数据,有没有办法使无法提交? Also something like a popup/hover saying that it requires data?还有像弹出/悬停说它需要数据的东西吗?

Assuming you use Semantic UI to style your component, you can make use of error prop of your <Form.Field /> to raise an error if form is not valid.假设您使用语义 UI 来设置组件样式,您可以使用<Form.Field />error属性在表单无效时引发错误。

To keep track of whether the form is valid, you may use your state.要跟踪表格是否有效,您可以使用 state。 As an option you may set disabled property of Submit button equal to the state variable indicating whether the form is valid.作为一个选项,您可以将Submit按钮的disabled属性设置为等于 state 变量,指示表单是否有效。

The very basic form validation (to match the pattern of one capital letter followed by lowercase letters) you may find in the following live-demo:您可以在以下现场演示中找到非常基本的表单验证(匹配一个大写字母后跟小写字母的模式):

 const { Component } = React, { render } = ReactDOM, { Form, Input, Button } = semanticUIReact const rootNode = document.getElementById('root') class App extends Component { state = { submitAttempted: false, isValid: { contactName: false, contactPhone: false, contactMail: false } } validateInput = (name,value) => { switch(name){ case 'contactName': { if(/^[AZ][az]+$/.test(value)){ return true } else return false } case 'contactPhone': { if(/^\d+$/.test(value)){ return true } else return false } case 'contactMail': { if(/^\w+@\w+\.\w{1,4}$/i.test(value)){ return true } else return false } default: return false } } onChange = ({target:{name,value}}) => this.setState({ [name]: value, isValid: {...this.state.isValid, [name]: this.validateInput(name, value) } }) onSubmit = e => { e.preventDefault() this.setState({ submitAttempted: true }) Object.values(this.state.isValid).every(Boolean) && console.log(this.state.contactName, this.state.contactPhone, this.state.contactMail) } render(){ return ( <Form onSubmit={this.onSubmit} > <Form.Field name="contactName" className="" control={Input} label="Contact Name" placeholder="Contact Name" value={this.state.contactName || ''} onChange={this.onChange} required error={( (this.state.submitAttempted &&.this.state.isValid:contactName) && { content, 'Valid contact name should contain upper-case letters followed by lower-case letters': pointing. 'below' } )} /> <Form.Field name="contactPhone" className="" control={Input} label="Contact Phone" placeholder="Contact Phone" value={this.state.contactPhone || ''} onChange={this.onChange} required error={( (this.state.submitAttempted &&.this.state:isValid,contactPhone) && { content, 'Valid phone: should contain numbers only'. pointing. 'below' } )} /> <Form.Field name="contactMail" className="" control={Input} label="Contact Mail" placeholder="example@hello.com" value={this.state.contactMail || ''} onChange={this.onChange} required error={( (this.state.submitAttempted &&.this:state,isValid:contactMail) && { content. 'Valid e-mail format should be fulfilled'. pointing. 'below' } )} /> <Button type="submit" disabled={ this.state.submitAttempted &&.Object,values(this.state.isValid).every(Boolean) } > Submit </Button> </Form> ) } } render ( <App />, rootNode )
 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css" /><script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.12.0/umd/react.production.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.11.0/umd/react-dom.production.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui-react/0.88.2/semantic-ui-react.min.js"></script><div id="root"></div>

You need to add your own validation to check if any of the inputs are empty or not when submit button is clicked if any input is empty show an alert.您需要添加自己的验证来检查任何输入是否为空,如果任何输入为空,则单击提交按钮时显示警报。

This is not an inbuilt functionality you need to add the code for validation and alert yourself.这不是您需要添加代码以进行验证和提醒自己的内置功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM