Guest User

Untitled

a guest
Oct 25th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. import React from 'react'
  2. import { Field, reduxForm ,SubmissionError} from 'redux-form'
  3.  
  4. const renderField = ({ input, label, type, meta: { touched, error, warning } }) => (
  5. <div className="form-group">
  6. <label>{label}</label>
  7. <input {...input} placeholder={label} type={type} className="form-control" />
  8. {touched && error && <div className="invalid-feedback d-block">{error}</div>}
  9.  
  10. </div>
  11. )
  12.  
  13.  
  14.  
  15. const Form2Child = (props)=>{
  16. const { handleSubmit, pristine, reset, submitting } = props
  17. return (
  18. <form onSubmit={handleSubmit} className="needs-validation" noValidate>
  19. <Field name="name" type="text" component={renderField} label="Name"/>
  20. <Field name="email" type="email" component={renderField} label="Email"/>
  21. <Field name="password" type="password" component={renderField} label="Password"/>
  22. <div>
  23. <button className="btn btn-primary" type="submit" disabled={submitting}>Submit</button>
  24. <button className="btn btn-default" type="button" disabled={pristine || submitting} onClick={reset}>Clear Values</button>
  25. </div>
  26. </form>
  27. )
  28. }
  29.  
  30. export default reduxForm({
  31. form: 'form2',
  32. })(Form2Child)
  33.  
  34. import React, { Component } from 'react'
  35. import Form2Child from './Form2Child'
  36. import axios from 'axios'
  37. import { SubmissionError } from 'redux-form';
  38.  
  39. class Form2 extends Component {
  40. constructor(){
  41. super();
  42. this.state={
  43.  
  44. }
  45. }
  46.  
  47.  
  48. handleSubmit=values=>{
  49. axios.post('http://localhost:4000/register',values)
  50. .then(res=>{
  51. console.log(res.data);
  52. if(res.status==200){
  53.  
  54. }
  55. }).catch(err=>{
  56. throw new SubmissionError(err.response.data); <---Here I Am Getting Error
  57. });
  58. }
  59.  
  60. render() {
  61. return (
  62. <Form2Child onSubmit={this.handleSubmit}/>
  63. )
  64. }
  65.  
  66. }
  67. export default Form2;
  68.  
  69. const isEmpty = value =>
  70. value === undefined ||
  71. value === null ||
  72. (typeof value === 'object' && Object.keys(value).length === 0) ||
  73. (typeof value === 'string' && value.trim().length === 0);
  74.  
  75. function validateRegisterInput(data) {
  76. let errors = {};
  77. data.name = !isEmpty(data.name) ? data.name : '';
  78. data.email = !isEmpty(data.email) ? data.email : '';
  79. data.password = !isEmpty(data.password) ? data.password : '';
  80. if (!Validator.isLength(data.name, { min: 2, max: 30 })) {
  81. errors.name = 'Name must be between 2 and 30 characters';
  82. }
  83.  
  84. if (Validator.isEmpty(data.name)) {
  85. errors.name = 'Name field is required';
  86. }
  87.  
  88. if (Validator.isEmpty(data.email)) {
  89. errors.email = 'Email field is required';
  90. }
  91.  
  92. if (!Validator.isEmail(data.email)) {
  93. errors.email = 'Email is invalid';
  94. }
  95.  
  96. if (Validator.isEmpty(data.password)) {
  97. errors.password = 'Password field is required';
  98. }
  99.  
  100. if (!Validator.isLength(data.password, { min: 6, max: 30 })) {
  101. errors.password = 'Password must be at least 6 characters';
  102. }
  103. return {
  104. errors,
  105. isValid: isEmpty(errors)
  106. };
  107. }
  108.  
  109. app.post('/register',function(req,res){
  110. const { errors, isValid } = validateRegisterInput(req.body);
  111. if (!isValid) {
  112. return res.status(400).json(errors);
  113. }
  114. Users.findOne({ email: req.body.email }).then(user => {
  115. if (user) {
  116. errors.email = 'Email already exists';
  117. return res.status(400).json(errors);
  118. } else {
  119. const newUser = new User({
  120. name: req.body.name,
  121. email: req.body.email,
  122. password: req.body.password
  123. });
  124. newUser
  125. .save()
  126. .then(user => res.json(user))
  127. .catch(err => console.log(err));
  128. }
  129. });
  130. })
Add Comment
Please, Sign In to add comment