Is there a way to publish a content without one or more required properties?
I'm using the EditorModelEventManager to edit the properties of a content before they are loaded into the content edit view.
public class EditorModelEvents
{
public void EditorModelEventManager_SendingContentModel(HttpActionExecutedContext sender, EditorModelEventArgs<ContentItemDisplay> e)
{
if (e.Model.ContentTypeAlias == Constants.MyPageAlias)
{
//RemoveValidation(e.Model, Constants.MyPropertyAlias);
RemoveProperty(e.Model, Constants.MyPropertyAlias);
}
}
private void RemoveProperty(ContentItemDisplay content, string propertyAlias)
{
foreach (var variant in content.Variants)
foreach (var tab in variant.Tabs)
tab.Properties = tab.Properties.Where(x => x.Alias != propertyAlias);
}
private void RemoveValidation(ContentItemDisplay content, string propertyAlias)
{
foreach (var variant in content.Variants)
{
var property = GetProperty(variant, propertyAlias);
if (property != null)
{
property.Validation.Mandatory = false;
property.Validation.Pattern = null;
}
}
}
}
With the RemoveValidation method I am able to skip the validation of properties set as mandatory and publish the content without even setting a value. The downside of using this method is that the properties are still visible to the user.
However, if I remove the property from the ContentItemDisplay model with the RemoveProperty method, the content is saved correctly but some validation is performed and the publish action fails: "{content name} could not be published because some properties did not pass validation rules".
Is there something that I can do to publish the content bypassing the property validation?
Without having to remove the validation all together, the easiest way (besides making the field optional instead of required) would be to fill in the required field on the OnPublishing event so that it passes the validation, without the back-office user noticing anything has happened to the field that isn't visible to him/her in the first place! Would this be an option?
It would be a solution for a specific type of content and properties. For each property, I need to know in advance the data type needed and set a value manually in order to pass validation.
I'm looking for a more general solution, that can work potentially with new content types/properties.
Publish a content without a mandatory property
Is there a way to publish a content without one or more required properties? I'm using the
EditorModelEventManager
to edit the properties of a content before they are loaded into the content edit view.With the
RemoveValidation
method I am able to skip the validation of properties set as mandatory and publish the content without even setting a value. The downside of using this method is that the properties are still visible to the user.However, if I remove the property from the
ContentItemDisplay
model with theRemoveProperty
method, the content is saved correctly but some validation is performed and the publish action fails:"{content name} could not be published because some properties did not pass validation rules"
.Is there something that I can do to publish the content bypassing the property validation?
Hi Michele,
Without having to remove the validation all together, the easiest way (besides making the field optional instead of required) would be to fill in the required field on the OnPublishing event so that it passes the validation, without the back-office user noticing anything has happened to the field that isn't visible to him/her in the first place! Would this be an option?
Kind regards,
Corné Hoskam
Hi Corné, thank you for your answer.
It would be a solution for a specific type of content and properties. For each property, I need to know in advance the data type needed and set a value manually in order to pass validation.
I'm looking for a more general solution, that can work potentially with new content types/properties.
is working on a reply...