react-native-form-generator icon indicating copy to clipboard operation
react-native-form-generator copied to clipboard

Array of validators not working?

Open justinramel opened this issue 8 years ago • 0 comments

Anyone else having problems with an array of validations not working?

The result is always failure even when the individual validators pass.

I've tracked it down to src/lib/InputComponent.js

      if(this.props.validationFunction.constructor === Array){
        /*
        validationFunction has to return an object in case of error,
          true in case of successful validation
         */
        this.props.validationFunction.map((valFn, i)=>{

          let validationResult = valFn(value, this);
          if(validationResult === true){
            this.valid = (this.valid !== false)? validationResult : this.valid;
          } else{
            this.validationErrors.push(validationResult);
            this.valid = false;
          }

        })

Looks like this.valid needs to start as true or you always get a false result and also this.props.validationFunction.map is losing 'this' context, my fix:

      if(this.props.validationFunction.constructor === Array){
        /*
        validationFunction has to return an object in case of error,
          true in case of successful validation
         */
        this.valid = true;
        this.props.validationFunction.map((valFn, i)=>{

          let validationResult = valFn(value, this);
          if(validationResult === true){
            this.valid = (this.valid !== false)? validationResult : this.valid;
          } else{
            this.validationErrors.push(validationResult);
            this.valid = false;
          }

        }, this)

Happy to submit a pull request :)

justinramel avatar Nov 06 '16 13:11 justinramel