-
Notifications
You must be signed in to change notification settings - Fork 2
/
your_independent_witness_mobile.go
87 lines (72 loc) · 2.62 KB
/
your_independent_witness_mobile.go
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
package donor
import (
"net/http"
"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/validation"
)
type yourIndependentWitnessMobileData struct {
App page.AppData
Errors validation.List
AuthorisedSignatory actor.AuthorisedSignatory
Form *yourIndependentWitnessMobileForm
}
func YourIndependentWitnessMobile(tmpl template.Template, donorStore DonorStore) Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request, donor *actor.DonorProvidedDetails) error {
data := &yourIndependentWitnessMobileData{
App: appData,
AuthorisedSignatory: donor.AuthorisedSignatory,
Form: &yourIndependentWitnessMobileForm{
HasNonUKMobile: donor.IndependentWitness.HasNonUKMobile,
},
}
if donor.IndependentWitness.HasNonUKMobile {
data.Form.NonUKMobile = donor.IndependentWitness.Mobile
} else {
data.Form.Mobile = donor.IndependentWitness.Mobile
}
if r.Method == http.MethodPost {
data.Form = readYourIndependentWitnessMobileForm(r)
data.Errors = data.Form.Validate()
if data.Errors.None() {
donor.IndependentWitness.HasNonUKMobile = data.Form.HasNonUKMobile
if data.Form.HasNonUKMobile {
donor.IndependentWitness.Mobile = data.Form.NonUKMobile
} else {
donor.IndependentWitness.Mobile = data.Form.Mobile
}
if err := donorStore.Put(r.Context(), donor); err != nil {
return err
}
return page.Paths.YourIndependentWitnessAddress.Redirect(w, r, appData, donor)
}
}
return tmpl(w, data)
}
}
type yourIndependentWitnessMobileForm struct {
Mobile string
HasNonUKMobile bool
NonUKMobile string
}
func readYourIndependentWitnessMobileForm(r *http.Request) *yourIndependentWitnessMobileForm {
return &yourIndependentWitnessMobileForm{
Mobile: page.PostFormString(r, "mobile"),
HasNonUKMobile: page.PostFormString(r, "has-non-uk-mobile") == "1",
NonUKMobile: page.PostFormString(r, "non-uk-mobile"),
}
}
func (d *yourIndependentWitnessMobileForm) Validate() validation.List {
var errors validation.List
if d.HasNonUKMobile {
errors.String("non-uk-mobile", "aMobilePhoneNumber", d.NonUKMobile,
validation.Empty(),
validation.NonUKMobile().ErrorLabel("enterAMobileNumberInTheCorrectFormat"))
} else {
errors.String("mobile", "aUKMobileNumber", d.Mobile,
validation.Empty(),
validation.Mobile().ErrorLabel("enterAMobileNumberInTheCorrectFormat"))
}
return errors
}