In an edit form, I want to generate/update the filename of a document based on another field in the form.
How to set the filename value after the other field changed ? At this point, I don't have a reference to the FileLeafRef field.
Please note that I want to preserve native rendering, so my custom render method only adds validator, before letting the original render do the job.
Here's what I tried (used a custom validator to hook the change):
(function () {
var registerDocumentTypeToFilename = function () {
var overrideCtx = {};
overrideCtx.Templates = {};
overrideCtx.Templates.Fields = {};
var myCustomValidator= function (originalFormCtx,ctx) {
myCustomValidator.prototype.Validate = function (value) {
console.log(value, originalFormCtx, ctx);
// This line does nothing (neither set the field nor error)
originalFormCtx.updateControlValue("FileLeafRef", value);
return new SPClientForms.ClientValidation.ValidationResult(false, null);
};
};
var myCustom= function (ctx) {
var formCtx = SPClientTemplates.Utility.GetFormContextForCurrentField(ctx);
var fieldValidators = new SPClientForms.ClientValidation.ValidatorSet();
fieldValidators.RegisterValidator(new myCustomValidator(formCtx,ctx));
formCtx.registerClientValidator(formCtx.fieldName, fieldValidators);
return SPFieldLookup_Edit(ctx);
}
overrideCtx.Templates.Fields = {
"myCustomLookupField": { 'NewForm': myCustom, 'EditForm': myCustom}
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
};
/* MDS and non MDS plumbing*/
RegisterModuleInit("corp/myCustom.js", registerDocumentTypeToFilename); // CSR-override for MDS enabled site
registerDocumentTypeToFilename(); //CSR-override for MDS disabled site (because we need to call the entry point function in this case whereas it is not needed for anonymous functions)
})();