-
Hi, I am very new to Blazor and Blazorise (but I really like this component package), and I am currently learning how to use the DataGrid. I want to use DataAnnotations for Validations but I can't seem to make it work in the DataGrid Columns...my code looks like this:
I think it's because I am using the Text and TextChanged or Value and ValueChanged of the input components? But I don't know how to use @bind-Text in the DataGridColumn...please help...🙏🙏🙏 Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Sorry for the confusion, I don't know how to delete my posted question... |
Beta Was this translation helpful? Give feedback.
-
Hi All New to open source and blazor, but i saw this post about data annotations. public class Annotations
{
public void InitialiseColumn<T>(ref T Record, string FieldName, object Value)
{
var a = Record.GetType().GetProperties();
var dstT = Record.GetType();
var dstF = dstT.GetField(FieldName);
if (dstF!=null)
{
dstF.SetValue(Record, Value);
}
else
{
var dstP = dstT.GetProperty(FieldName);
if (dstP != null)
{
dstP.SetValue(Record, Value);
}
}
}
public List<ValidationResult> CheckAnnotation<T>(T Record, string FieldName, object Value)
{
InitialiseColumn(ref Record, FieldName, Value);
var results = new List<ValidationResult>();
var ctx = new ValidationContext(Record);
ctx.MemberName = FieldName;
try
{
var valid = Validator.TryValidateProperty(Value, ctx, results);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
return results;
}
}
} Then used a field validation to implement validations as follows:- public void ValidateSurname(ValidatorEventArgs e)
{
var Record = new ACCOUNT_DATASET_WITH_ACCESS();
var Validator = new Annotations();
var errors = Validator.CheckAnnotation(Record, "US_SURNAME", e.Value ?? "");
foreach (var error in errors)
{
if (String.IsNullOrEmpty(e.ErrorText))
{
e.ErrorText = e.ErrorText + " and " + error.ToString();
}
else
{
e.ErrorText = error.ToString();
}
}
if (errors.Count != 0)
{
e.Status = ValidationStatus.Error;
}
else
{
e.ErrorText = "All Good";
e.Status = ValidationStatus.Success;
}
} Which seems to do the trick well. Sanguinal |
Beta Was this translation helpful? Give feedback.
Sorry for the confusion, I don't know how to delete my posted question...
DataGrid DataAnnotations Validation is currently not yet supported...