0

Does this property have any verification for itself by default? Like Max. length, etc.? If it does, where can it be found?

I'm asking, because i'm intending to override the UserName property to add more verification. Or should i add the verification in every ViewModel?

Jo Smo
  • 6,923
  • 9
  • 47
  • 67
  • Why not add DataAnnotations to your Model/ViewModel? –  Jul 06 '15 at 11:35
  • I'm trying to do that, but i don't know if there already exist some `DataAnnotations`? And if they do, i want to know which ones they are, so that i can add them again in the override property for `UserName`. – Jo Smo Jul 06 '15 at 11:41
  • I think that i will create a `ViewModel` from which all others will inherit the custom `UserName` property. – Jo Smo Jul 06 '15 at 11:48
  • I have edited my answer. –  Jul 06 '15 at 11:51

2 Answers2

2

I believe the best way for you to enforce the change is at the core, i.e. at the model ApplicationUser level.

modelBuilder.Entity<ApplicationUser>().Property(t => t.Name).HasMaxLength(10);

This way the database is in sync with your view validations and future controllers are scaffolded correctly.

Krishna
  • 2,451
  • 1
  • 26
  • 31
  • Looks promising. :) What's `modelBuilder` in your case? The dbcontext? – Jo Smo Jul 06 '15 at 12:03
  • modelBuilder was the name of the argument for DbModelBuilder within onModelCreating method of a class derived from dbContext https://msdn.microsoft.com/en-us/data/jj591617.aspx – Krishna Jul 06 '15 at 12:17
  • That's using the `Fluent API`. Any idea how to do it with `DataAnnotations`, without erasing the `DataAnnotations` from the parent class (`IdentityUser`)? To just append your own ones? – Jo Smo Jul 06 '15 at 12:49
  • hmmm.... would writing a custom attribute help? http://stackoverflow.com/questions/3607247/dataannotations-dynamically-attaching-attributes – Krishna Jul 06 '15 at 13:51
  • I don't think so... Because then i still have to override the base property. I will have to do it with the Fluent API i guess. Thanks. – Jo Smo Jul 06 '15 at 21:44
1

You could just add a Model/ViewModel to handle this for you.

public class LoginViewModel
{
    [Required(ErrorMessage="*")]
    [StringLength(20, MinimumLength=5)]
    public string UserName { get; set; }

    [DataType(DataType.Password)]
    public string Password { get; set; }
}

This way, in your view, you can notify the user that the length etc does not match. Check out DataAnnotations here

EDIT Is this what you are trying to do? I have not tested this code!

public class ApplicationUser : IdentityUser
{
    public string UserName
    { 
        get; 
        set
        {
            //your validation here
        } 
    }
}
  • But then i would have to copy all `DataAnnotations` to every `ViewModel` i have for user. It would be ideal if i could do that on the model which extends the `IdentityUser` class. – Jo Smo Jul 06 '15 at 11:43
  • It is... But you have to override the `UserName` property. As one already exists in the `IdentityUser` class or some which the `IdentityUser` inherits from. But my fear is that if i override the property, that some `DataAnnotations`/verification will be lost. That's the problem. I don't know if there even is some verification/`DataAnnotations` by default for the property `UserName`. – Jo Smo Jul 06 '15 at 11:53