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

Undefined is not a function (evaluating 'this.validate')

Open ridhofh opened this issue 8 years ago • 11 comments

I got error in _onPressButton, it refers to : _onPressButton() { // Call ValidationComponent validate method this.validate({ userEmail: {email: true, minlength:6, required: true}, userPassword: {minlength:6, required: true} }).then(Actions.index()); }

ridhofh avatar Jun 16 '17 07:06 ridhofh

Hi @ridhofh ! The validate methods doesn't return a promise. This is synchronous operation.

perscrew avatar Jun 16 '17 08:06 perscrew

so, what must i do, to run validate function from ValidationComponent?

ridhofh avatar Jun 16 '17 16:06 ridhofh

Sorry for the delay, you just have to extend ValidationComponent to your custom React Component and call : this.validate() in your click handler.

The validate methods will populate some validation informations in the root of your component.

perscrew avatar Jun 20 '17 08:06 perscrew

I'm having this issue too. Could you provide some sample code?

stephenfromrobin avatar Jul 27 '17 18:07 stephenfromrobin

see the documentation:

import ValidationComponent from 'react-native-form-validator';
 
export default class MyForm extends ValidationComponent {
  ...
}

thg303 avatar Sep 13 '17 13:09 thg303

getting the same error Undefined is not a function (evaluating 'this.validate')

ArsooCh avatar Jan 27 '18 07:01 ArsooCh

import React, { Component } from 'react'; import { View, Text, TextInput, TouchableHighlight } from 'react-native'; import ValidationComponent from '../../validator/index';

export default class App extends ValidationComponent {

constructor(props) { super(props); // this.state = {name : "My name", email: "[email protected]", number:"56", date: "2017-03-01"}; this.state = { name: "", email: "", number: "", date: "" }; }

_onPressButton = () => { this._validateForm(); }; onChangeTextName = (text) => { this.setState({ name: text }); this.validate({ name: { minlength: 3, maxlength: 7, required: true } });

} onChangeTextEmail = (text) => { this.setState({ email: text }); this.validate({ email: { email: true } });

} onChangeTextDigit = (text) => { this.setState({ number: text }); this.validate({ number: { numbers: true } });

} onChangeTextDate = (text) => { this.setState({ date: text }); this.validate({ date: { date: 'YYYY-MM-DD' } });

}

_validateForm() { this.validate({ name: { minlength: 3, maxlength: 7, required: true }, email: { email: true }, number: { numbers: true }, date: { date: 'YYYY-MM-DD' } }); }

render() {

return (
  <View style={{ paddingVertical: 30 }}>
    <Text>Name</Text>
    <TextInput ref="name" onChangeText={(name) => this.onChangeTextName(name)} value={this.state.name} />
    <Text>{(this.isFieldInError('name') && this.getErrorsInField('name'))?this.getErrorsInField('name'):''}</Text>

    <Text>Email</Text>
    <TextInput ref="email" onChangeText={(email) => this.onChangeTextEmail(email)} value={this.state.email} />
    <Text>{(this.isFieldInError('email') && this.getErrorsInField('email'))?this.getErrorsInField('email'):''}</Text>
    <Text>Number</Text>
    <TextInput ref="number" onChangeText={(number) => this.onChangeTextDigit(number)} value={this.state.number} />
    <Text>{(this.isFieldInError('number') && this.getErrorsInField('number'))?this.getErrorsInField('number'):''}</Text>
    <Text>DoB</Text>
    <TextInput ref="date" onChangeText={(date) => this.onChangeTextDate(date)} value={this.state.date} />
    <Text>{(this.isFieldInError('date') && this.getErrorsInField('date'))?this.getErrorsInField('date'):''}</Text>
   
    <TouchableHighlight onPress={this._onPressButton}>
      <Text>Submit</Text>
    </TouchableHighlight>

    
  </View>
);

}

}

ghost avatar May 21 '18 10:05 ghost

Use this _onPressButton = () => { this.validate({ name: { minlength: 3, maxlength: 7, required: true }, email: { email: true }, number: { numbers: true }, date: { date: 'YYYY-MM-DD' } }); }

Instead of _onPressButton(){ this.validate({ name: { minlength: 3, maxlength: 7, required: true }, email: { email: true }, number: { numbers: true }, date: { date: 'YYYY-MM-DD' } }); }

ghost avatar May 21 '18 10:05 ghost

after using arrow function too it gives same error

DheereshSinghKarki avatar Oct 14 '19 14:10 DheereshSinghKarki

@DheereshSinghKarki

You have to use 'bind' :

<TouchableHighlight onPress={this._onPressButton.bind(this)}> <Text>Submit</Text> </TouchableHighlight>

callyrajiv avatar Dec 02 '19 15:12 callyrajiv

thank you@DheereshSinghKarki its working for me

naveenjothi avatar Dec 26 '19 06:12 naveenjothi