c# - WPF Complex Validation -
ok, have wpf application (using mvvm) consisting of view has 2 textboxes: first name , last name.
both of them must consist of letters. i've achieved using attributes on corresponding entity (worker):
[regularexpression(@"^[a-za-z]+$", errormessage = "first name must consist of letters only.")] public string firstname [regularexpression(@"^[a-za-z]+$", errormessage = "last name must consist of letters only.")] public string lastname
and works great. but...i need have following validation rule: @ least 1 of fields: firstname or lastname must filled.
any ideas on how implement validation involving 2 fields?
the expected result is: if non of fields filled, validation msg appear beside firstname textbox: @ least first name or last name must filled. same message appear near the last name textbox. once fill 1 of fields, both messages disppear.
another challenge, if enter digit in first name textbox, want error message on first name textbox: first name must consist of letters only. , want error of @ least 1 of fields must filled (near both of textboxes) dissapear.
thanks!
you'd better off implementing idataerrorinfo
interface, or if you're using .net 4.5, newer inotifydataerrorinfo
interface. using idataerrorinfo
interface example, you'd need implement indexer data type class , in indexer, can define complicated rules can think of:
public override string this[string propertyname] { { string error = string.empty; if ((propertyname == "firstname" || propertyname == "lastname") && (firstname == string.empty || lastname == string.empty)) error = "you must enter either first name or last name fields."; return error; } }
Comments
Post a Comment