Skip to main content
Version: 2.1.4

BaseFormValidator

3p-core v2.1.4


3p-core / BaseFormValidator

Class: BaseFormValidator<T>

Generic base class for implementing form-level validation in SharePoint list forms.

This class supports both:

  • Simple required field validation (used directly)
  • Advanced custom validation (via subclassing)

Examples

// ✅ Basic usage for required field validation only
const validator = new BaseFormValidator(ctx, ['Title', 'StartDate']);
const isValid = validator.validate();
// 🛠️ Extend to apply custom validation rules
class CustomValidator extends BaseFormValidator<MyFields> {
protected validation(): void {
super.validation();
if (this.getFieldValue("StartDate") > this.getFieldValue("EndDate")) {
this.listFormContext.validation.setErrorMessage("EndDate", "End date must be after start date");
}
}
}

Type Parameters

T

T

A shape representing the field keys and their logical mapping (e.g., ProjectFormFields).

Constructors

Constructor

new BaseFormValidator<T>(listFormContext, mandatoryFields, requiredMessage?): BaseFormValidator<T>

Constructs a new validator instance.

Parameters

listFormContext

IListFormContext

The current form context providing access to field values, validation state, and list metadata.

mandatoryFields

keyof T[]

A list of fields (keys of T) that are required and should be validated for non-empty values.

requiredMessage?

string

Optional message to display for required field violations. Defaults to a localized version of "You can't leave this blank".

Returns

BaseFormValidator<T>

Methods

isRequired()

isRequired(key): boolean

Parameters

key

keyof T

Returns

boolean


validate()

validate(args?): boolean

Validates the form

Parameters

args?

T

Validation arguments

Returns

boolean

true if the form doesn't contain validation errors