实现效果如下:

当点击按钮的时候 对一个FormItem里的多个input/或者是input和select进行校验  同时通过Rol/Col实现布局

Rselect/input组件封装的组件如下field.js:

import React from 'react';
import { Select, Input, Row, Col } from 'antd';
import _ from 'underscore';
import './index.less';
const Option = Select.Option;
const uiPrefix = "certificate-field"
class CertificateField extends React.Component { constructor(props) {
super(props);
this.state = {
yearlists: [],//时间下拉框列表
cities: [],//城市下拉框列表
years: undefined,
cityId: undefined,
warrantNumber: undefined,
};
}
static defaultProps = {
cities: [],
yearlists: [],
hasYear: true,
hasCity: true,
readonly: true,
hasWarrantNum: true,
years: (new Date()).getFullYear(),
cityId: 1,
} getStateFromProps(props) {
let { value = {} } = props;
return { ...this.parseDataFromInput(value) };
}
parseDataFromInput(value) {
let { years, cityId, warrantNumber } = value;
years = years ? ('' + years) : undefined;
cityId = cityId ? ('' + cityId) : undefined;
warrantNumber = warrantNumber ? ('' + warrantNumber) : undefined return { years, cityId, warrantNumber };
} onBlur = () => {
let { years, cityId, warrantNumber } = this.state;
let yearlistsName = this.getTextFromValue("yearlist");
let citieName = this.getTextFromValue("citie");
const { onBlur } = this.props;
if (onBlur) {
const {
hasYear,
hasCity,
hasWarrantNum,
} = this.props;
let unFull = true;
if (unFull && hasYear == true && !years) {
unFull = false;
}
if (unFull && hasCity == true && !cityId) {
unFull = false;
}
if (unFull && hasWarrantNum == true && !warrantNumber) {
unFull = false;
}
if (unFull) {
onBlur(
JSON.stringify({
years,
cityId,
warrantNumber,
// yearlistsName,
// citieName,
})
)
} else {
onBlur(undefined)
}
}
} getTextFromValue = (type) => {
let resource;
let value;
let text = ''; if (type === 'city' || type === 'years') {
resource = this.state.cities;
value = this.state.cityId;
}
else {
resource = this.state[type + 's'];
value = this.state[type + 'Id'];
} let item = null;
if (resource !== undefined && resource.length > 0) {
item = resource.find(({ id }) => id === +value);
} text = text || '';
return item ? item.text : text;
} fireChange = () => {
let { years, cityId, warrantNumber } = this.state;
const { onChange } = this.props;
if (onChange) {
const {
hasYear,
hasCity,
hasWarrantNum,
} = this.props;
let unFull = true;
if (unFull && hasYear == true && !years) {
unFull = false;
}
if (unFull && hasCity == true && !cityId) {
unFull = false;
}
if (unFull && hasWarrantNum == true && !warrantNumber) {
unFull = false;
}
if (unFull) {
onChange(
JSON.stringify({
years,
cityId,
warrantNumber,
}))
} else {
onChange(undefined)
}
}
}
getColSpan = () => {
const result = {
select: 4,
input: 2
}
const {
hasYear,
hasCity,
} = this.props;
if (!hasYear) {
result.input += 4
}
if (!hasCity) {
result.input += 4
}
return result; } componentWillReceiveProps(nextProps) { // 父组件重传props时就会调用这个方法
this.getStateFromProps(nextProps);
const oldProps = this.props;
let { years, cityId, warrantNumber } = nextProps;
if (years != oldProps.years &&
cityId != oldProps.cityId &&
warrantNumber != oldProps.warrantNumber) {
this.setState({
years,
cityId,
warrantNumber
}, () => {
this.initData(nextProps)
this.fireChange(nextProps)
})
}
} componentDidMount() {
let { years, cityId, warrantNumber } = this.props;
this.setState({
years,
cityId,
warrantNumber
}, () => {
this.initData(this.props)
// this.fireChange(this.props)
})
} initData(props) {
let { initSelectData = {} } = this.props;//获取初始化的值(时间/城市 下拉框的值)
let { yearLists = [], cities = [] } = initSelectData;
let currentYear = (new Date()).getFullYear();
if (yearLists.length == 0) {
const startYear = 2016;
for (let i = 0; i <= currentYear - startYear; i++) {
yearLists.push({ text: startYear + i, id: startYear + i })
}
}
if (cities.length == 0) {
cities = [{ text: "鹤岗市", id: 1 }]
}
this.setState({
yearLists,
cities,
})
} onyearsChange = (years, noEvent) => {
years = '' + years;
this.setState({ years },this.fireChange);
} onCityChange = (cityId, noEvent) => {
cityId = '' + cityId;
this.setState({ cityId }, this.fireChange);
} onwarrantNumberChange = (e) => {
const warrantNumber = e.target.value;
this.setState({ warrantNumber }, this.fireChange);
} selectOptions = (constantsTypes, fieldOp) => {
fieldOp = fieldOp || { key: 'id', value: 'text' };
// {key: 'xxxx', value: 'xxxx'}
if (!_.isArray(constantsTypes)) {
const options = [];
_.each(
constantsTypes,
(value, key) => {
let trueValue;
let disabled = false;
if (_.isObject(value)) {
trueValue = value.value;
disabled = value.disabled;
}
else {
trueValue = value;
}
return options.push(<Option key={key} value={key} disabled={disabled}>{trueValue}</Option>);
}
);
return options;
}
// [{key: 'xxxx', value: 'xxxx'}]
// [1, 3, 4]
return constantsTypes.reduce(
(options, value, key) => {
let realKey;
let display;
if (_.isObject(value)) {
realKey = `${value[fieldOp.key]}`;
display = value[fieldOp.value];
}
else {
realKey = `${key}`;
display = value;
} options.push(<Option key={realKey} value={realKey}>{display}</Option>);
return options;
},
[]
);
} render() {
let {
years,
cityId,
yearLists = [],
cities = [],
warrantNumber
} = this.state;
let {
hasYear,
hasCity,
readonly = true,
hasWarrantNum,
detailPlaceholder,
} = this.props;
if (readonly) {
cityId = this.getTextFromValue('city')
}
const colSpan = this.getColSpan();
return (
<Row className={`${uiPrefix}`}>
{hasYear &&
<Col
span={colSpan.select}
>
<Row>
<Col span={22} style={{ paddingRigth: "0" }}>
<Select
style={{ height: 32 }}
value={years}
placeholder="请选择"
onChange={this.onyearsChange}
onBlur={this.onBlur}
>
{this.selectOptions(yearLists)}
</Select>
</Col>
</Row>
</Col>
}
{hasCity &&
<Col span={colSpan.select}>
<Row>
<Col span={22}>
<Select
disabled={readonly}
value={cityId}
placeholder="请选择"
onChange={this.onCityChange}
onBlur={this.onBlur}
>
{this.selectOptions(cities)}
</Select>
</Col>
</Row>
</Col>
}
{
hasWarrantNum &&
<Col span={colSpan.select}>
{/* <div className={`${uiPrefix}-select-item`}> */}
{/* 不动产权第 */}
<Input
maxLength={16}
// style={{ width: 120 }}
value={warrantNumber}
placeholder={detailPlaceholder}
onChange={this.onwarrantNumberChange}
onBlur={this.onBlur}
/>
{/* 号 */}
{/* </div> */}
</Col>
} </Row>
);
}
}
export default CertificateField;

需要调用入口页面index.js:

import React from "react";
import { Form, Input, Button, Radio, message, Modal } from "antd";
import { CertificateField } from "components";
class Schhouse extends React.Component {
onSearchHandle = () => {
//注意validateFields里的fields,要和getFieldDecorator中的fields保持一致 否则拿不到值
this.props.form.validateFields(["fields"], (err, fieldsValue) => {
console.log(fieldsValue, "fieldsValue")
if (err) {
return;
}
})
} render() {
const { getFieldDecorator } = this.props.form;
return (
<>
<Form>
<Form.Item label="标签">
{getFieldDecorator('fields', {
rules: [{ required: true, message: '请输入标签对应内容' }],
})(
<CertificateField />
)}
</Form.Item>
<Form.Item>
<Button type="primary" htmlType="submit" onClick={this.onSearchHandle.bind(this)}>
查询并办理
</Button>
</Form.Item>
</Form>
</>
)
}
} export default Form.create()(Schhouse);

非常完美!!!

在一个formitem中多input的验证方法-antd的验证的更多相关文章

  1. 多个Fragment在一个activity中通过按钮的展示方法

    fragment使用方法 1. 创建主Mainactivity extends AppCompatActivity 2. Oncreate & setContentView 3. 完成XML的 ...

  2. jquery validate 自定义验证方法

    query validate有很多验证规则,但是更多的时候,需要根据特定的情况进行自定义验证规则. 这里就来聊一聊jquery validate的自定义验证. jquery validate有一个方法 ...

  3. jquery validate 自定义验证方法 日期验证

    jquery validate有很多验证规则,但是更多的时候,需要根据特定的情况进行自定义验证规则. 这里就来聊一聊jquery validate的自定义验证. jquery validate有一个方 ...

  4. Android中的onActivityResult和setResult方法的使用

    如果你想在Activity中得到新打开Activity关闭后返回的数据,你需要使用系统提供的startActivityForResult(Intent intent,int requestCode)方 ...

  5. jQuery Validate 表单验证插件----自定义一个验证方法

    一.下载依赖包 网盘下载:https://yunpan.cn/cryvgGGAQ3DSW  访问密码 f224 二.引入依赖包 <script src="../../scripts/j ...

  6. ASP.NET 表单验证方法与客户端(浏览器)服务器交互机制的故事

    想到这个问题完全是一个意外吧,是在寻找另外一个问题答案的过程中,才对验证方法与浏览器服务器交互机制的关系有了清晰的认识. 先说下验证方法,验证方法分为前台验证和后台验证. 前台验证就是类似jQuery ...

  7. vuex中filter的使用 && 快速判断一个数是否在一个数组中

    vue中filter的使用 computed: mapState({ items: state => state.items.filter(function (value, index, arr ...

  8. validation验证器指定action中某些方法不需要验证

    今天写代码时,遇到个问题,在一个输入数据的页面有一个按钮,单击会发出请求从数据库中取数据,在这里出现问题,单击该按钮,配置的validation起作用,该请求没有到达后台的action 点击按钮选择作 ...

  9. ASP.NET开发中主要的字符验证方法-JS验证、正则表达式、验证控件、后台验证

    ASP.NET开发中主要的字符验证方法-JS验证.正则表达式.验证控件.后台验证 2012年03月19日 星期一 下午 8:53 在ASP.NET开发中主要的验证方法收藏 <1>使用JS验 ...

随机推荐

  1. JMeter-显示调试日志log

    JMeter-调试日志记录 参考文档:https://jmeter.apache.org/usermanual/hints_and_tips.html 大多数测试元素包括调试日志记录. 如果从GUI运 ...

  2. 史上最深入浅出的IT术语解读

    假设你是个妹子,你有一位男朋友,与此同时你和另外一位男生暧昧不清,比朋友好,又不是恋人.你随时可以甩了现任男友,另外一位马上就能补上.这是冷备份. 假设你是个妹子,同时和两位男性在交往,两位都是你男朋 ...

  3. 一次H5毛玻璃效果有感

    印象中H5实现毛玻璃效果是挺好实现的,主要的代码就是css的filter:blur. 之前也用过几次,给背景图加高斯模糊啊,给一个div加高斯模糊啊.只要给需要添加高斯模糊的元素直接添加filter属 ...

  4. IO流学习之字符流(三)

    IO流之字符流缓冲区: 概念: 流中的缓冲区:是先把程序需要操作的数据保存在内存中,然后我们的程序读写数据的时候,不直接和持久设备之间交互,而改成和内存中的数据进行交互. 缓冲区:它就是临时存储数据, ...

  5. PAT (Basic Level) Practice (中文)1064 朋友数 (20 分) (set)

    如果两个整数各位数字的和是一样的,则被称为是“朋友数”,而那个公共的和就是它们的“朋友证号”.例如 123 和 51 就是朋友数,因为 1+2+3 = 5+1 = 6,而 6 就是它们的朋友证号.给定 ...

  6. PAT (Advanced Level) Practice 1036 Boys vs Girls (25 分)

    This time you are asked to tell the difference between the lowest grade of all the male students and ...

  7. PTA 1001 A+B Format

    问题描述: Calculate a+b and output the sum in standard format -- that is, the digits must be separated i ...

  8. Wannafly Camp 2020 Day 3I N门问题 - 概率论,扩展中国剩余定理

    有一个猜奖者和一个主持人,一共有 \(n\) 扇门,只有一扇门后面有奖,主持人事先知道哪扇门后有奖,而猜奖者不知道.每一轮,猜奖者选择它认为的有奖概率最大(如果有多个最大,随机选一个)的一扇门,主持人 ...

  9. threadpool 实例介绍第二篇

  10. include=FALSE的作用

    每次都会加载很多的包,会显示很多没用的信息,特别是那个spdep. 例如: {r include=FALSE} library(plm) library(tseries) library(zoo) l ...