我在Ignite 2.0中使用redux-form。我正在尝试验证值,但是每当我尝试查找字符串的长度时,它就会给我字符串未定义
这是我的组成部分
import React, { Component } from "react";
import { Field, reduxForm } from "redux-form";
import {
Container,
Content,
Text,
Button,
View,
Item,
Input
} from "native-base";
import { StackNavigator } from "react-navigation";
const validate = value => {
const error = {};
error.name = "";
error.password = "";
const { name, password } = value;
if (name === "undefined" || name === "") {
error.name = "Empty";
}
if (password === "undefined" || password.length < 8) {
error.password = "too short";
}
console.log("error", error);
};
class SignupForm extends Component {
renderInput({
input,
label,
placeholder,
type,
meta: { touched, error, warning }
}) {
var hasError = false;
if (error !== undefined) {
hasError = true;
}
return (
<Item error={hasError}>
<Input {...input} placeholder={placeholder} type={type} />
{hasError ? <Text>{error}</Text> : <Text />}
</Item>
);
}
render() {
const { handleSubmit } = this.props;
return (
<Container>
<Content>
<Field
name="name"
component={this.renderInput}
label="Name"
placeholder="Name"
type="text"
/>
<Field
name="password"
component={this.renderInput}
placeholder="Password"
label="Password"
type="password"
/>
<Button type="submit" onPress={handleSubmit}>
<Text>Submit</Text>
</Button>
</Content>
</Container>
);
}
}
export default reduxForm({
form: "SignupForm",
validate
})(SignupForm);
password.length 不断抛出错误 TypeError:password未定义 谁能告诉我我在做什么错或者我错过了什么。
谢谢
答案 0 :(得分:1)
series: [{
type: "sunburst",
data: this.data,
rootId:'Southern Asia'}]
在此,将if (password === "undefined" || password.length < 8) {
error.password = "too short";
}
替换为双引号"undefined"
,因为undefined
是原始值。
答案 1 :(得分:0)
以这种方式检查undefined
。
if (!password && password.length < 8)
答案 2 :(得分:0)
if (password === "undefined" || password.length < 8) {
error.password = "too short";
}
在这里您应该进行常规检查,只需检查!password || password.length < 8
这样,您也可以处理null
和undefined
的情况。