Validation fixes are left for dessert. It's entirely possible to build great validation implementation into a site and never use fixes, but they make a perfect finishing touch.
Show me the Fixes
The setup: the Title field has a max length validator. If the field value is longer than 40 symbols, the validator displays an error even before the item is saved (no refresh required)
Now right click the red square in the validator bar:
Click "Trim" and the field value gets cut back to 40 symbols, and again - save operation is not required, content editor doesn't refresh.
How to Make One
Now back to our business requirement: we need to make sure no one spells Sitecore as "SiteCore". In part 3 we made a validator that checks for this situation, but it's also fairly easy to fix the spelling without human involvement.
Step 1. Back to the validator definition at /sitecore/system/settings/validation rules/field rules. Under the validator item, create a new item based on the menu item template (as often, its easier to duplicate an existing fix, for example the one under the max length validator). There are two important fields to fill: Display Name and Message.
Step 2. Register the validator:fixsitecore command. This should be familiar if you do any Sitecore client development. Open then /App_Config/commands.config file and add a new command:
<command name="validator:fixsitecore" type="Validators.Actions.FixSitecore, Validators"/>
Step 3. The code. Similar to other client commands, but inherit from Sitecore.Shell.Framework.Commands.ContentEditor.Validators.ValidatorCommand. Key points are noted in the comments:
public class FixSitecore : ValidatorCommand {
public override void Execute(CommandContext context) {
// gets the validator. use it to access validator parameters, if needed.
var validator = GetValidator(context);
if (validator == null) {
return;
}
// get the field web control that we want to interact this. This really
// depends on the way the content editor field is implement. Remember
// that we deal with a simple single-line text field in this example.
var rawControl = GetControlToValidate(validator);
if (rawControl == null) {
return;
}
var control = rawControl as Sitecore.Web.UI.HtmlControls.Control;
if (control == null) {
return;
}
// The actual fix logic
control.Value = control.Value.Replace("SiteCore", "Sitecore");
// Re-run the validation, so that red light turns to green in the UI.
Validate();
}
}
The hardest part is having to deal with asp.net control structure. We cannot provide the familiar Sitecore item and field API here because validator bar validation runs in real time before the item is saved. This means that the field value might not even exist in the database, and is only available on the UI level.
This means that most fixes are tied to the field implementation. Changing the text field value is fairly easy. To do the same for the rich-text field, a different approach is required: javascript is probably the way to go, because rich text field embeds editor in an iframe.
Step 4. The result:
Summary
While not all validation fixes are straight-forward to implement, they can provide that finishing touch you need for a perfect validation system or a great demonstration.
This concludes the series of posts about new validation features implemented in Sitecore 6, hope you can put it to a good use soon.
Part 1: Introduction, configuration, validation types.
Part 2: Error levels, built-in validators.
Part 3: Making a custom validator.
Part 4: Making a validator fix action.