admin管理员组文章数量:1025300
I am Brad, I am new to ReactJS currently i am working on ant-design form. I want to validate the input file validation for empty file input fields and display the message "please attach a file", I am not able to write code for this module please help me and i am very stuck
My Form Code
import React from 'react';
import styled from 'styled-ponents';
import 'antd/dist/antd.css';
import { Upload, message, Button, Icon } from 'antd';
const PhotoText = styled.div`
font-size: 16px;
font-weight: bold;
padding: 2rem 0 1rem 0;
display: block;
text-align: -webkit-auto;
`;
const ButtonWrapper = styled.div`
text-align: center;
`;
let file = { id: 'test' };
const { propss } = {
name: 'file',
action: '//jsonplaceholder.typicode/posts/',
headers: {
authorization: 'authorization-text',
},
onChange(info) {
file = info.file;
if (info.file.status !== 'uploading') {
}
if (info.file.status === 'done') {
message.success(`${info.file.name} file uploaded successfully`);
file = info.file;
} else if (info.file.status === 'error') {
message.error(`${info.file.name} file upload failed.`);
}
},
};
class RegisterStepTwo extends React.Component {
constructor(props) {
super(props);
this.saveData = this.saveData.bind(this);
}
saveData(e) {
this.props.addData(e, file);
}
render() {
return (
<div>
<PhotoText>Select a Photo to Upload</PhotoText>
<Upload {...propss}>
<Button>
<Icon type="upload" /> Click to Upload
</Button>
</Upload>
<br />
<PhotoText>Select a Video to Upload</PhotoText>
<Upload {...propss}>
<Button>
<Icon type="upload" /> Click to Upload
</Button>
</Upload>
</div>
);
}
}
export default RegisterStepTwo;
I am Brad, I am new to ReactJS currently i am working on ant-design form. I want to validate the input file validation for empty file input fields and display the message "please attach a file", I am not able to write code for this module please help me and i am very stuck
My Form Code
import React from 'react';
import styled from 'styled-ponents';
import 'antd/dist/antd.css';
import { Upload, message, Button, Icon } from 'antd';
const PhotoText = styled.div`
font-size: 16px;
font-weight: bold;
padding: 2rem 0 1rem 0;
display: block;
text-align: -webkit-auto;
`;
const ButtonWrapper = styled.div`
text-align: center;
`;
let file = { id: 'test' };
const { propss } = {
name: 'file',
action: '//jsonplaceholder.typicode./posts/',
headers: {
authorization: 'authorization-text',
},
onChange(info) {
file = info.file;
if (info.file.status !== 'uploading') {
}
if (info.file.status === 'done') {
message.success(`${info.file.name} file uploaded successfully`);
file = info.file;
} else if (info.file.status === 'error') {
message.error(`${info.file.name} file upload failed.`);
}
},
};
class RegisterStepTwo extends React.Component {
constructor(props) {
super(props);
this.saveData = this.saveData.bind(this);
}
saveData(e) {
this.props.addData(e, file);
}
render() {
return (
<div>
<PhotoText>Select a Photo to Upload</PhotoText>
<Upload {...propss}>
<Button>
<Icon type="upload" /> Click to Upload
</Button>
</Upload>
<br />
<PhotoText>Select a Video to Upload</PhotoText>
<Upload {...propss}>
<Button>
<Icon type="upload" /> Click to Upload
</Button>
</Upload>
</div>
);
}
}
export default RegisterStepTwo;
Share
Improve this question
asked Nov 24, 2018 at 13:47
BradBrad
1333 gold badges5 silver badges13 bronze badges
1 Answer
Reset to default 0Looks like you're looking for beforeUpload function (scroll down a bit).
Add that function to props and check the content of form inside it (validate it). According to the docs, returning false should stop the upload.
Edit: I really shouldn't write code for you. Anyway, something like this should work:
const { propss } = {
...code you've got so far,
beforeUpdate(file) {
if (file === '') {
message.error('your error message');
return false;
}
}
};
There is an an example - click on < >
symbol to see the code.
Before upload function takes file argument, adds it to the list, and returns false, thus preventing upload. User needs to press the button to do so.
You should instead check the variable (if it's not empty), show a message (message.error()) and return false.
I am Brad, I am new to ReactJS currently i am working on ant-design form. I want to validate the input file validation for empty file input fields and display the message "please attach a file", I am not able to write code for this module please help me and i am very stuck
My Form Code
import React from 'react';
import styled from 'styled-ponents';
import 'antd/dist/antd.css';
import { Upload, message, Button, Icon } from 'antd';
const PhotoText = styled.div`
font-size: 16px;
font-weight: bold;
padding: 2rem 0 1rem 0;
display: block;
text-align: -webkit-auto;
`;
const ButtonWrapper = styled.div`
text-align: center;
`;
let file = { id: 'test' };
const { propss } = {
name: 'file',
action: '//jsonplaceholder.typicode/posts/',
headers: {
authorization: 'authorization-text',
},
onChange(info) {
file = info.file;
if (info.file.status !== 'uploading') {
}
if (info.file.status === 'done') {
message.success(`${info.file.name} file uploaded successfully`);
file = info.file;
} else if (info.file.status === 'error') {
message.error(`${info.file.name} file upload failed.`);
}
},
};
class RegisterStepTwo extends React.Component {
constructor(props) {
super(props);
this.saveData = this.saveData.bind(this);
}
saveData(e) {
this.props.addData(e, file);
}
render() {
return (
<div>
<PhotoText>Select a Photo to Upload</PhotoText>
<Upload {...propss}>
<Button>
<Icon type="upload" /> Click to Upload
</Button>
</Upload>
<br />
<PhotoText>Select a Video to Upload</PhotoText>
<Upload {...propss}>
<Button>
<Icon type="upload" /> Click to Upload
</Button>
</Upload>
</div>
);
}
}
export default RegisterStepTwo;
I am Brad, I am new to ReactJS currently i am working on ant-design form. I want to validate the input file validation for empty file input fields and display the message "please attach a file", I am not able to write code for this module please help me and i am very stuck
My Form Code
import React from 'react';
import styled from 'styled-ponents';
import 'antd/dist/antd.css';
import { Upload, message, Button, Icon } from 'antd';
const PhotoText = styled.div`
font-size: 16px;
font-weight: bold;
padding: 2rem 0 1rem 0;
display: block;
text-align: -webkit-auto;
`;
const ButtonWrapper = styled.div`
text-align: center;
`;
let file = { id: 'test' };
const { propss } = {
name: 'file',
action: '//jsonplaceholder.typicode./posts/',
headers: {
authorization: 'authorization-text',
},
onChange(info) {
file = info.file;
if (info.file.status !== 'uploading') {
}
if (info.file.status === 'done') {
message.success(`${info.file.name} file uploaded successfully`);
file = info.file;
} else if (info.file.status === 'error') {
message.error(`${info.file.name} file upload failed.`);
}
},
};
class RegisterStepTwo extends React.Component {
constructor(props) {
super(props);
this.saveData = this.saveData.bind(this);
}
saveData(e) {
this.props.addData(e, file);
}
render() {
return (
<div>
<PhotoText>Select a Photo to Upload</PhotoText>
<Upload {...propss}>
<Button>
<Icon type="upload" /> Click to Upload
</Button>
</Upload>
<br />
<PhotoText>Select a Video to Upload</PhotoText>
<Upload {...propss}>
<Button>
<Icon type="upload" /> Click to Upload
</Button>
</Upload>
</div>
);
}
}
export default RegisterStepTwo;
Share
Improve this question
asked Nov 24, 2018 at 13:47
BradBrad
1333 gold badges5 silver badges13 bronze badges
1 Answer
Reset to default 0Looks like you're looking for beforeUpload function (scroll down a bit).
Add that function to props and check the content of form inside it (validate it). According to the docs, returning false should stop the upload.
Edit: I really shouldn't write code for you. Anyway, something like this should work:
const { propss } = {
...code you've got so far,
beforeUpdate(file) {
if (file === '') {
message.error('your error message');
return false;
}
}
};
There is an an example - click on < >
symbol to see the code.
Before upload function takes file argument, adds it to the list, and returns false, thus preventing upload. User needs to press the button to do so.
You should instead check the variable (if it's not empty), show a message (message.error()) and return false.
本文标签: javascriptValidate File Upload in ReactJsStack Overflow
版权声明:本文标题:javascript - Validate File Upload in ReactJs - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745611572a2159031.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论