summaryrefslogtreecommitdiff
path: root/src/views/_ibs/Settings/Network/ModalHostname.vue
blob: 0567fe613013a1dcdb41544f27f088572be589e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<template>
  <b-modal
    id="modal-hostname"
    ref="modal"
    :title="$t('pageNetwork.modal.editHostnameTitle')"
    @hidden="resetForm"
  >
    <b-form id="hostname-settings" @submit.prevent="handleSubmit">
      <b-row>
        <b-col sm="6">
          <b-form-group
            :label="$t('pageNetwork.hostname')"
            label-for="hostname"
          >
            <b-form-input
              id="hostname"
              v-model="form.hostname"
              type="text"
              :state="getValidationState($v.form.hostname)"
              @input="$v.form.hostname.$touch()"
            />
            <b-form-invalid-feedback role="alert">
              <template v-if="!$v.form.hostname.required">
                {{ $t('global.form.fieldRequired') }}
              </template>
              <template v-if="!$v.form.hostname.validateHostname">
                {{ $t('global.form.lengthMustBeBetween', { min: 1, max: 64 }) }}
              </template>
            </b-form-invalid-feedback>
          </b-form-group>
        </b-col>
      </b-row>
    </b-form>
    <template #modal-footer="{ cancel }">
      <b-button variant="secondary" @click="cancel()">
        {{ $t('global.action.cancel') }}
      </b-button>
      <b-button
        form="hostname-settings"
        type="submit"
        variant="primary"
        @click="onOk"
      >
        {{ $t('global.action.add') }}
      </b-button>
    </template>
  </b-modal>
</template>

<script>
import VuelidateMixin from '@/components/_ibs/Mixins/VuelidateMixin.js';
import { required, helpers } from 'vuelidate/lib/validators';

const validateHostname = helpers.regex('validateHostname', /^\S{0,64}$/);

export default {
  mixins: [VuelidateMixin],
  props: {
    hostname: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      form: {
        hostname: '',
      },
    };
  },
  watch: {
    hostname() {
      this.form.hostname = this.hostname;
    },
  },
  validations() {
    return {
      form: {
        hostname: {
          required,
          validateHostname,
        },
      },
    };
  },
  methods: {
    handleSubmit() {
      this.$v.$touch();
      if (this.$v.$invalid) return;
      this.$emit('ok', { HostName: this.form.hostname });
      this.closeModal();
    },
    closeModal() {
      this.$nextTick(() => {
        this.$refs.modal.hide();
      });
    },
    resetForm() {
      this.form.hostname = this.hostname;
      this.$v.$reset();
      this.$emit('hidden');
    },
    onOk(bvModalEvt) {
      // prevent modal close
      bvModalEvt.preventDefault();
      this.handleSubmit();
    },
  },
};
</script>