From a541afca92dacc41d343e994073707b002ad179c Mon Sep 17 00:00:00 2001 From: Sukhrob Ilyosbekov Date: Fri, 2 Aug 2024 01:29:27 -0400 Subject: [PATCH] added remove validator button --- .../Components/FieldValidatorsEditor.razor | 44 +++++++++++++++++-- .../Components/FieldValidatorsEditor.razor.cs | 15 +++++++ src/FormBuilder/Components/FormField.razor | 26 ++++++++--- src/FormBuilder/Components/FormField.razor.cs | 3 +- .../Components/ValidatorPropertyEditor.razor | 1 + .../ValidatorPropertyEditor.razor.cs | 3 ++ src/FormBuilder/Models/Fields/Field.cs | 4 ++ 7 files changed, 83 insertions(+), 13 deletions(-) diff --git a/src/FormBuilder/Components/FieldValidatorsEditor.razor b/src/FormBuilder/Components/FieldValidatorsEditor.razor index 60c9b40..6446de4 100644 --- a/src/FormBuilder/Components/FieldValidatorsEditor.razor +++ b/src/FormBuilder/Components/FieldValidatorsEditor.razor @@ -37,22 +37,58 @@ { case RequiredValidator requiredValidator: - + +
+ + +
+
break; case EmailValidator emailValidator: - + +
+ + +
+
break; case LengthValidator lengthValidator: - + +
+ + +
+
break; case NumericRangeValidator numericRangeValidator: - + +
+ + +
+
break; } diff --git a/src/FormBuilder/Components/FieldValidatorsEditor.razor.cs b/src/FormBuilder/Components/FieldValidatorsEditor.razor.cs index d13610c..3983671 100644 --- a/src/FormBuilder/Components/FieldValidatorsEditor.razor.cs +++ b/src/FormBuilder/Components/FieldValidatorsEditor.razor.cs @@ -15,7 +15,22 @@ public partial class FieldValidatorsEditor : ComponentBase private void AddValidator(ValidatorType validatorType) { + if (HasValidator(validatorType)) + { + return; + } + var validator = ValidatorFactory.Create(validatorType); Field.Validators.Add(validator); } + + private void RemoveValidator(Validator validator) + { + Field.Validators.Remove(validator); + } + + private bool HasValidator(ValidatorType validatorType) + { + return Field.Validators.Any(v => v.Type == validatorType); + } } diff --git a/src/FormBuilder/Components/FormField.razor b/src/FormBuilder/Components/FormField.razor index 604e807..d363934 100644 --- a/src/FormBuilder/Components/FormField.razor +++ b/src/FormBuilder/Components/FormField.razor @@ -15,13 +15,25 @@ break; case SelectField selectField: - - + if (Disabled) + { + + + } + else + { + + + } + break; case NumericField numericIntField: + @Header diff --git a/src/FormBuilder/Components/ValidatorPropertyEditor.razor.cs b/src/FormBuilder/Components/ValidatorPropertyEditor.razor.cs index 5d89800..57ea3ad 100644 --- a/src/FormBuilder/Components/ValidatorPropertyEditor.razor.cs +++ b/src/FormBuilder/Components/ValidatorPropertyEditor.razor.cs @@ -9,6 +9,9 @@ public partial class ValidatorPropertyEditor : ComponentBase where TValu [Parameter, EditorRequired] public TValue Validator { get; set; } = default!; + + [Parameter] + public RenderFragment? Header { get; set; } #endregion } diff --git a/src/FormBuilder/Models/Fields/Field.cs b/src/FormBuilder/Models/Fields/Field.cs index 68198e9..75103a0 100644 --- a/src/FormBuilder/Models/Fields/Field.cs +++ b/src/FormBuilder/Models/Fields/Field.cs @@ -104,6 +104,8 @@ private void HandleValidatorsCollectionChanged(object? sender, NotifyCollectionC { validator.PropertyChanged += RaiseValidatorsChanged; } + + OnPropertyChanged(nameof(Validators)); // Notify that the collection has changed } else if (args is { Action: NotifyCollectionChangedAction.Remove, OldItems: not null }) { @@ -111,6 +113,8 @@ private void HandleValidatorsCollectionChanged(object? sender, NotifyCollectionC { validator.PropertyChanged -= RaiseValidatorsChanged; } + + OnPropertyChanged(nameof(Validators)); } }