2

I'm trying to create a reusable higher solution for instant/client side form validations in react.

I thought of creating a validation class with validation methods and simply passing the relevant ones to the form fields.

So for example if I have a required email field it will be passed two methods from the validation class to check that.

Here's an abbreviated example:

class Validate {
  constructor (value) {
    this.value = value
  }

  required = () => {
    console.log('in validator required, value is :', this.value)
    if (!this.value.toString().trim().length) {
      return 'required'
    }
  }
  lt = () => {
    if (this.value.toString().trim().length < 50) {
      return 'extra details must be at least 50 characters'
    }
  }
}

export default class Form extends Component {
  constructor () {
    super()
    this.validate = new Validate(this.state.email) // Passing the field to the instance here means it will be undefined even after the state changed with new value for email.
  }
  /* ... */

  render () {
    const {
      email
    } = this.state.value
    const {
      required
    } = this.validate
    return (
      <form className='form__wrapper' onSubmit={this.onSubmit}>
        <InputText
          type='text'
          value={email}
          validations={required}
          /* ... */
        />
      </form>
    )
  }
}

const InputText = ({ value, validate }) => (
  <div className={`field-wrapper ${customClass}`} >
    <input
      value={value}
      /* ... */
    />
    {value ? validate(value) : null}
  </div>
)

My problem as, you can see from the comment above, is that I don't know how to pass my variables to the class so they will get updated when the user input data and the value of the email field has changed.

5
  • It seems a problem related to this. When validate is called, it will not be called with the appropriate this. Try replacing const { required } = this.validate with const required = this.validate.required.bind(this.validate) Commented Oct 18, 2017 at 9:37
  • Is it just trying to validate once, and then not updating the validations? Commented Oct 18, 2017 at 9:46
  • That's not the issue if I change this.validate = new Validate('some text') it's not undefined in the method. But if I use the variable from the state the Validate class is instantiated with an undefined value for the email, because the email is indeed undefined at that moment, but later when the user inputs an email the Validate class still has undefined as the value for the email field. Commented Oct 18, 2017 at 9:53
  • @Dream_Cap No I'm trying to update the validations as well in real time Commented Oct 18, 2017 at 9:54
  • Have you thought about having a handleChange function that sits on the form component and then calls to the validate class on every change to the input for validation? I'm not sure if it'll work, but maybe. Commented Oct 18, 2017 at 10:25

1 Answer 1

2

Truthfully, what I feel javascript is meant to be a functional language rather than a OOP one.

However, this feels like an anti-pattern to me. The value has to be updated within the class it belongs to even in OOPs (well other than public variables, but even then they are directly changed rather than by reference as far as I remember.

So the two solutions that I would recommend are:

  1. The easiest, use functional programming, pass in the values directly to the function.
  2. Make Validate function wrap around the form component, and then pass in email as a prop to form component, and change events too should be passed down and the ntriggered in form component.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.