How can I make this field required so that a user can't upload an image without the alternate text box having a value?
-
I wondered this same thing and came across this article - mskutta.github.io/2019/11/18/… I hope this is helpful. Thanks!– Garret MulderCommented Mar 25 at 13:39
-
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.– AnonymousCommented Mar 25 at 13:44
3 Answers
Another way is to Set Default Alternate Text on Images Uploaded
to the Sitecore Media Library, of course, it is not a sort of validation but it would be a good alternative rather than blocking the content author each time for alt text.
<getMediaCreatorOptions>
pipeline processor to set default alternate text on an image Item during upload:
using Sitecore.Diagnostics;
using Sitecore.Pipelines.GetMediaCreatorOptions;
namespace Sitecore.Sandbox.Pipelines.GetMediaCreatorOptions
{
public class SetDefaultAlternateTextIfNeed
{
public void Process(GetMediaCreatorOptionsArgs args)
{
Assert.ArgumentNotNull(args, "args");
if (!string.IsNullOrWhiteSpace(args.Options.AlternateText))
{
return;
}
args.Options.AlternateText = GetAlternateText(args);
}
protected virtual string GetAlternateText(GetMediaCreatorOptionsArgs args)
{
Assert.ArgumentNotNull(args, "args");
if (string.IsNullOrWhiteSpace(args.Options.Destination) || args.Options.Destination.IndexOf("/") < 0)
{
return string.Empty;
}
int startofNameIndex = args.Options.Destination.LastIndexOf("/") + 1;
return args.Options.Destination.Substring(startofNameIndex);
}
}
}
Patch file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<getMediaCreatorOptions>
<processor type="Sitecore.Sandbox.Pipelines.GetMediaCreatorOptions.SetDefaultAlternateTextIfNeed, Sitecore.Sandbox"/>
</getMediaCreatorOptions>
</pipelines>
</sitecore>
</configuration>
Reference and Credit: Set Default Alternate Text on Images Uploaded
Hope it helps!
You can achieve this using Field Validation Rules.
Out of the box, Sitecore has a validation rule called Alt Required. Which you can find in the content editor here: /sitecore/system/Settings/Validation Rules/Field Rules/System/Alt Required
If you want the validation rule to stop people from being able to save the new image item, then you need to change this rule to fire a Fatal Error rather than just a Warning.
You can do this by updating the Parameters field to this value:
Text=The Alt field is empty.&Result=FatalError
With that in place, you need to access the Alt field from the Image Base template: /sitecore/templates/System/Media/Unversioned/Image/Image/Alt
Here you find the rule has already been added to Validator Bar. You should add it to the to the other fields (Quick Action Bar & Validate Button) as well as this will allow people to find out what is invalidating the item. Also workflow if you are using them at all.
Now when a user tries to upload an image and forgets to add an Alt Tag, they will get the following alert blocking them from saving the item.
They can find out what is invalidating the item, by Navigating to REVIEW => Validation
The dialog window will show that the Has required alt text is causing a fatal error, blocking the save process.
-
Thanks! But this does it on save which I've already done. My issue is making sure that before you're allowed to upload a file you must have set alt text (which is a field you can set in the experience editor before a file is uploaded) see screenshot.– PinpahoCommented Jun 20, 2023 at 14:10
You can follow below mentioned steps and try:
1.Set up validation rules for the Alt field on Image items.
2.Configure the validation rule to show a "Fatal Error" if alt text is empty.
3.Prevent saving Image items with empty alt text by setting the validation result to "Fatal Error".
4.Create a custom workflow with a Validation Action to catch validation errors during the Publish Command.
5.Configure the workflow to subject the alt text field to validation.
Reference Link - https://mskutta.github.io/2019/11/18/sitecore-how-to-enforce-required-alt-text-on-image-items/