-
Notifications
You must be signed in to change notification settings - Fork 0
Usage
alexanderar edited this page Apr 5, 2015
·
4 revisions
public class UserViewModel
{
public string Name{ get; set; }
public string Email{ get; set; }
public string Password{ get; set; }
public string Phone{ get; set; }
public int Id{ get; set; }
public bool AllowEditPhone {get;set;}
}
public class UserViewModelAnnotations : FluentDataAnnotation<UserViewModel>
{
public UserViewModelAnnotations()
{
For(m => m.Email)
.SetReadOnly(true)
/*Setting data type for the field.
*Similar to using [DataType(DataType.EmailAddress)] attribute.*/
.SetDataType(DataType.EmailAddress)
/*Setting custom format for the field.
*Similar to using [DisplayFormat(DataFormatString = "The email is : {0}")].*/
.SetDisplayFormat("The email is : {0}")
/*Setting display name.
*Similar to using [Display(Name = "Email")] attribute.*/
.SetDisplayName("Email")
For(m => m.Password)
//Setting display name from resources
.SetDisplayName(Account.UserPasswordLabel)
.SetDataType(DataType.Password)
this.For(m => m.Phone)
//Phone field will be editable only when current user is in the specific role
.SetReadOnly(() => !Thread.CurrentPrincipal.IsInRole("AllowEditPhone"))
//Displays the number in the following format original 123456789 => transformed: 123***789
.ApplyValueTransform(s => Regex.Replace(s, @"(?<=\d{3})\d(?=\d{3})", "*", RegexOptions.Compiled));
}
}
Used to apply annotations conditionally based on result of predicate:
The following code will make Phoneproperty read only, in case that AllowEditPhone property is set to false
this.When(model => !model.AllowEditPhone,
() => {
this.For(m => m.Phone).SetReadOnly(false);
});