[英]Uncaught TypeError: Cannot read property 'form' of undefined
Am pretty new to JavaScript and React but have been playing around with a antd a UI Component library creating a form with validation but ran into some troubles. 对JavaScript和React来说是一个很新的事物,但是一直在使用一个古老的UI组件库来创建带有验证的表单,但遇到了一些麻烦。
Following their API/Documentation here I created a Form which should contain the props when using From.create however I get a Uncaught TypeError: Cannot read property 'form' of undefined1
when doing so in the checkConfirm function const form = this.props.form;
按照这里的API /文档,我创建了一个Form,使用From.create时应包含道具,但是却收到Uncaught TypeError: Cannot read property 'form' of undefined1
在checkConfirm函数const form = this.props.form;
这样做时, Uncaught TypeError: Cannot read property 'form' of undefined1
const form = this.props.form;
line. 线。
class CustomizedForm extends React.Component {
constructor(...args) {
super(...args);
}
handleSubmit() {
e.preventDefault();
this.props.form.validateFields((err, values) => {
if (!err) {
console.log('Received values of form: ', values);
}
})
}
checkConfirm(rule, value, callback) {
console.log(value.length);
const form = this.props.form;
if (value.length == 11) {
form.validateFields(['confirm'], { force: true });
};
callback();
}
render() {
const { getFieldDecorator } = this.props.form;
const formItemLayout = {
labelCol: { span: 6 },
wrapperCol: { span: 14 }
};
return (
<div>
<Form inline style={{ marginTop: 10, marginBottom: 10 }} onSubmit={this.handleSubmit}>
<FormItem
{...formItemLayout}
hasFeedback
>
{getFieldDecorator('password', {
rules: [{
required: true, message: 'Please input your password!',
}, {
validator: this.checkConfirm,
}],
})(
<Input placeholder="password" />
)}
</FormItem>
<FormItem hasFeedback>
</FormItem>
<FormItem >
<Input style={{ width: '200px' }} size="default" placeholder="Shelf Place"></Input>
</FormItem>
<FormItem>
<ProcessSelect />
</FormItem>
<FormItem>
<ZoneSelect />
</FormItem>
<FormItem>
<ZalosRangePicker />
</FormItem>
<FormItem>
<ButtonGroup size="default">
<Button size="default" icon="search" />
<Button size="default" icon="close" />
</ButtonGroup>
</FormItem>
</Form>
</div>
)
}
}
CustomizedForm = Form.create({})(CustomizedForm);
I found some mistakes here, 我在这里发现了一些错误,
. 。 Reason of error, 错误原因,
You need to bind this to function like, 您需要将此绑定到类似的功能,
constructor(...args) {
super(...args);
this.checkConfirm = this.checkConfirm.bind(this)
}
Function Defination 功能定义
checkConfirm(rule, value, callback) {
console.log(value.length);
const form = this.props.form;
if (value.length == 11) {
form.validateFields(['confirm'], { force: true });
};
callback();
}
You are not passing any arrangements to function. 您没有通过任何安排以发挥作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.