/// <summary> /// Validates the data for each property /// </summary> /// <returns></returns> /// <remarks> /// All property data validation goes into the modelstate with a prefix of "Properties" /// </remarks> protected bool ValidatePropertyData(IDictionary <string, object> postedProperties, TPersisted content) { var hasError = false; if (postedProperties != null) { foreach (var p in postedProperties) { var propertyType = content.PropertyTypes.Single(x => x.Alias == p.Key); var editor = PropertyEditorResolver.Current.GetByAlias(propertyType.PropertyEditorAlias); if (editor == null) { var message = string.Format("The property editor with alias: {0} was not found for property with id {1}", propertyType.PropertyEditorAlias, content.Properties[p.Key].Id); LogHelper.Warn <ContentPropertyValidator <TPersisted> >(message); continue; } //get the posted value for this property var postedValue = p.Value; //get the pre-values for this property var preValues = _dataTypeService.GetPreValuesCollectionByDataTypeId(propertyType.DataTypeDefinitionId); foreach (var result in editor.ValueEditor.Validators.SelectMany(v => v.Validate(postedValue, preValues, editor))) { _modelState.AddPropertyError(result, p.Key); } //Now we need to validate the property based on the PropertyType validation (i.e. regex and required) // NOTE: These will become legacy once we have pre-value overrides. if (propertyType.Mandatory) { foreach (var result in editor.ValueEditor.RequiredValidator.Validate(postedValue, "", preValues, editor)) { hasError = true; _modelState.AddPropertyError(result, p.Key); } } if (propertyType.ValidationRegExp.IsNullOrWhiteSpace() == false) { foreach (var result in editor.ValueEditor.RegexValidator.Validate(postedValue, propertyType.ValidationRegExp, preValues, editor)) { hasError = true; _modelState.AddPropertyError(result, p.Key); } } } } return(!hasError); }
protected virtual bool ValidatePropertyData(ModelStateDictionary modelState, FluidityEntityPostModel postModel, FluidityEditorFieldConfig[] configProps) { foreach (var p in configProps) { var dataTypeInfo = _dataTypeHelper.ResolveDataType(p); var postedValue = postModel.Properties.Single(x => x.Alias == p.Property.Name).Value; // Validate against the prop editor validators foreach (var result in dataTypeInfo.PropertyEditor.ValueEditor.Validators .SelectMany(v => v.Validate(postedValue, dataTypeInfo.PreValues, dataTypeInfo.PropertyEditor))) { modelState.AddPropertyError(result, p.Property.Name); } // Now we need to validate the property based on the PropertyType validation (i.e. regex and required) // NOTE: These will become legacy once we have pre-value overrides. if (p.IsRequired) { foreach (var result in dataTypeInfo.PropertyEditor.ValueEditor.RequiredValidator .Validate(postedValue, "", dataTypeInfo.PreValues, dataTypeInfo.PropertyEditor)) { modelState.AddPropertyError(result, p.Property.Name); } } if (!p.ValidationRegex.IsNullOrWhiteSpace()) { // We only want to execute the regex statement if: // * the value is null or empty AND it is required OR // * the value is not null or empty // See: http://issues.umbraco.org/issue/U4-4669 var asString = postedValue as string; if ( //Value is not null or empty (postedValue != null && asString.IsNullOrWhiteSpace() == false) //It's required || p.IsRequired) { foreach (var result in dataTypeInfo.PropertyEditor.ValueEditor.RegexValidator .Validate(postedValue, p.ValidationRegex, dataTypeInfo.PreValues, dataTypeInfo.PropertyEditor)) { modelState.AddPropertyError(result, p.Property.Name); } } } } return(modelState.IsValid); }
public void Get_Cultures_With_Property_Errors() { var ms = new ModelStateDictionary(); var localizationService = new Mock <ILocalizationService>(); localizationService.Setup(x => x.GetDefaultLanguageIsoCode()).Returns("en-US"); ms.AddPropertyError(new ValidationResult("no header image"), "headerImage", null); //invariant property ms.AddPropertyError(new ValidationResult("title missing"), "title", "en-US"); //variant property var result = ms.GetVariantsWithPropertyErrors("en-US"); //even though there are 2 errors, they are both for en-US since that is the default language and one of the errors is for an invariant property Assert.AreEqual(1, result.Count); Assert.AreEqual("en-US", result[0].culture); }
public override bool ValidatePropertiesData( MemberSave?model, IContentProperties <ContentPropertyBasic>?modelWithProperties, ContentPropertyCollectionDto?dto, ModelStateDictionary modelState) { if (model is null) { return(false); } if (model.Username.IsNullOrWhiteSpace()) { modelState.AddPropertyError( new ValidationResult("Invalid user name", new[] { "value" }), $"{Constants.PropertyEditors.InternalGenericPropertiesPrefix}login"); } if (model.Email.IsNullOrWhiteSpace() || new EmailAddressAttribute().IsValid(model.Email) == false) { modelState.AddPropertyError( new ValidationResult("Invalid email", new[] { "value" }), $"{Constants.PropertyEditors.InternalGenericPropertiesPrefix}email"); } var validEmail = ValidateUniqueEmail(model); if (validEmail == false) { modelState.AddPropertyError( new ValidationResult("Email address is already in use", new[] { "value" }), $"{Constants.PropertyEditors.InternalGenericPropertiesPrefix}email"); } var validLogin = ValidateUniqueLogin(model); if (validLogin == false) { modelState.AddPropertyError( new ValidationResult("Username is already in use", new[] { "value" }), $"{Constants.PropertyEditors.InternalGenericPropertiesPrefix}login"); } return(base.ValidatePropertiesData(model, modelWithProperties, dto, modelState)); }
public void Add_Variant_Segment_Property_Error() { var ms = new ModelStateDictionary(); var localizationService = new Mock <ILocalizationService>(); localizationService.Setup(x => x.GetDefaultLanguageIsoCode()).Returns("en-US"); ms.AddPropertyError(new ValidationResult("no header image"), "headerImage", "en-US", "mySegment"); //variant/segment property Assert.AreEqual("_Properties.headerImage.en-US.mySegment", ms.Keys.First()); }
/// <summary> /// We need to manually validate a few things here like email and login to make sure they are valid and aren't duplicates /// </summary> /// <param name="model"></param> /// <param name="dto"></param> /// <param name="modelState"></param> /// <param name="modelWithProperties"></param> /// <returns></returns> public override bool ValidatePropertyData(MemberSave model, IContentProperties <ContentPropertyBasic> modelWithProperties, ContentPropertyCollectionDto dto, ModelStateDictionary modelState) { if (model.Username.IsNullOrWhiteSpace()) { modelState.AddPropertyError( new ValidationResult("Invalid user name", new[] { "value" }), $"{Constants.PropertyEditors.InternalGenericPropertiesPrefix}login"); } if (model.Email.IsNullOrWhiteSpace() || new EmailAddressAttribute().IsValid(model.Email) == false) { modelState.AddPropertyError( new ValidationResult("Invalid email", new[] { "value" }), $"{Constants.PropertyEditors.InternalGenericPropertiesPrefix}email"); } //default provider! var membershipProvider = Core.Security.MembershipProviderExtensions.GetMembersMembershipProvider(); var validEmail = ValidateUniqueEmail(model, membershipProvider); if (validEmail == false) { modelState.AddPropertyError( new ValidationResult("Email address is already in use", new[] { "value" }), $"{Constants.PropertyEditors.InternalGenericPropertiesPrefix}email"); } var validLogin = ValidateUniqueLogin(model, membershipProvider); if (validLogin == false) { modelState.AddPropertyError( new ValidationResult("Username is already in use", new[] { "value" }), $"{Constants.PropertyEditors.InternalGenericPropertiesPrefix}login"); } return(base.ValidatePropertyData(model, modelWithProperties, dto, modelState)); }