public void AddValidation(AutoFormsValidationAttribute validation, View control, string title) { if (string.IsNullOrWhiteSpace(title)) { title = "Item"; } //Debug.WriteLine($"AddValidation {validation.Type} for {title}"); var label = new Label { IsVisible = false, IsEnabled = false, TextColor = _clrValidationText, Margin = new Thickness(3, 0, 0, 3) }; switch (validation) { case AutoFormsMaxLengthAttribute max when validation.Type == AutoFormsValidationAttribute.ValidationType.MaxLength: label.Text = $"\"{title}\" is over maximum length of {max.Length} "; MonitorEntryControl(true, validation, control, label); break; case AutoFormsMinLengthAttribute min when validation.Type == AutoFormsValidationAttribute.ValidationType.MinLength: label.Text = $"\"{title}\" must have at least {min.Length} characters"; MonitorEntryControl(true, validation, control, label); break; case AutoFormsValidationAttribute _ when validation.Type == AutoFormsValidationAttribute.ValidationType.Numeric: label.Text = $"\"{title}\" can only contain numeric values"; MonitorEntryControl(true, validation, control, label); break; case AutoFormsValidationAttribute _ when validation.Type == AutoFormsValidationAttribute.ValidationType.Email: label.Text = $"\"{title}\" must be a valid email address"; MonitorEntryControl(false, validation, control, label); break; case AutoFormsValidationAttribute _ when validation.Type == AutoFormsValidationAttribute.ValidationType.Required: label.Text = $"\"{title}\" is required"; MonitorEntryControl(false, validation, control, label); break; default: label = null; break; } if (label == null) { return; } _stack.Children.Add(label); _validations.Add(new Validation(validation, control, label)); }
public void AddValidation(AutoFormsValidationAttribute validation, View control, string title) { if (string.IsNullOrWhiteSpace(title)) { title = "Item"; } title = ControlBase.GetLocalizedString(title); var label = new Label { IsVisible = false, IsEnabled = false, TextColor = _clrValidationText, Margin = new Thickness(3, 0, 0, 3) }; switch (validation) { case AutoFormsMaxLengthAttribute max when validation.Type == AutoFormsValidationAttribute.ValidationType.MaxLength: label.Text = string.Format(ControlBase.GetLocalizedString(AutoFormsConstants.ValidationMaxLength), title, max.Length); MonitorEntryControl(true, validation, control, label); break; case AutoFormsMinLengthAttribute min when validation.Type == AutoFormsValidationAttribute.ValidationType.MinLength: label.Text = string.Format(ControlBase.GetLocalizedString(AutoFormsConstants.ValidationMinLength), title, min.Length); break; case AutoFormsValidationAttribute _ when validation.Type == AutoFormsValidationAttribute.ValidationType.Numeric: label.Text = string.Format(ControlBase.GetLocalizedString(AutoFormsConstants.ValidationNumeric), title); break; case AutoFormsValidationAttribute _ when validation.Type == AutoFormsValidationAttribute.ValidationType.Email: label.Text = string.Format(ControlBase.GetLocalizedString(AutoFormsConstants.ValidationEmail), title); MonitorEntryControl(false, validation, control, label); break; case AutoFormsValidationAttribute _ when validation.Type == AutoFormsValidationAttribute.ValidationType.Required: label.Text = string.Format(ControlBase.GetLocalizedString(AutoFormsConstants.ValidationRequired), title); MonitorEntryControl(false, validation, control, label); break; default: label = null; break; } if (label == null) { return; } _stack.Children.Add(label); _validations.Add(new Validation(validation, control, label)); }
public Validation(AutoFormsValidationAttribute validationAttribute, View control, Label label) { ValidationAttribute = validationAttribute; Control = control; Label = label; }
private void MonitorEntryControl(bool activeMonitoring, AutoFormsValidationAttribute attribute, View control, Label label) { var c = control as Entry; if (c != null) { if (activeMonitoring) { c.TextChanged += (object sender, TextChangedEventArgs e) => { var invalid = !attribute.IsValid(e.NewTextValue); if (label.IsEnabled != invalid) { ToggleValidation(label); } }; } c.Unfocused += (object sender, FocusEventArgs e) => { if (!e.IsFocused) { var invalid = !attribute.IsValid(c.Text); if (label.IsEnabled != invalid) { ToggleValidation(label); } } }; return; } var ed = control as Editor; if (ed != null) { if (activeMonitoring) { ed.TextChanged += (object sender, TextChangedEventArgs e) => { var invalid = !attribute.IsValid(e.NewTextValue); if (label.IsEnabled != invalid) { ToggleValidation(label); } }; } ed.Unfocused += (object sender, FocusEventArgs e) => { if (!e.IsFocused) { var invalid = !attribute.IsValid(ed.Text); if (label.IsEnabled != invalid) { ToggleValidation(label); } } }; } }