nsabiyera / Oak

Frictionless development for ASP.NET MVC single page web apps. Prototypical and dynamic capabilities brought to C#.
http://amirrajan.github.com/Oak
MIT License
6 stars 7 forks source link

GuidNotEmpty : Validation #15

Open jakejscott opened 11 years ago

jakejscott commented 11 years ago
public class GuidNotEmpty : Validation
{
  public GuidNotEmpty(string property) : base(property)
  {
  }

  public override void Init(dynamic entity)
  {
    base.Init(entity as object);

    if (string.IsNullOrEmpty(ErrorMessage)) ErrorMessage = Property + " is required.";
  }

  public bool Validate(dynamic entity)
  {
    var value = PropertyValueIn(entity);

    return value != Guid.Empty;
  }
amirrajan commented 11 years ago

Keep in mind that the ErrorMessage can be a string or it can be a delegate that returns a string. You'll want to put a null check to avoid the invalid argument exception: https://github.com/amirrajan/Oak/blob/master/Oak/Validation.cs#L267

jakejscott commented 11 years ago

Oh okay thanks!

amirrajan commented 11 years ago

You may also look at the Exclusion validator. You could exclude guid.empty and null values maybe.