Validation and Data storing in one step
-
I am reading file and after validation i am saving data in properties Now i was hoping to use properties for validation and then save if validation succeed for e.g.
const string AN2Mandatory = @"[a-zA-Z@$^�-9\s\S]{2}";
const string AN70Optional = @"[a-zA-Z@$^�-9\s\S]{70}";these are for validations and these are properties
public string VersionNumber { get; set; }
public string ClientName { get; set; }this is sample logic
Match m = Regex.Match(Line, AN70Optional);
VersionNumber = m.Value;can i apply attributes or something so that while puting value in property my validation is evaluated first and then fill property. instead of validating separately and then storing it
-
I am reading file and after validation i am saving data in properties Now i was hoping to use properties for validation and then save if validation succeed for e.g.
const string AN2Mandatory = @"[a-zA-Z@$^�-9\s\S]{2}";
const string AN70Optional = @"[a-zA-Z@$^�-9\s\S]{70}";these are for validations and these are properties
public string VersionNumber { get; set; }
public string ClientName { get; set; }this is sample logic
Match m = Regex.Match(Line, AN70Optional);
VersionNumber = m.Value;can i apply attributes or something so that while puting value in property my validation is evaluated first and then fill property. instead of validating separately and then storing it
I can offer you two ways... 1 - Put the validation into the property setter (simple). 2 - Write (or find) an attribute that gets the regex as parameter and validates the property decorated with (much powerful).
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
-
I can offer you two ways... 1 - Put the validation into the property setter (simple). 2 - Write (or find) an attribute that gets the regex as parameter and validates the property decorated with (much powerful).
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
-
Great - when you ready made it into an article to share with all!!!
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
-
Great - when you ready made it into an article to share with all!!!
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
This is a win form application i have created a class public class ValidateProperty : Attribute { string _pattern; bool _mandatory; public ValidateProperty(string Pattern, bool Mandatory = false) { _pattern = Pattern; _mandatory = Mandatory; } public override bool Match(object obj) { Match m = Regex.Match((string)obj, _pattern); if (_mandatory == true) { if (string.IsNullOrWhiteSpace(m.Value)) return false; else { return true; } } else return true; } } and then applied on property [ValidateProperty(@"[a-zA-Z@$^�-9\s\S]{70}",true)] public string ClientName { get; set; } but it is not validating when i put a string with less than 70 char. even in debug mode code is not going to match function
-
This is a win form application i have created a class public class ValidateProperty : Attribute { string _pattern; bool _mandatory; public ValidateProperty(string Pattern, bool Mandatory = false) { _pattern = Pattern; _mandatory = Mandatory; } public override bool Match(object obj) { Match m = Regex.Match((string)obj, _pattern); if (_mandatory == true) { if (string.IsNullOrWhiteSpace(m.Value)) return false; else { return true; } } else return true; } } and then applied on property [ValidateProperty(@"[a-zA-Z@$^�-9\s\S]{70}",true)] public string ClientName { get; set; } but it is not validating when i put a string with less than 70 char. even in debug mode code is not going to match function
{70} doesn't mean exactly 70, but up to 70!!! The correct format of {} is {m,n} where m is from and n is to... When you use only one number it interpreted as {0,n}. For exactly 70 characters try {70,70}!!!
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
-
{70} doesn't mean exactly 70, but up to 70!!! The correct format of {} is {m,n} where m is from and n is to... When you use only one number it interpreted as {0,n}. For exactly 70 characters try {70,70}!!!
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
-
yeah that's fine but issue is that it is not fired even if i pass null or less than 70 char less than 70 so it should return false or should not accept value in that property. but currently it is not happenin
You attribute's Match method got hit?
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
-
You attribute's Match method got hit?
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
-
Go and read this: http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute(v=vs.110).aspx[^] It may also bee of interest: http://msdn.microsoft.com/en-us/library/ff649907.aspx[^] http://msdn.microsoft.com/en-us/library/ee707335(v=vs.91).aspx[^] And also read some regex - yours is a mess...
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
-
Go and read this: http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute(v=vs.110).aspx[^] It may also bee of interest: http://msdn.microsoft.com/en-us/library/ff649907.aspx[^] http://msdn.microsoft.com/en-us/library/ee707335(v=vs.91).aspx[^] And also read some regex - yours is a mess...
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
this is good thanks but data i will have is something like this RA01XYZ 201401231445012345611012345678998765432101234DESTINATIONNAMESIZEIS70 RA is one field 01 is one field XYZ is on and so on so i will have to extract and then validate any suggestions
-
this is good thanks but data i will have is something like this RA01XYZ 201401231445012345611012345678998765432101234DESTINATIONNAMESIZEIS70 RA is one field 01 is one field XYZ is on and so on so i will have to extract and then validate any suggestions
With regex you can validate without breaking it apart...
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
-
With regex you can validate without breaking it apart...
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
-
yeah but then we will have to validate through regex and store it in property separately i guess this is what they call catch 22 ;)
I think not. You should pass the regex as a property to your new attribute...
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
-
I think not. You should pass the regex as a property to your new attribute...
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
that's what i am doing but see i need to first extract validate store [RegexValidator(@"[a-zA-Z@$^�-9\s\S]{2}", ErrorMessage = "ok")] public string RecordType { get; set; } [RegexValidator(@"[a-zA-Z@$^�-9\s\S]{2}",ErrorMessage="ok1")] public string VersionNumber { get; set; } [StringLengthValidator(1, 50, MessageTemplate = "Last Name must be between 1 and 70 characters")] public string ClientName { get; set; } ----------------- string Line="RA01XYZ 201401231445012345611012345678998765432101234DESTINATIONNAMESIZEIS70 SENDINGENTITYIDENTIFIER SENDERNAMESIZE70 "; p.RecordType = Line; p.VersionNumber = Line; Now issue is that it will always start from 1 character it will not increment RecordType will start from 1 to 2 so VersionNumber should start from 3 to 4 and ClientName from 5 to 50 i guess i'll try mentioning this using StringLength validator but then datatype validation will be a case
-
that's what i am doing but see i need to first extract validate store [RegexValidator(@"[a-zA-Z@$^�-9\s\S]{2}", ErrorMessage = "ok")] public string RecordType { get; set; } [RegexValidator(@"[a-zA-Z@$^�-9\s\S]{2}",ErrorMessage="ok1")] public string VersionNumber { get; set; } [StringLengthValidator(1, 50, MessageTemplate = "Last Name must be between 1 and 70 characters")] public string ClientName { get; set; } ----------------- string Line="RA01XYZ 201401231445012345611012345678998765432101234DESTINATIONNAMESIZEIS70 SENDINGENTITYIDENTIFIER SENDERNAMESIZE70 "; p.RecordType = Line; p.VersionNumber = Line; Now issue is that it will always start from 1 character it will not increment RecordType will start from 1 to 2 so VersionNumber should start from 3 to 4 and ClientName from 5 to 50 i guess i'll try mentioning this using StringLength validator but then datatype validation will be a case
I see. For that you have to write an other class/method to split the string into fields... You may use these: http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.structlayoutattribute(v=vs.110).aspx[^] http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.fieldoffsetattribute(v=vs.110).aspx[^]
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
-
This is a win form application i have created a class public class ValidateProperty : Attribute { string _pattern; bool _mandatory; public ValidateProperty(string Pattern, bool Mandatory = false) { _pattern = Pattern; _mandatory = Mandatory; } public override bool Match(object obj) { Match m = Regex.Match((string)obj, _pattern); if (_mandatory == true) { if (string.IsNullOrWhiteSpace(m.Value)) return false; else { return true; } } else return true; } } and then applied on property [ValidateProperty(@"[a-zA-Z@$^�-9\s\S]{70}",true)] public string ClientName { get; set; } but it is not validating when i put a string with less than 70 char. even in debug mode code is not going to match function
Just attaching a custom attribute to the property doesn't automatically cause some validation. You must have something that looks for an "appropriate" attribute, using Reflection, typically, and causes the actual validation.
-
I see. For that you have to write an other class/method to split the string into fields... You may use these: http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.structlayoutattribute(v=vs.110).aspx[^] http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.fieldoffsetattribute(v=vs.110).aspx[^]
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
-
Just attaching a custom attribute to the property doesn't automatically cause some validation. You must have something that looks for an "appropriate" attribute, using Reflection, typically, and causes the actual validation.
See here... http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute(v=vs.110).aspx[^]
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
-
See here... http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute(v=vs.110).aspx[^]
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
I see... However, the OP didn't derive from
ValidationAttribute
, they derived directly fromAttribute
:public class ValidateProperty : Attribute
and that's NOT going to work.