Validation and Data storing in one step
-
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.
-
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.
He started with simple attribute, but moved on after this: http://www.codeproject.com/Messages/4746945/Re-Validation-and-Data-storing-in-one-step.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)