2

I am a bit confused with the right way of registering custom CSR callbacks for fields on a list form.

While things are pretty straightforward if I am developing a totally custom field control, I am not so sure about the right way of "subclassing" standard SharePoint controls.

For example, it seems that if I call the standard SPFieldNote_Edit function in my control template and then register a custom init callback, the init callback previously registered by SPFieldNode_Edit will be overwritten, effectively breaking the normal operation of the standard control.

Neither am I sure about custom validators: if I invoke a standard control template and then call registerClientValidators, will the standard validators be overwritten with my ClientValidatorSet, or will rather my ClientValidatorSet will be appended to the list?

Unfortunately I couldn't find anything specific in the documentation, and neither there seems to be an Office PnP sample demonstrating proper subclassing of the standard controls.

Perhaps I could find the answers by looking at the internals of clienttemplates.js, but I am not sure if I am supposed to look there (legally, at least).

So any help will be appreciated.

1

I've only done custom validation through CSR once, and aside from my actual custom validation logic, it was based heavily on the code in this example by Martin Hatch. Based on what's happening in that code, here are my suspicions.

I would say that if you use registerClientValidator, you will be overwriting any existing validators for that field. Why do I suspect that?

First of all, in the example code, he starts with new-ing up a new ValidatorSet object:

// register the field validators
var fieldValidators = new SPClientForms.ClientValidation.ValidatorSet();

which ultimately gets registered for the field. Secondly, he checks to see if the field is required, and if so, adds a required field validator:

if (formCtx.fieldSchema.Required) {
  fieldValidators.RegisterValidator(
    new SPClientForms.ClientValidation.RequiredValidator()
  );
}

Then he adds his custom validator, and finally registers the validator set with the field in the form:

formCtx.registerClientValidator(formCtx.fieldName, fieldValidators);

If registerClientValidator appended the validators to any existing ones, why bother checking if the field is required and adding a required field validator?

So my suspicion is that if you use formCtx.registerClientValidator you will be replacing any previously registered validator set.

However, that begs the question, can you somehow retrieve an existing validator set that has been registered for a field, and just append new validators into the existing set? Clearly fieldValidators.RegisterValidator, the way it is used in the example code, is an appending function.

A lot of "unofficial" but highly accurate and useful documentation about how CSR works has come from poring over clienttemplates.debug.js, so that seems like it might be the way to go to get a definitive answer.

  • Excellent! In the foreseeable future, I will hardly need anything beyond the basic required validator from the stock SharePoint code, so your example has anything I need to know. Thanks a lot! – DmytroL Feb 21 '16 at 7:53
  • 1
    That's all good except for the part where it overrides. In fact, when you register a ValidatorSet you can see SharePoint code calls MergeValidators if there are already existing ones. The reason you might need to register the requried validator is if you created a "custom" field rendering, then there are no built-in ones automatically. But if you just want to add another validator to a regular text box (say: validate as email) you won't have to register the required validator in your set. – Shai Petel Apr 25 '18 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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