Skip to content
Snippets Groups Projects
Commit 9ffa6498 authored by Jono Wenger's avatar Jono Wenger
Browse files

Fix range

parent e9f16b9c
No related branches found
No related tags found
No related merge requests found
...@@ -110,10 +110,10 @@ func (i *FileButton) SetFile(value string) { ...@@ -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 validation fails, the error is set as the help text and returns true.
// If validations succeeds, it returns true. // If validations succeeds, it returns true.
func (i *FileButton) Validate() (interface{}, bool) { func (i *FileButton) Validate() (interface{}, bool) {
validated, err := i.v(i.value) validated, helpText, err := i.v(i.value)
if err != nil { if err != nil {
jww.ERROR.Printf("Failed to validate input %+v: %+v", i.caption, err) jww.ERROR.Printf("Failed to validate input %+v: %+v", i.caption, err)
i.SetHelpText(err.Error()) i.SetHelpText(helpText)
return nil, false return nil, false
} }
......
...@@ -9,7 +9,7 @@ import ( ...@@ -9,7 +9,7 @@ import (
// ValidateFunc is the function used to validate an input value when a form is // 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 // submitted. If the validation fails, an error describing why is returned. If
// validation succeeds, nil is returned. // validation succeeds, nil is returned.
type ValidateFunc func(str string) (interface{}, error) type ValidateFunc func(str string) (interface{}, string, error)
type Part struct { type Part struct {
caption string caption string
...@@ -39,19 +39,20 @@ func (p *Part) ClearHelpText() { ...@@ -39,19 +39,20 @@ func (p *Part) ClearHelpText() {
func (p *Part) Validate() (interface{}, bool) { func (p *Part) Validate() (interface{}, bool) {
var validated interface{} var validated interface{}
var err error var err error
var helpText string
if t, _ := p.GetInputAttribute("type"); t == "checkbox" { if t, _ := p.GetInputAttribute("type"); t == "checkbox" {
val := "" val := ""
if p.Checked() { if p.Checked() {
val = "true" val = "true"
} }
validated, err = p.v(val) validated, helpText, err = p.v(val)
} else { } else {
validated, err = p.v(p.f.GetValue()) validated, helpText, err = p.v(p.f.GetValue())
} }
if err != nil { if err != nil {
jww.ERROR.Printf("Failed to validate input %q: %+v", p.caption, err) jww.ERROR.Printf("Failed to validate input %q: %+v", p.caption, err)
p.SetHelpText(err.Error()) p.SetHelpText(helpText)
return nil, false return nil, false
} }
......
package form package form
import ( import (
"fmt"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/xx-labs/sleeve/wallet" "github.com/xx-labs/sleeve/wallet"
"gitlab.com/xx_network/primitives/id/idf" "gitlab.com/xx_network/primitives/id/idf"
...@@ -11,89 +12,90 @@ import ( ...@@ -11,89 +12,90 @@ import (
// ValidateXXNetworkAddress returns an error if the xx network address is // ValidateXXNetworkAddress returns an error if the xx network address is
// invalid. This function adheres to the ValidateFunc type. // 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 { if len(str) == 0 {
return nil, errors.New("Required.") return nil, "Required", errors.New("Required")
} }
ok, err := wallet.ValidateXXNetworkAddress(str) ok, err := wallet.ValidateXXNetworkAddress(str)
if !ok || err != nil { 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 // ValidateEmail returns an error if the email is invalid. This function adheres
// to the ValidateFunc type. // to the ValidateFunc type.
func ValidateEmail(str string) (interface{}, error) { func ValidateEmail(str string) (interface{}, string, error) {
if len(str) == 0 { if len(str) == 0 {
return "", nil return "", "", nil
} }
_, err := mail.ParseAddress(str) _, err := mail.ParseAddress(str)
return str, err return str, "Invalid email address", err
} }
// ValidateMultiplier returns an error if the xx network address is // ValidateMultiplier returns an error if the xx network address is
// invalid. // invalid.
func ValidateMultiplier(max uint64) ValidateFunc { func ValidateMultiplier(max uint64) ValidateFunc {
return func(str string) (interface{}, error) { return func(str string) (interface{}, string, error) {
if len(str) == 0 { if len(str) == 0 {
return nil, errors.New("Required.") return nil, "Required", errors.New("Required")
} }
u64, err := strconv.ParseUint(str, 10, 64) u64, err := strconv.ParseUint(str, 10, 64)
if err != nil { if err != nil {
return nil, err return nil, "Invalid integer", err
} }
if u64 < 0 || u64 > max { 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) "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 // ValidateFilePath returns an error if the file path is invalid. This function
// adheres to the ValidateFunc type. // adheres to the ValidateFunc type.
func ValidateFilePath(str string) (interface{}, error) { func ValidateFilePath(str string) (interface{}, string, error) {
if len(str) == 0 { if len(str) == 0 || str == "No file chosen" {
return nil, errors.New("Required.") return nil, "Required", errors.New("Required")
} }
file, err := utils.ReadFile(str) file, err := utils.ReadFile(str)
if err != nil { 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 // ValidateIdfPath returns an error if the IDF file path is invalid. This
// function adheres to the ValidateFunc type. // function adheres to the ValidateFunc type.
func ValidateIdfPath(str string) (interface{}, error) { func ValidateIdfPath(str string) (interface{}, string, error) {
if len(str) == 0 { if len(str) == 0 || str == "No file chosen" {
return nil, errors.New("Required.") return nil, "Required", errors.New("Required")
} }
_, nid, err := idf.UnloadIDF(str) _, nid, err := idf.UnloadIDF(str)
if err != nil { 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 // ValidateCheckbox returns an error if the checkbox is not checked. This
// function adheres to the ValidateFunc type. // function adheres to the ValidateFunc type.
func ValidateCheckbox(str string) (interface{}, error) { func ValidateCheckbox(str string) (interface{}, string, error) {
if len(str) == 0 { if len(str) == 0 {
return nil, errors.New("Required.") return nil, "Required", errors.New("Required")
} }
return true, nil return true, "", nil
} }
...@@ -129,24 +129,21 @@ start_process(); ...@@ -129,24 +129,21 @@ start_process();
function changeRangeValue(val, max) { function changeRangeValue(val, max) {
val = val.replace(/\D/g, '');
if (val > max) { if (val > max) {
console.log("here 1") val = max;
document.getElementById("number").value = max;
} else if (val < 0) { } else if (val < 0) {
console.log("here 2") val = 0;
document.getElementById("number").value = 0;
} else if (val == "-0") { } else if (val == "-0") {
console.log("here 3") val = 0;
document.getElementById("number").value = 0;
} else if (val % 1 != 0) { } else if (val % 1 != 0) {
console.log("here 4") val = Math.floor(val);
document.getElementById("number").value = Math.floor(val);
} else if (val.length > 7) { } else if (val.length > 7) {
console.log("here 6")
val = val.slice(0, 7); val = val.slice(0, 7);
}
if (val.length > 0) {
document.getElementById("number").value = val; document.getElementById("number").value = val;
} }
console.log("here 7" + val)
document.getElementById("range").value = isNaN(parseInt(val, 10)) ? 0 : parseInt(val, 10); document.getElementById("range").value = isNaN(parseInt(val, 10)) ? 0 : parseInt(val, 10);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment