Przeglądaj źródła

[update]

世帯名、世帯主、住所、電話番号の値を検証する処理
poohr 4 tygodni temu
rodzic
commit
0821abd41e

+ 40
- 30
src/app/pages/danka-edit/danka-edit.html Wyświetl plik

21
             <div class="form-list">
21
             <div class="form-list">
22
               <div class="form-row">
22
               <div class="form-row">
23
                 <label for="householdName">世帯名</label>
23
                 <label for="householdName">世帯名</label>
24
-                <input
25
-                  id="householdName"
26
-                  type="text"
27
-                  formControlName="householdName"
28
-                />
24
+                <div class="form-field">
25
+                  <input
26
+                    id="householdName"
27
+                    type="text"
28
+                    formControlName="householdName"
29
+                  />
30
+                  @if (dankaForm.get('householdName')?.invalid && dankaForm.get('householdName')?.touched) {
31
+                    <p class="error-message">世帯名を入力してください。</p>
32
+                  }
33
+                </div>
29
               </div>
34
               </div>
30
 
35
 
31
               <div class="form-row">
36
               <div class="form-row">
32
                 <label for="householder">世帯主</label>
37
                 <label for="householder">世帯主</label>
33
-                <input
34
-                  id="householder"
35
-                  type="text"
36
-                  formControlName="householder"
37
-                />
38
+                <div class="form-field">
39
+                  <input
40
+                    id="householder"
41
+                    type="text"
42
+                    formControlName="householder"
43
+                  />
44
+                  @if (dankaForm.get('householder')?.invalid && dankaForm.get('householder')?.touched) {
45
+                    <p class="error-message">世帯主を入力してください。</p>
46
+                  }
47
+                </div>
38
               </div>
48
               </div>
39
 
49
 
40
               <div class="form-row">
50
               <div class="form-row">
41
                 <label for="postalCode">郵便番号</label>
51
                 <label for="postalCode">郵便番号</label>
42
-                <input
43
-                  id="postalCode"
44
-                  type="text"
45
-                  formControlName="postalCode"
46
-                />
52
+                <div class="form-field">
53
+                  <input
54
+                    id="postalCode"
55
+                    type="text"
56
+                    formControlName="postalCode"
57
+                  />
58
+                  @if (dankaForm.get('postalCode')?.invalid && dankaForm.get('postalCode')?.touched) {
59
+                    <p class="error-message">郵便番号を入力してください。</p>
60
+                  }
61
+                </div>
47
               </div>
62
               </div>
48
 
63
 
49
               <div class="form-row">
64
               <div class="form-row">
73
               @for (phone of phones.controls; track $index; let i = $index) {
88
               @for (phone of phones.controls; track $index; let i = $index) {
74
                 <div class="phone-table-row" [formGroupName]="i">
89
                 <div class="phone-table-row" [formGroupName]="i">
75
                   <div>
90
                   <div>
76
-                    <input
77
-                      type="text"
78
-                      formControlName="tel"
79
-                      placeholder="電話番号"
80
-                    />
91
+                    <input type="text" formControlName="tel" placeholder="電話番号"/>
92
+                    @if (phone.get('tel')?.invalid && phone.get('tel')?.touched) {
93
+                      <p class="phone-error-message">
94
+                        電話番号は数字とハイフンで入力してください。
95
+                      </p>
96
+                    }
81
                   </div>
97
                   </div>
82
 
98
 
83
                   <div>
99
                   <div>
84
-                    <input
85
-                      type="text"
86
-                      formControlName="note"
87
-                      placeholder="備考"
88
-                    />
100
+                    <input type="text" formControlName="note" placeholder="備考"/>
89
                   </div>
101
                   </div>
90
 
102
 
91
                   <div class="phone-row-action">
103
                   <div class="phone-row-action">
92
-                    <button
93
-                      type="button"
94
-                      class="remove-phone-button"
95
-                      (click)="removePhone(i)"
96
-                    >
104
+                    <button type="button" class="remove-phone-button" [disabled]="phones.length === 1" (click)="removePhone(i)">
97
                       削除
105
                       削除
98
                     </button>
106
                     </button>
99
                   </div>
107
                   </div>
100
                 </div>
108
                 </div>
101
               }
109
               }
110
+
102
             </div>
111
             </div>
103
 
112
 
104
             <div class="phone-action">
113
             <div class="phone-action">
125
           <button
134
           <button
126
             type="button"
135
             type="button"
127
             class="save-button"
136
             class="save-button"
137
+            [disabled]="dankaForm.invalid"
128
             (click)="saveDanka()"
138
             (click)="saveDanka()"
129
           >
139
           >
130
             保存
140
             保存

+ 42
- 0
src/app/pages/danka-edit/danka-edit.scss Wyświetl plik

95
   margin-top: 14px;
95
   margin-top: 14px;
96
 }
96
 }
97
 
97
 
98
+.form-field {
99
+  width: 100%;
100
+  display: flex;
101
+  flex-direction: column;
102
+}
103
+
98
 .form-row {
104
 .form-row {
99
   display: grid;
105
   display: grid;
100
   grid-template-columns: 120px 1fr;
106
   grid-template-columns: 120px 1fr;
311
   font-size: 14px;
317
   font-size: 14px;
312
   box-sizing: border-box;
318
   box-sizing: border-box;
313
 }
319
 }
320
+
321
+.error-message {
322
+  margin: 6px 0 0;
323
+  color: #b33a2f;
324
+  font-size: 14px;
325
+  font-weight: 700;
326
+  line-height: 1.4;
327
+  white-space: normal;
328
+}
329
+
330
+.save-button:disabled {
331
+  background: #c8b8a5;
332
+  border-color: #c8b8a5;
333
+  color: #ffffff;
334
+  cursor: not-allowed;
335
+  opacity: 0.7;
336
+}
337
+
338
+.phone-field {
339
+  width: 100%;
340
+  display: flex;
341
+  flex-direction: column;
342
+}
343
+
344
+.phone-error-message {
345
+  margin: 4px 0 0;
346
+  color: #b33a2f;
347
+  font-size: 12px;
348
+  font-weight: 700;
349
+  line-height: 1.4;
350
+}
351
+
352
+.remove-phone-button:disabled {
353
+  opacity: 0.5;
354
+  cursor: not-allowed;
355
+}

+ 23
- 21
src/app/pages/danka-edit/danka-edit.ts Wyświetl plik

23
   danka: Danka | undefined;
23
   danka: Danka | undefined;
24
 
24
 
25
   dankaForm = new FormGroup({
25
   dankaForm = new FormGroup({
26
-    householdName: new FormControl(''),
27
-    householder: new FormControl(''),
28
-    postalCode: new FormControl(''),
26
+    householdName: new FormControl('', [Validators.required]),
27
+    householder: new FormControl('', [Validators.required]),
28
+    postalCode: new FormControl('', Validators.pattern(/^\d{3}-\d{4}$/)),
29
     address: new FormControl(''),
29
     address: new FormControl(''),
30
-    phones: new FormArray([
31
-      this.createPhoneForm('', ''),
32
-      this.createPhoneForm('', ''),
33
-      this.createPhoneForm('', ''),
34
-    ]),
30
+    phones: new FormArray([this.createPhoneForm('', '')]),
35
   });
31
   });
36
 
32
 
37
   constructor(
33
   constructor(
65
     return this.dankaForm.get('phones') as FormArray;
61
     return this.dankaForm.get('phones') as FormArray;
66
   }
62
   }
67
 
63
 
68
-  createPhoneForm(string = '', note = '') {
64
+  createPhoneForm(tel = '', note = '') {
69
     return new FormGroup({
65
     return new FormGroup({
70
-      tel: new FormControl(string),
66
+      tel: new FormControl(tel, [Validators.pattern(/^[0-9-]+$/)]),
71
       note: new FormControl(note),
67
       note: new FormControl(note),
72
     });
68
     });
73
   }
69
   }
77
   }
73
   }
78
 
74
 
79
   removePhone(index: number) {
75
   removePhone(index: number) {
80
-    this.phones.removeAt(index);
76
+    if(this.phones.length > 1) {
77
+      this.phones.removeAt(index);
78
+    }
81
   }
79
   }
82
 
80
 
83
   //保存の処理
81
   //保存の処理
84
   saveDanka() {
82
   saveDanka() {
83
+    if (this.dankaForm.invalid) {
84
+      this.dankaForm.markAllAsTouched();
85
+      return;
86
+    }
85
     const formValue = this.dankaForm.value;
87
     const formValue = this.dankaForm.value;
86
     const isEdit = !!this.danka;
88
     const isEdit = !!this.danka;
87
     const dankaId = isEdit ? this.danka!.id : Date.now().toString();
89
     const dankaId = isEdit ? this.danka!.id : Date.now().toString();
88
-
89
     const updatedDanka = {
90
     const updatedDanka = {
90
       id: dankaId,
91
       id: dankaId,
91
-      householdName: formValue.householdName ?? '',
92
-      householder: formValue.householder ?? '',
93
-      postalCode: formValue.postalCode ?? '',
94
-      address: formValue.address ?? '',
95
-      phones: (formValue.phones ?? []).map((phone) => ({
96
-        tel: phone.tel ?? '',
97
-        note: phone.note ?? '',
98
-      })),
92
+      householdName: formValue.householdName?.trim() ?? '',
93
+      householder: formValue.householder?.trim() ?? '',
94
+      postalCode: formValue.postalCode?.trim() ?? '',
95
+      address: formValue.address?.trim() ?? '',
96
+      phones: (formValue.phones ?? [])
97
+        .map((phone) => ({
98
+          tel: phone.tel?.trim() ?? '',
99
+          note: phone.note?.trim() ?? '',
100
+        }))
101
+        .filter((phone) => phone.tel !== '' || phone.note !== ''),
99
     };
102
     };
100
 
103
 
101
     this.dankaService.saveDanka(updatedDanka);
104
     this.dankaService.saveDanka(updatedDanka);
105
     } else {
108
     } else {
106
       this.router.navigateByUrl('/danka-list');
109
       this.router.navigateByUrl('/danka-list');
107
     }
110
     }
108
-
109
   }
111
   }
110
 
112
 
111
   //削除の処理
113
   //削除の処理

+ 2
- 1
src/app/pages/danka-list/danka-list.ts Wyświetl plik

44
 
44
 
45
   //検索処理
45
   //検索処理
46
   searchDanka() {
46
   searchDanka() {
47
-    this.selectedFilter = 'search';
47
+        this.selectedFilter = 'search';
48
     //検索欄に入力された値から余白を削除
48
     //検索欄に入力された値から余白を削除
49
     const keyword = this.searchKeyword.trim();
49
     const keyword = this.searchKeyword.trim();
50
     //検索欄が空の場合は檀家の一覧を表示
50
     //検索欄が空の場合は檀家の一覧を表示
52
       this.filterDankaList = this.dankaList;
52
       this.filterDankaList = this.dankaList;
53
       return;
53
       return;
54
     }
54
     }
55
+
55
     //検索欄に値がある場合は値に該当する内容を表示
56
     //検索欄に値がある場合は値に該当する内容を表示
56
     this.filterDankaList = this.dankaList.filter((danka) => {
57
     this.filterDankaList = this.dankaList.filter((danka) => {
57
       return (
58
       return (

+ 4
- 4
src/app/services/dankaService.ts Wyświetl plik

14
       address: '市内 1-2-3',
14
       address: '市内 1-2-3',
15
       phones: [
15
       phones: [
16
         {
16
         {
17
-          tel: '03-xxxx-xxxx',
17
+          tel: '03-4567-8910',
18
           note: '寺報連絡',
18
           note: '寺報連絡',
19
         },
19
         },
20
         {
20
         {
21
-          tel: '090-xxxx-xxxx',
21
+          tel: '090-1234-5678',
22
           note: '世帯主',
22
           note: '世帯主',
23
         },
23
         },
24
       ],
24
       ],
31
       address: '市内 1-2-3',
31
       address: '市内 1-2-3',
32
       phones: [
32
       phones: [
33
         {
33
         {
34
-          tel: '03-xxxx-xxxx',
34
+          tel: '0-5678-9101',
35
           note: '寺報連絡',
35
           note: '寺報連絡',
36
         },
36
         },
37
         {
37
         {
38
-          tel: '090-xxxx-xxxx',
38
+          tel: '080-7890-4567',
39
           note: '世帯主',
39
           note: '世帯主',
40
         },
40
         },
41
       ],
41
       ],

Ładowanie…
Anuluj
Zapisz