Skip to content

Commit

Permalink
[Hosts]Indicate when an entry input field is invalid (microsoft#31407)
Browse files Browse the repository at this point in the history
* [Hosts]Indicate when an entry input field is invalid

* Minor styling fix

* Update src/modules/Hosts/Hosts/Strings/en-us/Resources.resw

Co-authored-by: Davide Giacometti <davide.giacometti@outlook.it>

* Update src/modules/Hosts/Hosts/Strings/en-us/Resources.resw

Co-authored-by: Davide Giacometti <davide.giacometti@outlook.it>

* Change messages to "Has to be"

---------

Co-authored-by: Niels Laute <niels.laute@live.nl>
Co-authored-by: Davide Giacometti <davide.giacometti@outlook.it>
  • Loading branch information
3 people authored Feb 14, 2024
1 parent 914861b commit 76de196
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
35 changes: 33 additions & 2 deletions src/modules/Hosts/Hosts/HostsXAML/Views/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,23 @@
<TextBox
x:Uid="Address"
IsSpellCheckEnabled="False"
Text="{Binding Address, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
Text="{Binding Address, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<TextBox.Description>
<StackPanel
Margin="0,4"
Orientation="Horizontal"
Spacing="4"
Visibility="{Binding IsAddressValid, Converter={StaticResource BoolToInvertedVisibilityConverter}}">
<FontIcon
Margin="0,0,0,0"
AutomationProperties.AccessibilityView="Raw"
FontSize="14"
Foreground="{ThemeResource SystemFillColorCautionBrush}"
Glyph="&#xE7BA;" />
<TextBlock x:Uid="EntryAddressIsInvalidWarning" />
</StackPanel>
</TextBox.Description>
</TextBox>
<TextBox
x:Uid="Hosts"
AcceptsReturn="False"
Expand All @@ -459,7 +475,22 @@
ScrollViewer.VerticalScrollBarVisibility="Visible"
ScrollViewer.VerticalScrollMode="Enabled"
Text="{Binding Hosts, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
TextWrapping="Wrap" />
TextWrapping="Wrap">
<TextBox.Description>
<StackPanel
Margin="0,4"
Orientation="Horizontal"
Spacing="4"
Visibility="{Binding IsHostsValid, Converter={StaticResource BoolToInvertedVisibilityConverter}}">
<FontIcon
AutomationProperties.AccessibilityView="Raw"
FontSize="14"
Foreground="{ThemeResource SystemFillColorCautionBrush}"
Glyph="&#xE7BA;" />
<TextBlock x:Uid="EntryHostsIsInvalidWarning" />
</StackPanel>
</TextBox.Description>
</TextBox>
<TextBox
x:Uid="Comment"
AcceptsReturn="False"
Expand Down
18 changes: 17 additions & 1 deletion src/modules/Hosts/Hosts/Models/Entry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public partial class Entry : ObservableObject

[ObservableProperty]
[NotifyPropertyChangedFor(nameof(Valid))]
[NotifyPropertyChangedFor(nameof(IsAddressValid))]
private string _address;

partial void OnAddressChanged(string value)
Expand All @@ -36,6 +37,7 @@ partial void OnAddressChanged(string value)

[ObservableProperty]
[NotifyPropertyChangedFor(nameof(Valid))]
[NotifyPropertyChangedFor(nameof(IsHostsValid))]
private string _hosts;

partial void OnHostsChanged(string value)
Expand All @@ -61,6 +63,10 @@ partial void OnHostsChanged(string value)

public bool Valid => Validate(true);

public bool IsAddressValid => ValidateAddressField();

public bool IsHostsValid => ValidateHostsField(true);

public string Line { get; private set; }

public AddressType Type { get; private set; }
Expand Down Expand Up @@ -152,14 +158,24 @@ public Entry Clone()
};
}

private bool ValidateAddressField()
{
return Type != AddressType.Invalid;
}

private bool ValidateHostsField(bool validateHostsLength)
{
return ValidationHelper.ValidHosts(Hosts, validateHostsLength);
}

public bool Validate(bool validateHostsLength)
{
if (Equals("102.54.94.97", "rhino.acme.com", "source server") || Equals("38.25.63.10", "x.acme.com", "x client host"))
{
return false;
}

return Type != AddressType.Invalid && ValidationHelper.ValidHosts(Hosts, validateHostsLength);
return ValidateAddressField() && ValidateHostsField(validateHostsLength);
}

private bool Equals(string address, string hosts, string comment)
Expand Down
6 changes: 6 additions & 0 deletions src/modules/Hosts/Hosts/Strings/en-us/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@
<value>Entries contain too many hosts</value>
<comment>"Hosts" refers to the system hosts file, do not loc</comment>
</data>
<data name="EntryAddressIsInvalidWarning.Text" xml:space="preserve">
<value>Has to be a valid IPv4 or IPv6 address</value>
</data>
<data name="EntryHostsIsInvalidWarning.Text" xml:space="preserve">
<value>Has to be one or more valid host names separated by spaces</value>
</data>
<data name="UpdateBtn" xml:space="preserve">
<value>Update</value>
</data>
Expand Down

0 comments on commit 76de196

Please sign in to comment.