Advertisement
Guest User

Untitled

a guest
May 16th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2.  
  3. class Register extends Component {
  4.   constructor() {
  5.     super();
  6.     this.state = {
  7.       displayName: '',
  8.       displayNameError: '',
  9.       email: '',
  10.       emailError: '',
  11.       password: '',
  12.       passwordError: '',
  13.       passwordConfirm: '',
  14.       passwordConfirmError: '',
  15.       onSubmitCheck: ['displayName', 'email', 'password', 'passwordConfirm']
  16.     };
  17.  
  18.     this.handleChange = this.handleChange.bind(this);
  19.     this.submitUser = this.submitUser.bind(this);
  20.     this.validateField = this.validateField.bind(this);
  21.   }
  22.  
  23.   handleChange(evt) {
  24.     // This would also be a good place to validate fields too
  25.     this.setState({ [evt.target.name]: evt.target.value }, this.validateField(evt.target.name, evt.target.value));
  26.   }
  27.  
  28.   submitUser(evt) {
  29.     evt.preventDefault()
  30.     let { onSubmitCheck } = this.state
  31.  
  32.     // Need to validate fields first
  33.     onSubmitCheck.forEach(fieldName => {
  34.       this.validateField(fieldName, this.state[fieldName])
  35.     })
  36.  
  37.     // If there are no errors, then register the user
  38.     for (let index = 0; index < onSubmitCheck.length; index++) {
  39.       if (this.state[onSubmitCheck[index] + 'Error'] !== '') {
  40.         // there's an error so do nothing
  41.         return
  42.       }
  43.     }
  44.     // No errors found proceed
  45.     console.log("Submitting Form...")
  46.   }
  47.  
  48.   validateField(fieldName, value) {
  49.     // We can import the validators from somewhere else, say AJV for example.
  50.     let validators = {
  51.       displayName: (value) => {
  52.         if (value !== "a") {
  53.           this.setState({ displayNameError: 'An error has occurred' })
  54.         } else {
  55.           this.setState({ displayNameError: '' })
  56.         }
  57.       },
  58.       email: (value) => {
  59.         if (value !== "a") {
  60.           this.setState({ emailError: 'An error has occurred' })
  61.         } else {
  62.           this.setState({ emailError: '' })
  63.         }
  64.       },
  65.       password: (value) => {
  66.         if (value !== "a") {
  67.           this.setState({ passwordError: 'An error has occurred' })
  68.         } else {
  69.           this.setState({ passwordError: '' })
  70.         }
  71.       },
  72.       passwordConfirm: (value) => {
  73.         if (value !== "a") {
  74.           this.setState({ passwordConfirmError: 'An error has occurred' })
  75.         } else {
  76.           this.setState({ passwordConfirmError: '' })
  77.         }
  78.       }
  79.     }
  80.  
  81.     validators[fieldName](value)
  82.     // If I move this out, I'll need to update the above to
  83.     // this.setState({fieldName: validators[fieldName](value)})
  84.   }
  85.  
  86.  
  87.   // was-validated should not be used because 1. It can only really be applied at the end. 2. It only pays attention to HTML5 validity api and ignores
  88.   // custom validation. Ie, if a required field is not empty, was-validated will mark that as valid, even if we have custom validation that's failing
  89.   render() {
  90.     return (
  91.       <form className="mt-3" onSubmit={this.submitUser} noValidate>
  92.         <div className="container">
  93.           <div className="row justify-content-center">
  94.             <div className="col-lg-8">
  95.               <div className="card bg-light">
  96.                 <div className="card-body">
  97.                   <h3 className="font-weight-light mb-3">Register</h3>
  98.                   <div className="form-row">
  99.                     <section className="col-sm-12 form-group">
  100.                       <label
  101.                         className="form-control-label sr-only"
  102.                         htmlFor="displayName"
  103.                       >
  104.                         Display Name
  105.                       </label>
  106.                       <input
  107.                         className={this.state.displayNameError ? "form-control is-invalid" : "form-control"}
  108.                         type="text"
  109.                         id="displayName"
  110.                         placeholder="Display Name"
  111.                         name="displayName"
  112.                         required
  113.                         value={this.state.displayName}
  114.                         onChange={this.handleChange}
  115.                       />
  116.                       <div className="valid-feedback">
  117.                         Looks good!
  118.                       </div>
  119.                       <div className="invalid-feedback">
  120.                         {this.state.displayNameError}
  121.                       </div>
  122.                     </section>
  123.                   </div>
  124.                   <section className="form-group">
  125.                     <label
  126.                       className="form-control-label sr-only"
  127.                       htmlFor="email"
  128.                     >
  129.                       Email
  130.                     </label>
  131.                     <input
  132.                       className={this.state.emailError ? "form-control is-invalid" : "form-control"}
  133.                       type="email"
  134.                       id="email"
  135.                       placeholder="Email Address"
  136.                       required
  137.                       name="email"
  138.                       value={this.state.email}
  139.                       onChange={this.handleChange}
  140.                     />
  141.                     <div className="invalid-feedback">
  142.                       {this.state.emailError}
  143.                     </div>
  144.                   </section>
  145.                   <div className="form-row">
  146.                     <section className="col-sm-6 form-group">
  147.                       <input
  148.                         className={this.state.passwordError ? "form-control is-invalid" : "form-control"}
  149.                         type="password"
  150.                         name="password"
  151.                         placeholder="Password"
  152.                         value={this.state.password}
  153.                         onChange={this.handleChange}
  154.                       />
  155.                       <div className="invalid-feedback">
  156.                         {this.state.passwordError}
  157.                       </div>
  158.                     </section>
  159.                     <section className="col-sm-6 form-group">
  160.                       <input
  161.                         className={this.state.passwordConfirmError ? "form-control is-invalid" : "form-control"}
  162.                         type="password"
  163.                         required
  164.                         name="passwordConfirm"
  165.                         placeholder="Confirm Password"
  166.                         value={this.state.passwordConfirm}
  167.                         onChange={this.handleChange}
  168.                       />
  169.                       <div className="invalid-feedback">
  170.                         {this.state.passwordConfirmError}
  171.                       </div>
  172.                     </section>
  173.                   </div>
  174.                   <div className="form-group text-right mb-0">
  175.                     <button className="btn btn-primary" type="submit">
  176.                       Register
  177.                     </button>
  178.                   </div>
  179.                 </div>
  180.               </div>
  181.             </div>
  182.           </div>
  183.         </div>
  184.       </form>
  185.     );
  186.   }
  187. }
  188.  
  189. export default Register;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement