Browse Source

[update]

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

+ 40
- 30
src/app/pages/danka-edit/danka-edit.html View File

@@ -21,29 +21,44 @@
21 21
             <div class="form-list">
22 22
               <div class="form-row">
23 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 34
               </div>
30 35
 
31 36
               <div class="form-row">
32 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 48
               </div>
39 49
 
40 50
               <div class="form-row">
41 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 62
               </div>
48 63
 
49 64
               <div class="form-row">
@@ -73,32 +88,26 @@
73 88
               @for (phone of phones.controls; track $index; let i = $index) {
74 89
                 <div class="phone-table-row" [formGroupName]="i">
75 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 97
                   </div>
82 98
 
83 99
                   <div>
84
-                    <input
85
-                      type="text"
86
-                      formControlName="note"
87
-                      placeholder="備考"
88
-                    />
100
+                    <input type="text" formControlName="note" placeholder="備考"/>
89 101
                   </div>
90 102
 
91 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 106
                     </button>
99 107
                   </div>
100 108
                 </div>
101 109
               }
110
+
102 111
             </div>
103 112
 
104 113
             <div class="phone-action">
@@ -125,6 +134,7 @@
125 134
           <button
126 135
             type="button"
127 136
             class="save-button"
137
+            [disabled]="dankaForm.invalid"
128 138
             (click)="saveDanka()"
129 139
           >
130 140
             保存

+ 42
- 0
src/app/pages/danka-edit/danka-edit.scss View File

@@ -95,6 +95,12 @@
95 95
   margin-top: 14px;
96 96
 }
97 97
 
98
+.form-field {
99
+  width: 100%;
100
+  display: flex;
101
+  flex-direction: column;
102
+}
103
+
98 104
 .form-row {
99 105
   display: grid;
100 106
   grid-template-columns: 120px 1fr;
@@ -311,3 +317,39 @@
311 317
   font-size: 14px;
312 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 View File

@@ -23,15 +23,11 @@ export class DankaEdit {
23 23
   danka: Danka | undefined;
24 24
 
25 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 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 33
   constructor(
@@ -65,9 +61,9 @@ export class DankaEdit {
65 61
     return this.dankaForm.get('phones') as FormArray;
66 62
   }
67 63
 
68
-  createPhoneForm(string = '', note = '') {
64
+  createPhoneForm(tel = '', note = '') {
69 65
     return new FormGroup({
70
-      tel: new FormControl(string),
66
+      tel: new FormControl(tel, [Validators.pattern(/^[0-9-]+$/)]),
71 67
       note: new FormControl(note),
72 68
     });
73 69
   }
@@ -77,25 +73,32 @@ export class DankaEdit {
77 73
   }
78 74
 
79 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 82
   saveDanka() {
83
+    if (this.dankaForm.invalid) {
84
+      this.dankaForm.markAllAsTouched();
85
+      return;
86
+    }
85 87
     const formValue = this.dankaForm.value;
86 88
     const isEdit = !!this.danka;
87 89
     const dankaId = isEdit ? this.danka!.id : Date.now().toString();
88
-
89 90
     const updatedDanka = {
90 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 104
     this.dankaService.saveDanka(updatedDanka);
@@ -105,7 +108,6 @@ export class DankaEdit {
105 108
     } else {
106 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 View File

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

+ 4
- 4
src/app/services/dankaService.ts View File

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

Loading…
Cancel
Save