protected override bool CommonValidation(System.Web.Http.ModelBinding.ModelStateDictionary validationDictionary, Client currentClient, BookingStep entity, object param, params object[] additionalObjects) { BookingStep temp = null; if (entity.BookingStepNext != null) { temp = BookingStepRepository.GetBookingStepById(entity.BookingStepNext.Id, currentClient.Id); } if (temp != null) { if (entity.BookingStepConfig != null && temp.BookingStepConfig.Id != entity.BookingStepConfig.Id) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <BookingStep>(), "BookingStepNext"), GenericError.INVALID_GIVEN_PARAMETER); } if (entity.BookingArchived && !temp.BookingArchived) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <BookingStep>(), "BookingStepNext.BookingArchived"), GenericError.INVALID_GIVEN_PARAMETER); } } if (entity.BookingStepPrevious != null) { temp = BookingStepRepository.GetBookingStepById(entity.BookingStepPrevious.Id, currentClient.Id); } if (temp != null) { if (entity.BookingStepConfig != null && temp.BookingStepConfig.Id != entity.BookingStepConfig.Id) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <BookingStep>(), "BookingStepPrevious"), GenericError.INVALID_GIVEN_PARAMETER); } } return(validationDictionary.IsValid); }
protected override bool ValidatePost(System.Web.Http.ModelBinding.ModelStateDictionary validationDictionary, Client currentClient, MailModel entity, object param, params object[] additionalObjects) { IMailConfigRepository repo = (IMailConfigRepository)additionalObjects[0]; List <string> recipients = (List <string>)additionalObjects[1]; List <Document> attachments = (List <Document>)additionalObjects[2]; MailController.AdditionalRepositories addRepo = (MailController.AdditionalRepositories)param; NullCheckValidation.NullValidation(TypeOfName.GetNameFromType <MailModel>(), new Dictionary <String, Object>() { { "Body", entity.Body }, { "Subject", entity.Subject }, { "Password", entity.Password } }, validationDictionary); if (repo.GetMailConfigById(entity.MailConfigId, currentClient.Id) == null) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <MailModel>(), "MailConfigId"), GenericError.FORBIDDEN_RESOURCE_OR_DOES_NO_EXIST); } if (entity.To.Count == 0) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <MailModel>(), "To"), GenericError.CANNOT_BE_NULL_OR_EMPTY); } try { Encoding.UTF8.GetString(Convert.FromBase64String(entity.Password)); } catch (Exception) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <MailModel>(), "Password"), GenericError.DOES_NOT_MEET_REQUIREMENTS); } foreach (int cur in entity.To) { People p = addRepo.PeopleRepo.GetPeopleById(cur, currentClient.Id); if (p == null) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <MailModel>(), "To"), GenericError.FORBIDDEN_RESOURCE_OR_DOES_NO_EXIST); return(false); } if (p.AcceptMailing == true && p.Email != null) { recipients.Add(p.Email); } } if (entity.Attachments != null) { foreach (int cur in entity.Attachments) { Document d = addRepo.DocumentRepo.GetDocumentById(cur, currentClient.Id); if (d == null) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <MailModel>(), "Attachments"), GenericError.FORBIDDEN_RESOURCE_OR_DOES_NO_EXIST); return(false); } attachments.Add(d); } } return(validationDictionary.IsValid); }
public bool CheckSizeUpload(System.Web.Http.ModelBinding.ModelStateDictionary validationDictionary, DocumentLog log) { if (log == null) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <Document>(), "DocumentLog"), GenericError.FORBIDDEN_RESOURCE_OR_DOES_NO_EXIST); } else if (log.CurrentSize > log.ResourceConfig.LimitBase) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <Document>(), "LimitBase"), GenericError.UPLOAD_LIMIT); } return(validationDictionary.IsValid); }
protected override bool CommonValidation(System.Web.Http.ModelBinding.ModelStateDictionary validationDictionary, Client currentClient, Booking entity, object param, params object[] additionalObjects) { double elapsed; // If DateDeparture - DateArrive < 1 or if the booking time is more than 365 days if (entity.DateArrival != null && entity.DateDeparture != null && ((elapsed = DateUtils.GetElapsedDaysFromDateTimes(entity.DateArrival, entity.DateDeparture)) < 1 || elapsed > MAX_NIGHTS)) { validationDictionary.AddModelError("Dates", GenericError.WRONG_DATA); } if (entity.DateArrival != null && entity.DateDeparture != null && !PeriodUtils.AllCovered(PeriodRepository.GetPeriodByDates((DateTime)entity.DateArrival, (DateTime)entity.DateDeparture, currentClient.Id).ToList(), (DateTime)entity.DateArrival, (DateTime)entity.DateDeparture)) { validationDictionary.AddModelError("Period", GenericError.DOES_NOT_MEET_REQUIREMENTS); } return(validationDictionary.IsValid); }
public void SetModelStateError(System.Web.Http.ModelBinding.ModelStateDictionary modelState) { if (!string.IsNullOrEmpty(error)) { modelState.AddModelError("error", error); } }
/* ** Download Validation */ public bool DownloadValidation(System.Web.Http.ModelBinding.ModelStateDictionary validationDictionary, Client currentClient, Document entity) { String absoluteUrl = entity != null?DocumentUtils.GetFullDocumentUrl(entity.Url) : null; if (entity == null || (((Boolean)entity.IsPrivate) && currentClient == null)) { validationDictionary.AddModelError(TypeOfName.GetNameFromType <Document>(), GenericError.FORBIDDEN_RESOURCE_OR_DOES_NO_EXIST); } else { if (!File.Exists(absoluteUrl)) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <Document>(), "Url"), GenericError.FILE_NOT_FOUND); } } return(validationDictionary.IsValid); }
private IHttpActionResult ReturnCustomError(System.Web.Http.ModelBinding.ModelStateDictionary modleState, CustomValidationException customEx) { foreach (var dicItem in customEx.ErrorCollection) { modleState.AddModelError(dicItem.Key, dicItem.Value); } return(BadRequest(modleState)); }
public bool UploadValidationAfterProvider(System.Web.Http.ModelBinding.ModelStateDictionary validationDictionary, Client currentClient, MultipartFileData file) { if (file == null || String.IsNullOrEmpty(file.Headers.ContentDisposition.FileName)) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <Document>(), "File"), GenericError.FORBIDDEN_RESOURCE_OR_DOES_NO_EXIST); } return(validationDictionary.IsValid); }
/* ** Entity Validations */ protected override bool CommonValidation(System.Web.Http.ModelBinding.ModelStateDictionary validationDictionary, Client currentClient, Document entity, object param, params object[] additionalObjects) { if (entity.Title != null && !extensionFilter.Any(s => entity.Title.EndsWith(s, StringComparison.OrdinalIgnoreCase))) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <Document>(), "Title"), GenericError.DOES_NOT_MEET_REQUIREMENTS); } return(validationDictionary.IsValid); }
protected override bool CommonValidation(System.Web.Http.ModelBinding.ModelStateDictionary validationDictionary, Client currentClient, PeopleBooking entity, object param, params object[] additionalObjects) { RoomBooking rbtmp = null; if (entity.RoomBooking != null) { rbtmp = RoomBookingRepository.GetRoomBookingById(entity.RoomBooking.Id, currentClient.Id); } if (entity.DateBegin != null && entity.DateEnd != null && (entity.DateBegin >= entity.DateEnd || DateUtils.GetElapsedDaysFromDateTimes((DateTime)entity.DateBegin, (DateTime)entity.DateEnd) < 1)) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <PeopleBooking>(), "Dates"), GenericError.DOES_NOT_MEET_REQUIREMENTS); } else if (entity.DateEnd != null && rbtmp != null && entity.DateEnd > rbtmp.DateEnd) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <PeopleBooking>(), "DateEnd"), GenericError.DOES_NOT_MEET_REQUIREMENTS); } return(validationDictionary.IsValid); }
internal static void AddPropertyError(this System.Web.Http.ModelBinding.ModelStateDictionary modelState, ValidationResult result, string propertyAlias) { //if there are no member names supplied then we assume that the validation message is for the overall property // not a sub field on the property editor if (!result.MemberNames.Any()) { //add a model state error for the entire property modelState.AddModelError($"_Properties.{propertyAlias}", result.ErrorMessage); } else { //there's assigned field names so we'll combine the field name with the property name // so that we can try to match it up to a real sub field of this editor foreach (var field in result.MemberNames) { modelState.AddModelError($"_Properties.{propertyAlias}.{field}", result.ErrorMessage); } } }
/* ** Upload Validation */ public bool UploadValidationBeforeProvider(System.Web.Http.ModelBinding.ModelStateDictionary validationDictionary, Client currentClient, Document entity, HttpRequestMessage Request) { if (entity == null) { validationDictionary.AddModelError(TypeOfName.GetNameFromType <Document>(), GenericError.FORBIDDEN_RESOURCE_OR_DOES_NO_EXIST); } else { if (entity.Url != null) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <Document>(), "Url"), GenericError.ALREADY_EXISTS); } if (!Request.Content.IsMimeMultipartContent()) { validationDictionary.AddModelError(TypeOfName.GetNameFromType <Document>(), GenericError.WRONG_DATA); } } return(validationDictionary.IsValid); }
protected override bool CommonValidation(System.Web.Http.ModelBinding.ModelStateDictionary validationDictionary, Client currentClient, RoomBooking entity, object param, params object[] additionalObjects) { if (entity.Booking != null) { tmp = BookingRepository.GetBookingById((int)entity.Booking.Id, currentClient.Id); } if (entity.Room != null) { rtmp = RoomRepository.GetRoomById((int)entity.Room.Id, currentClient.Id); } if (entity.DateBegin != null && tmp != null && (entity.DateBegin < tmp.DateArrival || entity.DateBegin > tmp.DateDeparture || (entity.DateEnd != null && entity.DateBegin >= entity.DateEnd))) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <RoomBooking>(), "DateBegin"), GenericError.DOES_NOT_MEET_REQUIREMENTS); } if (entity.DateEnd != null && tmp != null && (entity.DateEnd < tmp.DateArrival || entity.DateEnd > tmp.DateDeparture || (entity.DateBegin != null && entity.DateEnd <= entity.DateBegin))) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <RoomBooking>(), "DateEnd"), GenericError.DOES_NOT_MEET_REQUIREMENTS); } return(validationDictionary.IsValid); }
/// <summary> /// Adds a generic culture error for use in displaying the culture validation error in the save/publish/etc... dialogs /// </summary> /// <param name="modelState"></param> /// <param name="culture"></param> /// <param name="segment"></param> /// <param name="errMsg"></param> internal static void AddVariantValidationError(this System.Web.Http.ModelBinding.ModelStateDictionary modelState, string culture, string segment, string errMsg) { var key = "_content_variant_" + (culture.IsNullOrWhiteSpace() ? "invariant" : culture) + "_" + (segment.IsNullOrWhiteSpace() ? "null" : segment) + "_"; if (modelState.ContainsKey(key)) { return; } modelState.AddModelError(key, errMsg); }
/// <summary> /// Adds a generic culture error for use in displaying the culture validation error in the save/publish/etc... dialogs /// </summary> /// <param name="modelState"></param> /// <param name="culture"></param> /// <param name="errMsg"></param> internal static void AddCultureValidationError(this System.Web.Http.ModelBinding.ModelStateDictionary modelState, string culture, string errMsg) { var key = "_content_variant_" + culture + "_"; if (modelState.ContainsKey(key)) { return; } modelState.AddModelError(key, errMsg); }
/// <summary> /// Adds the error to model state correctly for a property so we can use it on the client side. /// </summary> /// <param name="modelState"></param> /// <param name="result"></param> /// <param name="parts"> /// Each model state validation error has a name and in most cases this name is made up of parts which are delimited by a '.' /// </param> internal static void AddValidationError(this System.Web.Http.ModelBinding.ModelStateDictionary modelState, ValidationResult result, params string[] parts) { // if there are assigned member names, we combine the member name with the owner name // so that we can try to match it up to a real field. otherwise, we assume that the // validation message is for the overall owner. // Owner = the component being validated, like a content property but could be just an html field on another editor var withNames = false; var delimitedParts = string.Join(".", parts); foreach (var memberName in result.MemberNames) { modelState.AddModelError($"{delimitedParts}.{memberName}", result.ErrorMessage); withNames = true; } if (!withNames) { modelState.AddModelError($"{delimitedParts}", result.ErrorMessage); } }
/// <summary> /// Processes the specified model state. /// </summary> /// <param name="modelState">State of the model.</param> /// <param name="result">The result.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns> public static bool Process(this System.Web.Http.ModelBinding.ModelStateDictionary modelState, IValidationContainer result) { foreach (var r in result.ValidationErrors) { foreach (var e in r.Value) { modelState.AddModelError(r.Key, e); } } return(modelState.IsValid); }
protected override bool ValidatePost(System.Web.Http.ModelBinding.ModelStateDictionary validationDictionary, Client currentClient, Period entity, object param, params object[] additionalObjects) { if (param != null && entity != null && entity.End != null && entity.Begin != null && entity.Days > 0) { List <Period> periodList = PeriodRepository.GetPeriodByDates(entity.Begin, entity.End, currentClient.Id).ToList(); if (PeriodUtils.IsDaysCross(periodList, entity)) { validationDictionary.AddModelError(TypeOfName.GetNameFromType <Period>(), GenericError.ALREADY_EXISTS); } } return(CommonValidation(validationDictionary, currentClient, entity, param, additionalObjects)); }
protected override bool ValidatePost(System.Web.Http.ModelBinding.ModelStateDictionary validationDictionary, Client currentClient, RoomBooking entity, object param, params object[] additionalObjects) { if (!CommonValidation(validationDictionary, currentClient, entity, param, additionalObjects)) { return(false); } if (tmp != null && rtmp != null && RoomBookingRepository.FindRoomBookingForDates(entity.Room.Id, 0, entity.DateBegin == null ? (DateTime)tmp.DateArrival : (DateTime)entity.DateBegin, entity.DateEnd == null ? (DateTime)tmp.DateDeparture : (DateTime)entity.DateEnd, currentClient.Id) != null) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <RoomBooking>(), "RoomId"), GenericError.ALREADY_EXISTS); } return(validationDictionary.IsValid); }
protected override bool CommonValidation(System.Web.Http.ModelBinding.ModelStateDictionary validationDictionary, Client currentClient, KeyGenerator entity, object param, params object[] additionalObjects) { if (!currentClient.IsManager && managerKeyTypes.Any(p => p == entity.KeyType)) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <KeyGenerator>(), "KeyType"), GenericError.FORBIDDEN_RESOURCE_OR_DOES_NO_EXIST); } if (entity.DateExp != null && DateTime.Compare((DateTime)entity.DateExp, DateTime.Now) < 0) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <KeyGenerator>(), "DateExp"), GenericError.DOES_NOT_MEET_REQUIREMENTS); } if (entity.Price != null && entity.Price < 0) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <KeyGenerator>(), "Price"), GenericError.DOES_NOT_MEET_REQUIREMENTS); } if ((entity.ValueType < 0 || entity.ValueType >= EValueType.VALUE_MAX_ENUM)) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <KeyGenerator>(), "ValueType"), GenericError.DOES_NOT_MEET_REQUIREMENTS); } if ((entity.KeyType < 0 || entity.KeyType >= EKeyType.VALUE_MAX_ENUM)) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <KeyGenerator>(), "KeyType"), GenericError.DOES_NOT_MEET_REQUIREMENTS); } return(validationDictionary.IsValid); }
/// <summary> /// Will add an error to model state for a key if that key and error don't already exist /// </summary> /// <param name="modelState"></param> /// <param name="key"></param> /// <param name="errorMsg"></param> /// <returns></returns> private static bool TryAddModelError(this System.Web.Http.ModelBinding.ModelStateDictionary modelState, string key, string errorMsg) { if (modelState.TryGetValue(key, out var errs)) { foreach (var e in errs.Errors) { if (e.ErrorMessage == errorMsg) { return(false); //if this same error message exists for the same key, just exit } } } modelState.AddModelError(key, errorMsg); return(true); }