1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
public class SingleUserModel
{
public string field { get; set; }
public string value { get; set; }
public bool response { get; set; }
public SingleUserModel(string _field, string _value)
{
field = _field;
value = _value;
response = CheckSingleUser(field, value);
}
public static bool CheckSingleUser(string _field, string _value)
{
string procedureToExecute = String.Empty;
if (string.IsNullOrEmpty(_value))
{
return false;
}
if (_field != "Email" && _field != "Login")
{
return false;
}
switch (_field)
{
case "Email":
procedureToExecute = "dbo.BA_Users_GetUserByEmail";
break;
case "Login":
procedureToExecute = "dbo.BA_Users_GetUserByLogin";
break;
}
using (SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["BankAccount"].ConnectionString))
{
sqlConnection.Open();
SqlCommand createUser = new SqlCommand(procedureToExecute, sqlConnection);
createUser.CommandType = CommandType.StoredProcedure;
createUser.Parameters.AddWithValue("@Value", _value);
int result = (int)createUser.ExecuteScalar();
if (result >= 1)
{
return false;
}
return true;
}
}
}
public class SingleUserAttribute : ValidationAttribute
{
public string Field { get; private set; }
public SingleUserAttribute(string field)
{
Field = field;
}
public override bool IsValid(object value)
{
return SingleUserModel.CheckSingleUser(Field, (string)value);
}
}
public class SingleUserValidator : DataAnnotationsModelValidator<SingleUserAttribute>
{
string _field;
string _message;
public SingleUserValidator(ModelMetadata metadata, ControllerContext context
, SingleUserAttribute attribute)
: base(metadata, context, attribute)
{
_field = attribute.Field;
_message = attribute.ErrorMessage;
}
public override IEnumerable<ModelClientValidationRule>
GetClientValidationRules()
{
var rule = new ModelClientValidationRule
{
ErrorMessage = _message,
ValidationType = "field"
};
rule.ValidationParameters.Add("name", _field);
return new[] { rule };
}
}
} |
Partager