Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextBox Regex Extension #799

Merged
merged 18 commits into from
Jan 25, 2017
Merged

Conversation

IbraheemOsama
Copy link
Member

referencing issue #753 for creating a validation regex extension for textbox.
As usual documentation is on the way :)

Copy link
Contributor

@shenchauhan shenchauhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small change, and some docs needed otherwise it looks good to go. Nice work, I can't wait to use it!


public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please convert back too.

<StackPanel Grid.Row="2"
Margin="10,0,10,0">
<TextBox Name="EmailValidatorForce"
controls:TextBoxRegexEx.Regex="^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider making an enum option too. Regex.Email, Regex.Phone etc etc. 

Style="{StaticResource TextBoxRegexExStyle}"
Text="" />
<StackPanel Orientation="Horizontal">
<TextBlock Text="Validation Result: " />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the text to "Is Valid", or "Valid Value" or similar. With "Validation Result" I would expect values of "Valid" and "Invalid"

<TextBox Name="PhoneNumberValidatorForce"
controls:TextBoxRegexEx.ValidationMode="Forced"
controls:TextBoxRegexEx.ValidationType="PhoneNumber"
Header="Text box with ValidationType= PhoneNumber , validation occurs on textbox changed and force occurs on lose focus with ValidationMode=Force"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix spacing before and after PhoneNumber

Header="Text box with Regex extension for phone number, validation occurs on textbox changed"
HeaderTemplate="{StaticResource HeaderTemplate}"
Style="{StaticResource TextBoxRegexExStyle}"
Text="" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the empty text setter

<StackPanel Margin="10,0,10,0">
<TextBox Name="PhoneNumberValidator"
controls:TextBoxRegexEx.Regex="^\s*\+?\s*([0-9][\s-]*){9,}$"
Header="Text box with Regex extension for phone number, validation occurs on textbox changed"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be "validation occurs on TextChanged."

<TextBox Name="PhoneNumberValidatorForce"
controls:TextBoxRegexEx.ValidationMode="Forced"
controls:TextBoxRegexEx.ValidationType="PhoneNumber"
Header="Text box with ValidationType= PhoneNumber , validation occurs on textbox changed and force occurs on lose focus with ValidationMode=Force"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be "validation occurs on TextChanged and"

/// </summary>
/// <param name="obj">TextBox Control</param>
/// <returns>Regex Value</returns>
public static string GetRegex(DependencyObject obj)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

method parameter for the get and set methods should be TextBox

}

/// <summary>
/// Gets ValidationMode property
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change comments to follow pattern in previous comments

private const string PhoneNumberRegex = @"^\s*\+?\s*([0-9][\s-]*){9,}$";
private const string CharactersRegex = "^[A-Za-z]+$";

private static void TextBoxRegexExPropertyOnChange(DependencyObject sender, DependencyPropertyChangedEventArgs e)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure all occurrences of TextBoxRegexEx are replaced with TextBoxRegex

textbox.TextChanged -= Textbox_TextChanged;
textbox.Loaded += Textbox_Loaded;
textbox.LostFocus += Textbox_LostFocus;
textbox.TextChanged += Textbox_TextChanged;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should attempt to validate whenever the expression changes

regex = textbox.GetValue(RegexProperty) as string;
if (string.IsNullOrWhiteSpace(regex))
{
throw new ArgumentException("Regex property can't be null or empty when custom mode is selected");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just set IsValid to true and return. Don't think this warrants an exception

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not exception is thrown then nothing will indicate there is something wrong :) if the user got to this scenario then he is making something wrong and that's why we should give a warning.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put a message to the output/debug window. Same as the way failed bindings do

@skendrot
Copy link
Contributor

Missed one: The number/decimal regex should probably allow for optional negative sign at the front

/// TextBoxRegex allows text validation using a regular expression.
/// </summary>
/// <remarks>
/// If<see cref="ValidationMode"> is set to Normal then IsValid will be set according to either the regex is valid.</see>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure you proof my comments 😛
"set according to whether the regex is valid."

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the current written comment is right :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also need a space after the first word 'If'

/// <summary>
/// The regex expression that will be validated on the textbox
/// </summary>
public static readonly DependencyProperty RegexProperty = DependencyProperty.RegisterAttached("Regex", typeof(string), typeof(TextBoxRegexEx), new PropertyMetadata(null, TextBoxRegexExPropertyOnChange));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the pattern above is for attached properties. See Grid.Row:
//
// Summary:
// Identifies the Grid.Row XAML attached property.
//
// Returns:
// The identifier for the Grid.Row XAML attached property.

GetRow and SetRow then have their comments of "Gets... " and "Sets..."

/// </remarks>
public partial class TextBoxRegex
{
/// <summary>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

summary tags for attached dependency properties should follow the format

        /// <summary>
        /// Identifies the PropertyNameHere attached dependency property.
        /// </summary>

/// </summary>
/// <param name="obj">TextBox to get Regex property from.</param>
/// <returns>The regular expression assigned to the TextBox</returns>
public static string GetRegex(DependencyObject obj)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method parameter for the Get* and Set* methods should be a TextBox

public static readonly DependencyProperty ValidationTypeProperty = DependencyProperty.RegisterAttached("ValidationType", typeof(ValidationType), typeof(TextBoxRegex), new PropertyMetadata(ValidationType.Custom, TextBoxRegexPropertyOnChange));

/// <summary>
/// Gets the Regex property
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gets the value of the TextBoxRegEx.RegEx XAML attached property from the specified TextBox.

Following pattern from framework team. See Grid.Row, RelativePanel.Above, TooltipService.Tooltip, etc.

}

/// <summary>
/// Set Regex property
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sets the value of the TextBoxRegEx.RegEx XAML attached property for a target TextBox.

Following pattern set by framework team

}

/// <summary>
/// Gets a value indicating if the Text is valid according to the Regex property.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change any other Get* Set* summary tags to match pattern set by framework team

@deltakosh deltakosh added this to the v1.3 milestone Jan 23, 2017
@deltakosh
Copy link
Contributor

Flagging this one as 1.3 as I feel we are close

@IbraheemOsama
Copy link
Member Author

@skendrot next time will not tell you that I'm doing a PR
lol

@deltakosh deltakosh merged commit 30d0da9 into CommunityToolkit:dev Jan 25, 2017
@deltakosh
Copy link
Contributor

Congrats!

@IbraheemOsama IbraheemOsama deleted the RegexValidator branch January 25, 2017 22:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants