简体   繁体   English

Redux Form验证不起作用

[英]The Redux Form validation is not working

I am new in react. 我是新来的。 Now if I am trying to put value in input fields, but not being able to do that showing blocked. 现在,如果我尝试在输入字段中输入值,但无法执行此操作,则显示阻塞。 Also no validation happening. 也没有验证发生。 Any suggestion will be appreciated. 任何建议将不胜感激。 Please feel free to ask if you need any more information. 请随时询问您是否需要更多信息。

package.js file package.js文件

"dependencies": {
    "axios": "^0.17.1",
    "react": "^16.0.0",
    "react-bootstrap": "^0.31.5",
    "react-dom": "^16.0.0",
    "react-redux": "^5.0.6",
    "react-router-dom": "^4.2.2",
    "redux": "^3.7.2",
    "redux-form": "^7.1.2",
    "redux-thunk": "^2.2.0"
  }

reducer/index.js 减速机/ index.js

import {combineReducers} from 'redux';
import {reducer as formReducer} from 'redux-form';
const rootReducer = combineReducers({
    form: formReducer
});

export default rootReducer;

root/index.js 根/ index.js

import React from 'react';
import ReactDOM from 'react-dom';
import {Provider} from 'react-redux';
import {createStore, applyMiddleware} from 'redux';
import {BrowserRouter, Route} from 'react-router-dom';
import reduxThunk from 'redux-thunk';

import reducers from './reducers';

import App from './components/app';
import Header from './components/main/header';
import Footer from './components/main/footer';
import Dashboard from './components/main/dashboard';
import Signin from './components/auth/signin';
import Signup from './components/auth/signup';
import About from './components/main/about';
import Contact from './components/main/contact';
import { reducer } from 'redux-form';

const createStoreWithMiddleware = applyMiddleware(reduxThunk)(createStore);

ReactDOM.render(    
        <Provider store={createStoreWithMiddleware(reducer)}>
            <BrowserRouter>
                <div className="container">
                    <Header />
                    <Route path="/" component={App} />
                    <Route exact path="/" component={Dashboard} />
                    <Route path="/signin" component={Signin} />
                    <Route path="/signup" component={Signup} />
                    <Route path="/about" component={About} />
                    <Route path="/contact" component={Contact} />
                    <Footer />
                </div>
            </BrowserRouter>
        </Provider>
,document.getElementById('root')
);

components/signin.js 组件/ signin.js

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

class Signin extends Component{

    renderCommonInputField(field){
        return(
            <div className="form-group">
                <label>{field.label}</label>
                <input
                    className="form-control"
                    type={field.type}
                    placeholder={field.placeholder} 
                    {...field.input}
                />
            </div>
        );
    }

    onFormSubmit(values){
        console.log('test');

        event.preventDefault();
    }
    render(){
        const {handleSubmit} = this.props;
        return(
            <div>
                <form onSubmit={handleSubmit(this.onFormSubmit.bind(this))}>
                    <Field 
                        label="Enter your email"
                        name="email"
                        type="text"
                        placeholder="enter email ex@..example@empl.com"
                        component={this.renderCommonInputField}
                    />
                    <Field 
                        label="Enter your password"
                        name="password"
                        type="password"
                        placeholder="enter your password"
                        component={this.renderCommonInputField}
                    />                    
                    <button type="submit" className="btn btn-default">Submit</button>
                </form>
            </div>
        );
    }
}
function validate(values){
    const errors = {}
    if(!values.email){
        errors.email = "Email is required"
    }
    if(!values.password){
        errors.password = "Password is required"
    }
    return errors;
}
export default reduxForm({
    form: 'signinform',
    validate
})(Signin);

UPDATED: 更新:

Your problems is a little mistake when calling createStoreWithMiddleware on index.js. 在index.js上调用createStoreWithMiddleware时,您的问题是一个小错误。 The param should be reducer s and not reducer. 帕拉姆应减速且不减速机。

Replace: <Provider store={createStoreWithMiddleware(reducer)}> . 替换: <Provider store={createStoreWithMiddleware(reducer)}>

By: <Provider store={createStoreWithMiddleware(reducers)}> 创建人: <Provider store={createStoreWithMiddleware(reducers)}>

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

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