Skip to content

Commit

Permalink
added remove validator button
Browse files Browse the repository at this point in the history
  • Loading branch information
suxrobGM committed Aug 2, 2024
1 parent 41f3939 commit a541afc
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 13 deletions.
44 changes: 40 additions & 4 deletions src/FormBuilder/Components/FieldValidatorsEditor.razor
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,58 @@
{
case RequiredValidator requiredValidator:
<RadzenAccordionItem Text="Required Validator" Icon="check">
<ValidatorPropertyEditor Validator="@requiredValidator"/>
<ValidatorPropertyEditor Validator="@requiredValidator">
<Header>
<RadzenButton
Text="Remove"
ButtonStyle="ButtonStyle.Danger"
Size="ButtonSize.Small"
Click="() => RemoveValidator(validator)">
</RadzenButton>
</Header>
</ValidatorPropertyEditor>
</RadzenAccordionItem>
break;
case EmailValidator emailValidator:
<RadzenAccordionItem Text="Email Validator" Icon="mail">
<ValidatorPropertyEditor Validator="@emailValidator"/>
<ValidatorPropertyEditor Validator="@emailValidator">
<Header>
<RadzenButton
Text="Remove"
ButtonStyle="ButtonStyle.Danger"
Size="ButtonSize.Small"
Click="() => RemoveValidator(validator)">
</RadzenButton>
</Header>
</ValidatorPropertyEditor>
</RadzenAccordionItem>
break;
case LengthValidator lengthValidator:
<RadzenAccordionItem Text="Length Validator" Icon="abc">
<ValidatorPropertyEditor Validator="@lengthValidator"/>
<ValidatorPropertyEditor Validator="@lengthValidator">
<Header>
<RadzenButton
Text="Remove"
ButtonStyle="ButtonStyle.Danger"
Size="ButtonSize.Small"
Click="() => RemoveValidator(validator)">
</RadzenButton>
</Header>
</ValidatorPropertyEditor>
</RadzenAccordionItem>
break;
case NumericRangeValidator numericRangeValidator:
<RadzenAccordionItem Text="Numeric Range Validator" Icon="filter_1">
<ValidatorPropertyEditor Validator="@numericRangeValidator"/>
<ValidatorPropertyEditor Validator="@numericRangeValidator">
<Header>
<RadzenButton
Text="Remove"
ButtonStyle="ButtonStyle.Danger"
Size="ButtonSize.Small"
Click="() => RemoveValidator(validator)">
</RadzenButton>
</Header>
</ValidatorPropertyEditor>
</RadzenAccordionItem>
break;
}
Expand Down
15 changes: 15 additions & 0 deletions src/FormBuilder/Components/FieldValidatorsEditor.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
26 changes: 19 additions & 7 deletions src/FormBuilder/Components/FormField.razor
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,25 @@
</RadzenTextBox>
break;
case SelectField selectField:
<RadzenDropDown
Name="@Field.Name"
@bind-Value="selectField.Value"
Data="@selectField.Options"
Placeholder="@selectField.Placeholder"
ReadOnly="Disabled">
</RadzenDropDown>
if (Disabled)
{
<RadzenTextBox
Name="@Field.Name"
@bind-Value="selectField.Value"
Placeholder="@selectField.Placeholder"
ReadOnly="Disabled">
</RadzenTextBox>
}
else
{
<RadzenDropDown
Name="@Field.Name"
@bind-Value="selectField.Value"
Data="@selectField.Options"
Placeholder="@selectField.Placeholder">
</RadzenDropDown>
}

break;
case NumericField<int> numericIntField:
<RadzenNumeric
Expand Down
3 changes: 1 addition & 2 deletions src/FormBuilder/Components/FormField.razor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Globalization;
using FormBuilder.Models;
using FormBuilder.Models;
using Microsoft.AspNetCore.Components;

namespace FormBuilder.Components;
Expand Down
1 change: 1 addition & 0 deletions src/FormBuilder/Components/ValidatorPropertyEditor.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@typeparam TValue where TValue : FormBuilder.Models.Validator

<RadzenStack Orientation="Orientation.Vertical">
@Header
<RadzenFormField Text="Error Text">
<RadzenTextBox @bind-Value="@Validator.Text"/>
</RadzenFormField>
Expand Down
3 changes: 3 additions & 0 deletions src/FormBuilder/Components/ValidatorPropertyEditor.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public partial class ValidatorPropertyEditor<TValue> : ComponentBase where TValu

[Parameter, EditorRequired]
public TValue Validator { get; set; } = default!;

[Parameter]
public RenderFragment? Header { get; set; }

#endregion
}
4 changes: 4 additions & 0 deletions src/FormBuilder/Models/Fields/Field.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,17 @@ 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 })
{
foreach (Validator validator in args.OldItems)
{
validator.PropertyChanged -= RaiseValidatorsChanged;
}

OnPropertyChanged(nameof(Validators));
}
}

Expand Down

0 comments on commit a541afc

Please sign in to comment.