While defining page properties it is possible to make a property mandatory, before it could be published by a content editor. I need a scenario where a property is mandatory only if a checkbox is selected by content editor. For e.g. in attached screenshot SearchTags should only be mandatory if searchable checkbox is selected.
Is there any way of achieving this, I am using umbraco 7.21.
It's not something that is possible to do out of the box since you build up a document type of different property editors that are not aware of each other.
However what you can do instead is to build your own custom property editor, which will consist of your "Searchable" and "Search tags" form fields. Then you can make sure that it's not possible to save and publish the page before at least one search tags has been selected.
You're welcome - Yes it's actually only the creativity (and skills) that sets the limit. I just created a property editor that gives me some data that I manage from a custom section. It's really nice to be able to have this kind of flexibility :)
Old thread but a solution I use is to prefix an alias 'mc_' (mandatory component) to a list of property types and do a check on saving action to see if a value is available in either of those properties.
This allows me to add as many mandatory properties without creating custom property types.
Code below :
public class MandatoryComponents : ApplicationEventHandler
{
/// <summary>
/// alias prefix to check against
/// </summary>
private const string ALIAS_PREFIX = "mc_";
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
ContentService.Saving += MandatoryCheck;
}
/// <summary>
/// Check each property type on a component we are saving for the prefix alias
/// if at least one has a value continue.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void MandatoryCheck(IContentService sender, SaveEventArgs<IContent> e)
{
var mandatoryPropertyList = new List<Property>();
foreach(var entity in e.SavedEntities)
{
foreach(var prop in entity.Properties)
{
if(prop.Alias.StartsWith(ALIAS_PREFIX))
{
mandatoryPropertyList.Add(prop);
}
}
}
if(mandatoryPropertyList.Count < 2)
{
return;
}
else
{
if(mandatoryPropertyList.Any(x => x.Value != null))
{
return;
}
else
{
var properties = string.Join(", ", mandatoryPropertyList.Select(x => x.PropertyType.Name).ToArray());
e.Cancel = true;
e.Messages.Add(new EventMessage("Oops!",
"Please add a value to either " + properties, EventMessageType.Error));
}
}
}
}
Understand this is an older thread but this may be useful.
Mandatory conditional fields in Umbraco backend
While defining page properties it is possible to make a property mandatory, before it could be published by a content editor. I need a scenario where a property is mandatory only if a checkbox is selected by content editor. For e.g. in attached screenshot SearchTags should only be mandatory if searchable checkbox is selected.
Is there any way of achieving this, I am using umbraco 7.21.
Hi Ajit
It's not something that is possible to do out of the box since you build up a document type of different property editors that are not aware of each other.
However what you can do instead is to build your own custom property editor, which will consist of your "Searchable" and "Search tags" form fields. Then you can make sure that it's not possible to save and publish the page before at least one search tags has been selected.
You can learn more about how to build property editors by having a look in the AngularJs Workbook https://github.com/umbraco/AngularWorkbook
Hope this helps.
/Jan
Thanks for pointing me to right direction, infact one can create quite complex editors.
Hi Ajit
You're welcome - Yes it's actually only the creativity (and skills) that sets the limit. I just created a property editor that gives me some data that I manage from a custom section. It's really nice to be able to have this kind of flexibility :)
Happy coding!
/Jan
Old thread but a solution I use is to prefix an alias 'mc_' (mandatory component) to a list of property types and do a check on saving action to see if a value is available in either of those properties.
This allows me to add as many mandatory properties without creating custom property types.
Code below :
Understand this is an older thread but this may be useful.
Nice little hack - thanks for sharing Marc
is working on a reply...