validationResultsFor(model, attribute)

Determines the validation results for an attribute of an Obj or a Widget.

Scrivito.validationResultsFor(myPage, "title");

This function accepts a model, an Obj or a Widget, alongside with an attribute name. It evaluates the provided validations for the model and the attribute. The return value is an array of objects containing the validation result messages and the severity levels. If there are no validation results, the array is empty.

Params

  • model (Obj or Widget) – Obj or Widget to be validated.
  • attribute (String) – the name of the attribute to be validated.

Returns

Object[] – an array of validation results. A validation result is an object with two keys, message and severity. The message is a string containing the message returned by the corresponding validation. The severity is a string denoting the severity of the validation result and can have one of the following values: "error", "warning" and "info". Note that only the results of validations returning a truthy value are included.

Remarks

This function needs to load data, which means that it can only be used in connected components or in combination with Scrivito.load.

Example

Render the list of results for the subtitle attribute in a Scrivito extension:

Scrivito.registerComponent("SubtitleTab", ({ content }) => (
  <>
    <Scrivito.ContentTag content={content} attribute="subtitle" />

    {Scrivito.validationResultsFor(content, "subtitle").map(result => (
      <span
        key={`${result.severity}${result.message}`}
        className={`validation-${result.severity}`}
      >
        {result.message}
      </span>
    ))}
  </>
));