我正在使用官方的Semantic UI React组件来创建Web应用程序.我的注册页面上有一个表单,其中包含电子邮件字段,密码字段和确认密码字段.
import {Component} from 'react'; import {Button, Form, Message} from 'semantic-ui-react'; import {signUp} from '../../actions/auth'; class SignUp extends Component { constructor(props) { super(props); this.handleSubmit = this.handleSubmit.bind(this); } handleSubmit(e, {formData}) { e.preventDefault(); // // Potentially need to manually validate fields here? // // Send a POST request to the server with the formData this.props.dispatch(signUp(formData)).then(({isAuthenticated}) => { if (isAuthenticated) { // Redirect to the home page if the user is authenticated this.props.router.push('/'); } } } render() { const {err} = this.props; return (
现在,我习惯了常规的语义UI库,它有一个Form Validation插件.通常,我会在单独的JavaScript文件中定义这样的规则
$('.ui.form').form({ fields: { email: { identifier: 'email', rules: [{ type: 'empty', prompt: 'Please enter your email address' }, { type: 'regExp', value: "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", prompt: 'Please enter a valid email address' }] }, password: { identifier: 'password', rules: [{ type: 'empty', prompt: 'Please enter your password' }, { type: 'minLength[8]', prompt: 'Your password must be at least {ruleValue} characters' }] }, confirmPassword: { identifier: 'confirmPassword', rules: [{ type: 'match[password]', prompt: 'The password you provided does not match' }] } } });
是否有类似的方法使用Semantic UI React组件验证表单?我搜索了文档但没有任何成功,似乎没有使用此Semantic UI React库进行验证的示例.
我是否需要在handleSubmit
函数中手动验证每个字段?解决此问题的最佳方法是什么?谢谢您的帮助!
在大多数情况下,您必须手动验证表单。然而,RSUI包括一对夫妇的工具,使事情变得更简单,特别是在错误的道具和
这是我最近整理的一个表格示例。它可以使用一些重构,但是基本上可以通过将每个输入与一个onChange()
函数绑定到状态,然后将回调传递到Submit函数来控制加载屏幕的可见性和“ Success。谢谢您提交”部分。表格。
export default class MeetingFormModal extends Component { constructor(props) { super(props) this.state = { firstName: '', lastName: '', email: '', location: '', firstNameError: false, lastNameError: false, emailError: false, locationError: false, formError: false, errorMessage: 'Please complete all required fields.', complete: false, modalOpen: false } this.submitMeetingForm = this.submitMeetingForm.bind(this); this.successCallback = this.successCallback.bind(this); } successCallback() { this.setState({ complete: true }) setTimeout( () => {this.setState({modalOpen: false})}, 5000); this.props.hideLoading(); } handleClose = () => this.setState({ modalOpen: false }) handleOpen = () => this.setState({ modalOpen: true }) submitMeetingForm() { let error = false; if (this.state.studentFirstName === '') { this.setState({firstNameError: true}) error = true } else { this.setState({firstNameError: false}) error = false } if (this.state.studentLastName === '') { this.setState({lastNameError: true}) error = true } else { this.setState({lastNameError: false}) error = false } if (this.state.email === '') { this.setState({emailError: true}) error = true } else { this.setState({emailError: false}) error = false } if (this.state.location === '') { this.setState({locationError: true}) error = true } else { this.setState({locationError: false}) error = false } if (error) { this.setState({formError: true}) return } else { this.setState({formError: false}) } let meeting = { first_name: this.state.firstName, last_name: this.state.lastName, email: this.state.email, location: this.state.location, this.props.createMeeting(meeting, this.successCallback) this.props.showLoading(); } capitalize(string) { return string.charAt(0).toUpperCase() + string.slice(1); } render() { return(Schedule Now} open={this.state.modalOpen} onClose={this.handleClose} closeIcon={true} > ) } }Schedule Your Interview {!this.state.complete ? {!this.state.complete ?: this.setState({firstName: e.target.value})} label='First Name' placeholder="First Name..." error={this.state.firstNameError}/> this.setState({lastName: e.target.value})} label='Last Name' placeholder="Last Name..." error={this.state.lastNameError}/> this.setState({email: e.target.value})} label='Email' placeholder="Email..." error={this.state.emailError}/> this.setState({location: e.target.value})} label='Location' placeholder='City, State/Province, Country...' error={this.state.locationError}/> }Thanks for scheduling a meeting, {this.capitalize(this.state.name)}. We've received your information and we'll be in touch shortly.
: null }
希望有帮助!