微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

反应表单内的 HandleSubimt 上未发生验证

如何解决反应表单内的 HandleSubimt 上未发生验证

如前所述,我有一些需要验证的字段,但它并没有在 react-form 上发生。我正在学习“Modern React with Redux”课程,我有以下代码,其中包含必须针对空值进行验证的 redux 表单:

import React from "react";
import {Field,reduxForm} from 'redux-form';

class StreamCreate extends React.Component {
    renderInput({input,label,Meta}){
        console.log(Meta)
        return (
            <div className="field">
                <label>{label}</label>
                <input {...input}/>
            </div>
        )
    }

    onSubmit(formValues){
        console.log(formValues);
    }
    
    render(){
        return(
            <form onSubmit={this.props.handleSubmit(this.onSubmit)} className="ui form">
                <Field name="title" component={this.renderInput} 
                    label="enter title"
                />
                <Field name="description" component={this.renderInput} 
                    label="enter description"
                />
                <button className="ui button primary">Submit</button>
            </form>
        )
    }
} 

const validate = (formValues) => { // This is not running!
    const errors = {};

    if (!formValues.title){
        errors.title = "you must enter a title";
    }
    if (!formValues.description){
        errors.description = "you must enter a description";
    }
}

export default reduxForm({
    form: 'streamCreate',validate // I followed the example to the T and checked the documentation
})(StreamCreate);

文档如下:

https://redux-form.com/8.2.2/examples/syncvalidation/

任何帮助将不胜感激!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。