Skip to content

Commit

Permalink
fix: Add devops name validator in devops list
Browse files Browse the repository at this point in the history
Signed-off-by: harrisonliu5 harrisonliu_5@163.com
  • Loading branch information
harrisonliu5 committed Jul 24, 2020
1 parent 63d0587 commit 62d13e5
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/components/Modals/DevOpsCreate/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export default class ProjectCreateModal extends React.Component {

this.store = props.store
this.workspaceStore = new WorkspaceStore()
this.nameRef = React.createRef()
this.formRef = React.createRef()
}

componentDidMount() {
Expand Down Expand Up @@ -114,6 +116,39 @@ export default class ProjectCreateModal extends React.Component {
</>
)

nameValidator = (rule, value, callback) => {
if (!value) {
return callback()
}

const { formTemplate, workspace } = this.props
const cluster = get(formTemplate, 'spec.placement.cluster')

this.store.checkName({ name: value, cluster, workspace }).then(resp => {
if (resp.exist) {
return callback({ message: t('Name exists'), field: rule.field })
}
callback()
})
}

handleClusterChange = () => {
if (this.nameRef && this.nameRef.current) {
const name = 'metadata.name'
if (
this.formRef &&
this.formRef.current &&
!isEmpty(this.formRef.current.state.errors)
) {
this.formRef.current.resetValidateResults(name)
}

this.nameRef.current.validate({
[name]: get(this.props.formTemplate, name),
})
}
}

render() {
const { visible, formTemplate, hideCluster, onOk, onCancel } = this.props
return (
Expand All @@ -126,6 +161,7 @@ export default class ProjectCreateModal extends React.Component {
visible={visible}
closable={false}
hideHeader
formRef={this.formRef}
>
<div className={styles.header}>
<img src="/assets/project-create.svg" alt="" />
Expand All @@ -140,12 +176,14 @@ export default class ProjectCreateModal extends React.Component {
<Form.Item
label={t('Name')}
desc={t('SERVICE_NAME_DESC')}
ref={this.nameRef}
rules={[
{ required: true, message: t('Please input name') },
{
pattern: PATTERN_SERVICE_NAME,
message: `${t('Invalid name')}, ${t('SERVICE_NAME_DESC')}`,
},
{ validator: this.nameValidator },
]}
>
<Input name="metadata.name" autoFocus={true} maxLength={63} />
Expand Down Expand Up @@ -173,6 +211,7 @@ export default class ProjectCreateModal extends React.Component {
valueRenderer={this.valueRenderer}
optionRenderer={this.optionRenderer}
defaultValue={this.defaultCluster}
onChange={this.handleClusterChange}
/>
</Form.Item>
</Column>
Expand Down

0 comments on commit 62d13e5

Please sign in to comment.