Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this.validate not working onPress button, only onChangeText input #10

Open
GustavoLucero opened this issue Jan 4, 2018 · 5 comments
Open

Comments

@GustavoLucero
Copy link

GustavoLucero commented Jan 4, 2018

I have this.validate({...}) method inside an onPress function, but this.validate is not fired when I press the button, this.validate is only fired when I write in the inputs. So this.getErrorMessages() is not returning nothing when I trigger onPress event, but it does return a response when I write in inputs.

¿Should it work like that?

NOTE: I'm working with nativebase.

  • Constructor
constructor() {
   super();
   this.state = { email: '' }
}
  • onPress function
login = () => {
    this.validate({
      email : {required: true, email: true}
    });
  }
  • Input component
   <Input style={styles.input}
      value={this.state.email}
      keyboardType='email-address'
      ref={(ref) => this.email = ref}
     onChangeText={(email) => this.setState({email})}
   />
  • Button and error messages Text
<Text>{this.getErrorMessages()}</Text>

<Button onPress={() => this.login()}>
   <Text>LOGIN</Text>
</Button>
@imamsutono
Copy link
Contributor

try to call setState with whatever state you have, such as error state and pass with any value

@ghost
Copy link

ghost commented May 21, 2018

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
Copy link

ghost commented May 21, 2018

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: "tibtib@gmail.com", 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>
);

}

}

@team-collaboration
Copy link

Did anyone found a solution for this? same issue happening in my app as well.
Please share the solution.

My issue is:
when clicking on the form submit button the error is not showing right after clicking on checkbox or selection from picker the error message shows up.

@PauZoffoli
Copy link

constructor(props) {

super(props  );

this.state = { cambio: "",name : "", email: "tibtib@gmail.com", number:"56", date: "2017-03-01"};

}

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

this.setState({
  cambio: ''
})

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants