diff --git a/form/fileButton.go b/form/fileButton.go index 597b5dbcb43a829963399b32be4e9c1b1cb60749..35d636d74fce3f675649a361153ab78ab4474c6e 100644 --- a/form/fileButton.go +++ b/form/fileButton.go @@ -110,10 +110,10 @@ func (i *FileButton) SetFile(value string) { // If validation fails, the error is set as the help text and returns true. // If validations succeeds, it returns true. func (i *FileButton) Validate() (interface{}, bool) { - validated, err := i.v(i.value) + validated, helpText, err := i.v(i.value) if err != nil { jww.ERROR.Printf("Failed to validate input %+v: %+v", i.caption, err) - i.SetHelpText(err.Error()) + i.SetHelpText(helpText) return nil, false } diff --git a/form/part.go b/form/part.go index e0ea0b24ca198360504f63b25482090ad3f5babd..86fa6923cdf1839240f9a00115d1839a046ce533 100644 --- a/form/part.go +++ b/form/part.go @@ -9,7 +9,7 @@ import ( // ValidateFunc is the function used to validate an input value when a form is // submitted. If the validation fails, an error describing why is returned. If // validation succeeds, nil is returned. -type ValidateFunc func(str string) (interface{}, error) +type ValidateFunc func(str string) (interface{}, string, error) type Part struct { caption string @@ -39,19 +39,20 @@ func (p *Part) ClearHelpText() { func (p *Part) Validate() (interface{}, bool) { var validated interface{} var err error + var helpText string if t, _ := p.GetInputAttribute("type"); t == "checkbox" { val := "" if p.Checked() { val = "true" } - validated, err = p.v(val) + validated, helpText, err = p.v(val) } else { - validated, err = p.v(p.f.GetValue()) + validated, helpText, err = p.v(p.f.GetValue()) } if err != nil { jww.ERROR.Printf("Failed to validate input %q: %+v", p.caption, err) - p.SetHelpText(err.Error()) + p.SetHelpText(helpText) return nil, false } diff --git a/form/validate.go b/form/validate.go index 62a5e72b9a337043b17c83ee91e1ee2b6919f320..ce20bbfd4ab1a5c668a3863cf26219bab8c66bf5 100644 --- a/form/validate.go +++ b/form/validate.go @@ -1,6 +1,7 @@ package form import ( + "fmt" "github.com/pkg/errors" "github.com/xx-labs/sleeve/wallet" "gitlab.com/xx_network/primitives/id/idf" @@ -11,89 +12,90 @@ import ( // ValidateXXNetworkAddress returns an error if the xx network address is // invalid. This function adheres to the ValidateFunc type. -func ValidateXXNetworkAddress(str string) (interface{}, error) { +func ValidateXXNetworkAddress(str string) (interface{}, string, error) { if len(str) == 0 { - return nil, errors.New("Required.") + return nil, "Required", errors.New("Required") } ok, err := wallet.ValidateXXNetworkAddress(str) if !ok || err != nil { - return nil, errors.Errorf("Invalid wallet address: %s", err.Error()) + return nil, "Invalid wallet address", errors.Errorf("Invalid wallet address: %s", err.Error()) } - return str, nil + return str, "", nil } // ValidateEmail returns an error if the email is invalid. This function adheres // to the ValidateFunc type. -func ValidateEmail(str string) (interface{}, error) { +func ValidateEmail(str string) (interface{}, string, error) { if len(str) == 0 { - return "", nil + return "", "", nil } _, err := mail.ParseAddress(str) - return str, err + return str, "Invalid email address", err } // ValidateMultiplier returns an error if the xx network address is // invalid. func ValidateMultiplier(max uint64) ValidateFunc { - return func(str string) (interface{}, error) { + return func(str string) (interface{}, string, error) { if len(str) == 0 { - return nil, errors.New("Required.") + return nil, "Required", errors.New("Required") } u64, err := strconv.ParseUint(str, 10, 64) if err != nil { - return nil, err + return nil, "Invalid integer", err } if u64 < 0 || u64 > max { - return nil, errors.Errorf( + helpText := fmt.Sprintf("Must be between %d and %d", 0, max) + return nil, helpText, errors.Errorf( "value must be between %d and %d", 0, max) } - return u64, nil + return u64, "", nil } } // ValidateFilePath returns an error if the file path is invalid. This function // adheres to the ValidateFunc type. -func ValidateFilePath(str string) (interface{}, error) { - if len(str) == 0 { - return nil, errors.New("Required.") +func ValidateFilePath(str string) (interface{}, string, error) { + if len(str) == 0 || str == "No file chosen" { + return nil, "Required", errors.New("Required") } file, err := utils.ReadFile(str) if err != nil { - return nil, err + return nil, "Failed to read file", err } - return file, nil + return file, "", nil } // ValidateIdfPath returns an error if the IDF file path is invalid. This // function adheres to the ValidateFunc type. -func ValidateIdfPath(str string) (interface{}, error) { - if len(str) == 0 { - return nil, errors.New("Required.") +func ValidateIdfPath(str string) (interface{}, string, error) { + if len(str) == 0 || str == "No file chosen" { + return nil, "Required", errors.New("Required") } _, nid, err := idf.UnloadIDF(str) if err != nil { - return nil, err + return nil, "Failed to read IDF", err } - return nid.HexEncode(), nil + return nid.HexEncode(), "", nil } // ValidateCheckbox returns an error if the checkbox is not checked. This // function adheres to the ValidateFunc type. -func ValidateCheckbox(str string) (interface{}, error) { +func ValidateCheckbox(str string) (interface{}, string, error) { if len(str) == 0 { - return nil, errors.New("Required.") + return nil, "Required", errors.New("Required") } - return true, nil + return true, "", nil } diff --git a/main.js b/main.js index 99a5130cecf65cd65742f51a00f0d79c06471ce5..4ece0a5aa63f8f6ffa4e085ee463af0d3d3e6b11 100644 --- a/main.js +++ b/main.js @@ -128,28 +128,25 @@ avoid_reload(); start_process(); -function changeRangeValue(val, max){ - if (val > max) { - console.log("here 1") - document.getElementById("number").value = max; +function changeRangeValue(val, max) { + val = val.replace(/\D/g, ''); + if (val > max) { + val = max; } else if (val < 0) { - console.log("here 2") - document.getElementById("number").value = 0; + val = 0; } else if (val == "-0") { - console.log("here 3") - document.getElementById("number").value = 0; + val = 0; } else if (val % 1 != 0) { - console.log("here 4") - document.getElementById("number").value = Math.floor(val); + val = Math.floor(val); } else if (val.length > 7) { - console.log("here 6") - val = val.slice(0, 7); - document.getElementById("number").value = val; - } - console.log("here 7" + val) + val = val.slice(0, 7); + } + if (val.length > 0) { + document.getElementById("number").value = val; + } document.getElementById("range").value = isNaN(parseInt(val, 10)) ? 0 : parseInt(val, 10); } -function changeInputValue(val){ +function changeInputValue(val) { document.getElementById("number").value = isNaN(parseInt(val, 10)) ? 0 : parseInt(val, 10); } \ No newline at end of file