Skip to content

Instantly share code, notes, and snippets.

@cprieto
Created September 11, 2009 21:25
Show Gist options
  • Save cprieto/185599 to your computer and use it in GitHub Desktop.
Save cprieto/185599 to your computer and use it in GitHub Desktop.
using System;
using System.Text.RegularExpressions;
using FluentValidation;
using Rioshu.Educa.Data;
using Rioshu.Educa.Models;
namespace Rioshu.Educa.Validators
{
public class SignUpFormValidator : AbstractValidator<SignUpForm>
{
const string validCharset = "[^a-zA-Z0-9_.]";
readonly IMemberRepository memberRepository;
public SignUpFormValidator(IMemberRepository memberRepository)
{
Check.Require(memberRepository != null, "Member repository must not be null");
this.memberRepository = memberRepository;
RuleFor(x => x.Username)
.NotEmpty()
.WithMessage("Debe suministrar un nombre de usuario")
.Must(IsValidUsername)
.WithMessage("El nombre de usuario solo puede contener letras, números, punto y/o guión");
RuleFor(x => x.FirstName)
.NotEmpty()
.WithMessage("El nombre no debe estar vacío");
RuleFor(x => x.LastName)
.NotEmpty()
.WithMessage("El apellido no debe estar vacío");
RuleFor(x => x.Email)
.EmailAddress()
.WithMessage("Debe suministrar un correo válido")
.When(x => !string.IsNullOrEmpty(x.Email));
RuleFor(x => x.BirthDate)
.Must((f, b) => b < DateTime.Now.Date.AddDays(-1) && b > DateTime.MinValue)
.WithMessage("Debe suministrar una fecha de nacimiento válida");
RuleFor(x => x.Password)
.Length(4, 15)
.WithMessage("Contraseña debe ser entre 4 y 15 caracteres");
}
static bool IsValidUsername(string username)
{
if (string.IsNullOrEmpty(username)) {
return false;
}
return !Regex.IsMatch(username, validCharset, RegexOptions.Compiled);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment