0

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)
})();

1 Answer 1

0

As a workaround, I can dynamically find the input field:

var myCustomValidator= function (originalFormCtx,ctx) {
    myCustomValidator.prototype.Validate = function (value) {

        var fileLeafRefInput = document.querySelector("#WebPart" + ctx.FormUniqueId + " input[id^='FileLeafRef']");

        fileLeafRefInput.value = "Mynewvalue";

        return new SPClientForms.ClientValidation.ValidationResult(false, null);
    };
};

This is working, but I feel I'm missing something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.