fodyarchived / validar Goto Github PK
View Code? Open in Web Editor NEWInjects IDataErrorInfo or INotifyDataErrorInfo code into a class at compile time.
License: MIT License
Injects IDataErrorInfo or INotifyDataErrorInfo code into a class at compile time.
License: MIT License
target is not a IValidationContext.
public ValidationTemplate(INotifyPropertyChanged target)
{
this.target = target;
validator = GetValidator(target.GetType());
validationResult = validator.Validate(target); // target is not a IValidationContext.
target.PropertyChanged += Validate;
}
void Validate(object sender, PropertyChangedEventArgs e)
{
validationResult = validator.Validate(target); // target is not a IValidationContext.
foreach (var error in validationResult.Errors)
{
RaiseErrorsChanged(error.PropertyName);
}
}
Is it possible to add Netstandard support? Fody and propertychanged.fody already have it. It would be nice if you can add it for this weaver.
Thanks.
Try as I might, I can't seem to get Validar.Fody and PropertyChanged.Fody to play nicely together. Everything works great except for triggering a PropertyChanged notification on the HasErrors property.
Could this log message be reduced to debug, or removed altogether?
https://github.com/Fody/Validar/blob/master/Fody/ModuleWeaver.cs#L69
Hello,
I'm tring to validate a quite simple model under WPF (in real case I use Caliburn Micro as MVVM framework)
My problem is that I can't disable an confirm button if there're error in the nested model's property
consider this case
public class MainObjectValidator : AbstractValidator<MainObject>
{
public MainObjectValidator()
{
RuleFor(x => x.Child).SetValidator(new ChildObjectValidator());
RuleFor(x => x.Name).NotEmpty();
}
}
[InjectValidation]
public class ChildObject : INotifyPropertyChanged
{
private int age;
public int Age
{
get
{
return this.age;
}
set
{
this.age = value;
NotifyPropertyChanged("Age");
}
}
private void NotifyPropertyChanged(String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class ChildObjectValidator : AbstractValidator<ChildObject>
{
public ChildObjectValidator()
{
RuleFor(x => x.Age).GreaterThan(18);
}
}
and Here's my ViewModel
public class ViewModel : ViewModelBase
{
private MainObject dummy;
public MainObject Dummy
{
get
{
return this.dummy;
}
set
{
this.dummy = value;
OnPropertyChanged(() => Dummy);
}
}
public ViewModel()
{
this.Dummy = new MainObject();
Dummy.Child.Age = 15;
Dummy.Name = "paolo";
CreateLoadCommand();
}
private void CreateLoadCommand()
{
LoadDataCommand = new RelayCommand(this.DoSomething());
}
public ICommand LoadDataCommand
{
get;
internal set;
}
public bool CanLoadDataCommand
{
get
{
return false;
}
}
private Action DoSomething()
{
return () =>
{
var x = Dummy.GetType().GetField("validationTemplate", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.FlattenHierarchy);
var t = (ValidationTemplate)x.GetValue(Dummy);
};
}
}
and my View
<code>
Window x:Class="WPFExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
Title="MainWindow" Height="350" Width="525">
<Grid DataContext="{Binding}">
<Grid.RowDefinitions>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition Height="25"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Content="Name" Grid.Column="0" Grid.Row="0"></Label>
<Label Content="Age" Grid.Column="0" Grid.Row="1"></Label>
<TextBox Text="{Binding Dummy.Name, Mode=TwoWay, ValidatesOnDataErrors=True}" Grid.Row="0" Grid.Column="1"></TextBox>
<telerik:RadMaskedNumericInput Value="{Binding Dummy.Child.Age, Mode=TwoWay, ValidatesOnDataErrors=True}" Grid.Row="1" Grid.Column="1"></telerik:RadMaskedNumericInput>
<Button Content="Ok" Grid.Row="2" Grid.ColumnSpan="2" Command="{Binding LoadDataCommand}"></Button>
</Grid>
</Window>
</code>
Even if on screen Age field get's red I can't disable the button, if I look at the value of
var t = (ValidationTemplate)x.GetValue(Dummy);
t.HasError
it's always true
I've used ValidationTemplate present in your homepage
If I am use custom attribute and InjectValidationAttribute, i get runtime exception
If I use a custom attribute with an InjectValidationAttribute, an exception occurs at run time, when the following code is executed:
public ValidationTemplate(T target)
{
var attributes = target.GetType().GetCustomAttributes(false).ToList();
}
Exception:
An exception of type 'System.TypeLoadException' occurred in mscorlib.dll but was not handled in user code
Additional information: Could not load type "Validar.InjectValidationAttribute" from assembly "PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Nuget package for Validar can't be used in StrongNamed Project because it must be signed.
Please add strong name in next version
It's written in the project page that I should mark my class with InjectValidation attribute but it's not there ... I downloaded the library via NuGet and there were no refrence added (just the after build task)
[ImplementPropertyChanged, InjectValidation]
public sealed class MyClass : INotifyPropertyChanged
{
creates the following static code analysis errors:
Microsoft.Design : ''MyClass.IDataErrorInfo.Error.get()'' is a virtual member declared in a sealed type.
Mark the member non-virtual or, if the type should be extensible, unseal it.
(same error repeated for all other interfaces members)
It seems only the UI can get the error info. in a mvvm scenario,how viewmodel get the validation info? only by reflection?
eg. create a new user, but the username is invalid. although the UI show the notification, the register button is valid. The button's event handler should inspect the model's validation info.At this time,I can't get the info I want,because when I code, the code doesn't generated. I should use reflection or validate the model again?
Hi @SimonCropp, First thanks for the library.
Definitely [InjectValidation]
works great, saves time and makes class more readable.
However when I would like to access INotifyDataErrorInfo members on my view-model in another method, its not possible now. because INotifyDataErrorInfo members are implemented at build time by [InjectValidation]
.
For Example,
public class ValidationTemplate : INotifyDataErrorInfo // General Validation Template
{
// Implementation of INotifyDataErrorInfo
}
[InjectValidation]
Public class User : INotifyPropertyChanged // View Model
{
[MaxLength(20)]
public string Name{ get; set; }
internal bool IsError() // Internal method required
{
return HasErrors; // Error since HasErrors implemented at build time by [InjectValidation]
}
}
This issues may be similar to #5
Hi. Thanks for the library. One question. When user is done with input on the form, saves the data and then I have to clear, or reset bindings in ViewModel for the next use, validations pops up right from the start on showing form again. I get that this can be expected since now validation is failing, but there should be a way to say - this is clean state, track the input and validate again. I don't see an option to somehow reset or clear validation errors as well, without calling validation template, which beats the purpose of Inject validation if you ask me.
I'm using Validar + FluentValidation nuget package.
Validar : 1.8.0
FluentValidation 8.6.2
WPF application running on top of .NET CORE 3.1
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.