diff --git a/src/components/Modals/Network/Policies/IpBlock.jsx b/src/components/Modals/Network/Policies/IpBlock.jsx index 0a7e9f5288a..13188a78995 100644 --- a/src/components/Modals/Network/Policies/IpBlock.jsx +++ b/src/components/Modals/Network/Policies/IpBlock.jsx @@ -84,38 +84,49 @@ export default class NetworkPoliciesIpBlockModal extends React.Component { this.setState(portRules) } - validFormData = () => { - const { specType, cidr, portRules } = this.state + validCIDR = evt => { + const cidr = { ...this.state.cidr } const { ip, mask } = cidr - cidr.ip.valid = PATTERN_IP.test(ip.value) - cidr.mask.valid = PATTERN_IP_MASK.test(mask.value) - let validated = cidr.ip.valid && cidr.mask.valid - let choSpecType = specType - const isValidPort = function(v) { - return v !== '' && /^(?![0])(\d{0,5})$/.test(v) && v < 65536 - } - if (specType === '') { - validated = false - choSpecType = false + + if (!evt || ip.value !== '') { + ip.valid = PATTERN_IP.test(ip.value) } - if (validated) { - portRules.forEach(rule => { - rule.port.valid = - isEmpty(rule.port.value) || isValidPort(rule.port.value) - if (!rule.port.valid) { - validated = false - } - }) + if (!evt || mask.value !== '') { + mask.valid = PATTERN_IP_MASK.test(mask.value) } - this.setState({ - cidr, - portRules, - specType: choSpecType, - }) + this.setState({ cidr }) + return !!ip.valid && !!mask.valid + } + + isValidPort = function(v) { + return v !== '' && /^(?![0])(\d{0,5})$/.test(v) && v < 65536 + } + validPort = () => { + const portRules = this.state.portRules.slice() + let validated = true + portRules.forEach(rule => { + rule.port.valid = + isEmpty(rule.port.value) || this.isValidPort(rule.port.value) + if (!rule.port.valid) { + validated = false + } + }) + this.setState({ portRules }) return validated } + validSpecType = () => { + const { specType } = this.state + if (specType === '') { + this.setState({ specType: false }) + } + return specType !== '' + } + + validFormData = () => + this.validSpecType() && this.validCIDR() && this.validPort() + handleSave = () => { const { specType, cidr, portRules } = this.state if (this.validFormData()) { @@ -143,6 +154,7 @@ export default class NetworkPoliciesIpBlockModal extends React.Component { render() { const { ...rest } = this.props const { specType, protocols, portRules, cidr } = this.state + let portInvalid = false return ( - -
- this.setCIDR({ ip: { value: v } })} - /> - / - this.setCIDR({ mask: { value: v } })} - /> -
+ + <> +
+ this.setCIDR({ ip: { value: v } })} + onBlur={this.validCIDR} + /> + / + this.setCIDR({ mask: { value: v } })} + onBlur={this.validCIDR} + /> +
+
+ {cidr.ip.valid === false || cidr.mask.valid === false + ? t('NETWORK_POLICY_MODAL_CIDRERR') + : t('NETWORK_POLICY_D_DESC2')} +
+
{`${t('Port')}:`}
- {portRules.map(({ port, protocol }, idx) => ( -
-
- this.modifyRule(idx, { port: { value: v } })} + {portRules.map(({ port, protocol }, idx) => { + if (port.valid === false) { + portInvalid = true + } + return ( +
+
+ this.modifyRule(idx, { port: { value: v } })} + /> + +
+
+ { + this.delPortRule(idx) + }} + /> +
-
- ))} + ) + })}
+ {portInvalid && ( +
+ {t('NETWORK_POLICY_MODAL_PORTERR')} +
+ )} ) } diff --git a/src/components/Modals/Network/Policies/index.scss b/src/components/Modals/Network/Policies/index.scss index 69397bd631c..54c3b039969 100644 --- a/src/components/Modals/Network/Policies/index.scss +++ b/src/components/Modals/Network/Policies/index.scss @@ -40,6 +40,10 @@ & > :last-child { width: 80px; } + &_desc { + margin-top: 4px; + color: $dark-color01; + } } .rulerow { display: flex; @@ -64,6 +68,7 @@ border: 1px solid $danger !important; } .errColor { + margin-top: 4px; color: $danger; } .addBtn { diff --git a/src/locales/en/network.js b/src/locales/en/network.js index 768f910b636..0fc16440012 100644 --- a/src/locales/en/network.js +++ b/src/locales/en/network.js @@ -66,4 +66,6 @@ export default { 'The network policy is configured to allow network isolation within the same cluster, that is, the ability to build a firewall between certain instances (pods).', CIDR_DESC: 'Based on the traffic direction', NETWORK_POLICY_MODAL_DIRECT: 'Please select the rule direction', + NETWORK_POLICY_MODAL_CIDRERR: 'Please fill in CIDR information correctly', + NETWORK_POLICY_MODAL_PORTERR: 'Please fill in the port correctly', } diff --git a/src/locales/zh/network.js b/src/locales/zh/network.js index 305072a39a8..30500202b47 100644 --- a/src/locales/zh/network.js +++ b/src/locales/zh/network.js @@ -64,4 +64,6 @@ export default { CIDR_DESC: '将根据流量的方向', NETWORK_POLICY_MODAL_DIRECT: '请选择规则方向', + NETWORK_POLICY_MODAL_CIDRERR: '请正确填写CIDR信息', + NETWORK_POLICY_MODAL_PORTERR: '请正确填写端口', }