Skip to content

Commit e1cf9f2

Browse files
Kahbazicsharpfritz
authored andcommitted
Adding Validations (#23)
* Adding validation * Added RegexValidator and CustomValidator * Added style to BaseValidator * Added ValidationSummary
1 parent cc75852 commit e1cf9f2

15 files changed

+517
-4
lines changed

samples/AfterBlazorServerSide/Pages/ComponentList.razor

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
<ul>
2121

2222
<li>CompareValidator </li>
23-
<li>CustomValidator </li>
23+
<li><a href="/ControlSamples/CustomValidator">CustomValidator</a> </li>
2424
<li>RangeValidator </li>
25-
<li>RegularExpressionValidator(?) </li>
26-
<li>RequiredFieldValidator </li>
27-
<li>ValidationSummary </li>
25+
<li><a href="/ControlSamples/RegularExpressionValidator">RegularExpressionValidator(?)</a> </li>
26+
<li><a href="/ControlSamples/RequiredFieldValidator">RequiredFieldValidator</a> </li>
27+
<li><a href="/ControlSamples/ValidationSummary">ValidationSummary </a> </li>
2828

2929
</ul>
3030
</div>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
@page "/ControlSamples/CustomValidator"
2+
3+
<h3>Regular Expression Validator</h3>
4+
@using BlazorWebFormsComponents.Validations;
5+
6+
7+
8+
<EditForm Model="@exampleModel" OnValidSubmit="@HandleValidSubmit">
9+
10+
<label>Write something that starts with 'A'</label>
11+
<InputText id="name" @ref="Name.Current" @bind-Value="exampleModel.Name" />
12+
<CustomValidator ServerValidate="@DoesItStartWithA"
13+
ControlToValidate="@Name"
14+
Text="Does not start with 'A'."
15+
ErrorMessage="Does not start with 'A' but in summary!" />
16+
17+
<button type="submit">Submit</button>
18+
19+
</EditForm>
20+
21+
22+
23+
@code {
24+
ForwardRef<InputBase<string>> Name = new ForwardRef<InputBase<string>>();
25+
26+
private ExampleModel exampleModel = new ExampleModel();
27+
28+
private void HandleValidSubmit()
29+
{
30+
Console.WriteLine("OnValidSubmit");
31+
}
32+
private bool DoesItStartWithA(string value)
33+
{
34+
return value.StartsWith("A", StringComparison.InvariantCultureIgnoreCase);
35+
}
36+
public class ExampleModel
37+
{
38+
public string Name { get; set; }
39+
}
40+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
@page "/ControlSamples/RegularExpressionValidator"
2+
3+
<h3>Regular Expression Validator</h3>
4+
@using BlazorWebFormsComponents.Validations;
5+
6+
7+
8+
<EditForm Model="@exampleModel" OnValidSubmit="@HandleValidSubmit">
9+
10+
<label>Write a 5 digit number</label>
11+
<InputText id="name" @ref="Name.Current" @bind-Value="exampleModel.Name" />
12+
<RegularExpressionValidator ValidationExpression="^[0-9]{5}$"
13+
ControlToValidate="@Name"
14+
Text="Not a 5 digit number."
15+
ErrorMessage="Not a 5 digit number but in summary!" />
16+
17+
<button type="submit">Submit</button>
18+
19+
</EditForm>
20+
21+
22+
23+
@code {
24+
ForwardRef<InputBase<string>> Name = new ForwardRef<InputBase<string>>();
25+
26+
private ExampleModel exampleModel = new ExampleModel();
27+
28+
private void HandleValidSubmit()
29+
{
30+
Console.WriteLine("OnValidSubmit");
31+
}
32+
33+
public class ExampleModel
34+
{
35+
public string Name { get; set; }
36+
}
37+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
@page "/ControlSamples/RequiredFieldValidator"
2+
@using static System.Drawing.Color
3+
4+
<h3>Required Field Validator</h3>
5+
@using BlazorWebFormsComponents.Validations;
6+
7+
8+
9+
<EditForm Model="@exampleModel" OnValidSubmit="@HandleValidSubmit">
10+
Write something
11+
<InputText id="name" @ref="Name.Current" @bind-Value="exampleModel.Name" />
12+
<RequiredFieldValidator Type="string"
13+
ControlToValidate="@Name"
14+
ForeColor="Red"
15+
Text="Name is required."
16+
ErrorMessage="Name is required but in summary!" />
17+
18+
<InputNumber id="number" @ref="Number.Current" @bind-Value="exampleModel.Number" />
19+
<RequiredFieldValidator Type="int?"
20+
ControlToValidate="@Number"
21+
Text="Number is required."
22+
ErrorMessage="Number is required but in summary!" />
23+
24+
<button type="submit">Submit</button>
25+
26+
</EditForm>
27+
28+
29+
30+
@code {
31+
ForwardRef<InputBase<string>> Name = new ForwardRef<InputBase<string>>();
32+
ForwardRef<InputBase<int?>> Number = new ForwardRef<InputBase<int?>>();
33+
34+
private ExampleModel exampleModel = new ExampleModel();
35+
36+
private void HandleValidSubmit()
37+
{
38+
Console.WriteLine("OnValidSubmit");
39+
}
40+
41+
public class ExampleModel
42+
{
43+
public string Name { get; set; }
44+
public int? Number { get; set; }
45+
}
46+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
@page "/ControlSamples/ValidationSummary"
2+
@using static System.Drawing.Color
3+
4+
<h3>Required Field Validator</h3>
5+
@using BlazorWebFormsComponents.Validations;
6+
7+
8+
9+
<EditForm Model="@exampleModel" OnValidSubmit="@HandleValidSubmit">
10+
Write something
11+
<InputText id="name" @ref="Name.Current" @bind-Value="exampleModel.Name" />
12+
<RequiredFieldValidator Type="string"
13+
ControlToValidate="@Name"
14+
ForeColor="Red"
15+
Text="Name is required."
16+
ErrorMessage="Name is required but in summary!" />
17+
18+
<RegularExpressionValidator ValidationExpression="^[0-9]{5}$"
19+
ControlToValidate="@Name"
20+
Text="Not a 5 digit number."
21+
ErrorMessage="Not a 5 digit number but in summary!" />
22+
23+
<RegularExpressionValidator ValidationExpression="^[0-3]{5}$"
24+
ControlToValidate="@Name"
25+
Text="Not a 5 digit number with 0-3."
26+
ErrorMessage="Not a 5 digit number with 0-3 but in summary!" />
27+
28+
<button type="submit">Submit</button>
29+
30+
<h5>Bold Bullet List</h5>
31+
<AspNetValidationSummary Font_Bold="true" DisplayMode="BulletList" />
32+
33+
<h5>Lime List</h5>
34+
<AspNetValidationSummary ForeColor="Lime" DisplayMode="List" />
35+
36+
<h5>Italic Single Paragraph</h5>
37+
<AspNetValidationSummary Font_Italic="true" DisplayMode="SingleParagraph" />
38+
39+
</EditForm>
40+
41+
42+
43+
@code {
44+
ForwardRef<InputBase<string>> Name = new ForwardRef<InputBase<string>>();
45+
46+
private ExampleModel exampleModel = new ExampleModel();
47+
48+
private void HandleValidSubmit()
49+
{
50+
Console.WriteLine("OnValidSubmit");
51+
}
52+
53+
public class ExampleModel
54+
{
55+
public string Name { get; set; }
56+
public int? Number { get; set; }
57+
}
58+
}

samples/AfterBlazorServerSide/Pages/ControlSamples/_Imports.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
@using SharedSampleObjects.Models
33

44
@using static BlazorWebFormsComponents.Enums.RepeatLayout
5+
@using static BlazorWebFormsComponents.Enums.ValidationSummaryDisplayMode
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace BlazorWebFormsComponents.Enums
6+
{
7+
public abstract class ValidationSummaryDisplayMode
8+
{
9+
10+
public static ListDisplayMode List => new ListDisplayMode();
11+
public static BulletListDisplayMode BulletList => new BulletListDisplayMode();
12+
public static SingleParagraphDisplayMode SingleParagraph => new SingleParagraphDisplayMode();
13+
14+
}
15+
16+
public class ListDisplayMode : ValidationSummaryDisplayMode { }
17+
public class BulletListDisplayMode : ValidationSummaryDisplayMode { }
18+
public class SingleParagraphDisplayMode : ValidationSummaryDisplayMode { }
19+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
@inherits BaseWebFormsComponent
2+
@using BlazorWebFormsComponents.Enums
3+
4+
@if (Enabled)
5+
{
6+
<div style="@(CalculatedStyle + (IsValid ? null : "display:none;"))">
7+
@if (IsValid)
8+
{
9+
@switch (DisplayMode)
10+
{
11+
case BulletListDisplayMode b:
12+
<ul>
13+
@foreach (var message in ValidationMessages)
14+
{
15+
<li>@message</li>
16+
}
17+
</ul>
18+
break;
19+
case ListDisplayMode l:
20+
@foreach (var message in ValidationMessages)
21+
{
22+
@message
23+
<br>
24+
}
25+
break;
26+
case SingleParagraphDisplayMode s:
27+
@string.Join(" ", ValidationMessages)
28+
break;
29+
}
30+
}
31+
</div>
32+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Drawing;
4+
using System.Linq;
5+
using System.Text;
6+
using BlazorWebFormsComponents.Enums;
7+
using Microsoft.AspNetCore.Components;
8+
using Microsoft.AspNetCore.Components.Forms;
9+
10+
namespace BlazorWebFormsComponents.Validations
11+
{
12+
public partial class AspNetValidationSummary : BaseWebFormsComponent, IHasStyle, IDisposable
13+
{
14+
private EditContext _previousEditContext;
15+
private readonly EventHandler<ValidationStateChangedEventArgs> _validationStateChangedHandler;
16+
17+
[CascadingParameter] EditContext CurrentEditContext { get; set; }
18+
19+
[Parameter] public ValidationSummaryDisplayMode DisplayMode { get; set; } = ValidationSummaryDisplayMode.BulletList;
20+
[Parameter] public Color BackColor { get; set; }
21+
[Parameter] public Color BorderColor { get; set; }
22+
[Parameter] public BorderStyle BorderStyle { get; set; }
23+
[Parameter] public Unit BorderWidth { get; set; }
24+
[Parameter] public string CssClass { get; set; }
25+
[Parameter] public Color ForeColor { get; set; }
26+
[Parameter] public Unit Height { get; set; }
27+
[Parameter] public HorizontalAlign HorizontalAlign { get; set; }
28+
[Parameter] public VerticalAlign VerticalAlign { get; set; }
29+
[Parameter] public Unit Width { get; set; }
30+
[Parameter] public bool Font_Bold { get; set; }
31+
[Parameter] public bool Font_Italic { get; set; }
32+
[Parameter] public string Font_Names { get; set; }
33+
[Parameter] public bool Font_Overline { get; set; }
34+
[Parameter] public FontUnit Font_Size { get; set; }
35+
[Parameter] public bool Font_Strikeout { get; set; }
36+
[Parameter] public bool Font_Underline { get; set; }
37+
38+
protected string CalculatedStyle { get; set; }
39+
40+
public bool IsValid => CurrentEditContext.GetValidationMessages().Any();
41+
42+
public IEnumerable<string> ValidationMessages => CurrentEditContext.GetValidationMessages().Select(x => x.Split(',')[1]);
43+
44+
public AspNetValidationSummary()
45+
{
46+
_validationStateChangedHandler = (sender, eventArgs) => StateHasChanged();
47+
}
48+
49+
protected override void OnParametersSet()
50+
{
51+
52+
if (CurrentEditContext == null)
53+
{
54+
throw new InvalidOperationException($"{nameof(ValidationSummary)} requires a cascading parameter " +
55+
$"of type {nameof(EditContext)}. For example, you can use {nameof(ValidationSummary)} inside " +
56+
$"an {nameof(EditForm)}.");
57+
}
58+
59+
if (CurrentEditContext != _previousEditContext)
60+
{
61+
DetachValidationStateChangedListener();
62+
CurrentEditContext.OnValidationStateChanged += _validationStateChangedHandler;
63+
_previousEditContext = CurrentEditContext;
64+
}
65+
}
66+
67+
protected override void OnInitialized()
68+
{
69+
70+
this.SetFontsFromAttributes(AdditionalAttributes);
71+
72+
var styleBuilder = new StringBuilder();
73+
74+
this.ToStyleString(styleBuilder);
75+
76+
CalculatedStyle = styleBuilder.ToString();
77+
78+
base.OnInitialized();
79+
80+
}
81+
82+
public void Dispose()
83+
{
84+
DetachValidationStateChangedListener();
85+
}
86+
87+
private void DetachValidationStateChangedListener()
88+
{
89+
if (_previousEditContext != null)
90+
{
91+
_previousEditContext.OnValidationStateChanged -= _validationStateChangedHandler;
92+
}
93+
}
94+
}
95+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@inherits BaseWebFormsComponent
2+
@typeparam Type
3+
4+
@if (Enabled && !IsValid)
5+
{
6+
<span style="@CalculatedStyle">
7+
@if (string.IsNullOrWhiteSpace(Text))
8+
{
9+
@ErrorMessage
10+
}
11+
else
12+
{
13+
@Text
14+
}
15+
</span>
16+
}

0 commit comments

Comments
 (0)