Skip to content

Commit 83f598d

Browse files
committed
Merge branch 'geltapatio-602-Pnp-Taxonomy-Picker-onload-validation' into dev
2 parents 9376d83 + df16dd5 commit 83f598d

File tree

5 files changed

+62
-5
lines changed

5 files changed

+62
-5
lines changed

docs/documentation/docs/controls/TaxonomyPicker.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ The TaxonomyPicker control can be configured with the following properties:
164164
| anchorId | string | no | Set the anchorid to a child term in the TermSet to be able to select terms from that level and below. |
165165
| termActions | ITermActions | no | Allows to execute custom action on the term like e.g. get other term labelsITermActions. |
166166
| hideTagsNotAvailableForTagging | boolean | no | Specifies if the tags marked with 'Available for tagging' = false should be hidden |
167+
| validateOnLoad | boolean | no | Specifies if the initial values will be validated, when the component is loaded. Default value is false |
167168
| hideDeprecatedTags | boolean | no | Specifies if deprecated tags should be hidden |
168169
| placeholder | string | no | Short text hint to display in empty picker |
169170
| errorMessage | string | no | Static error message displayed below the picker. Use `onGetErrorMessage` to dynamically change the error message displayed (if any) based on the current value. `errorMessage` and `onGetErrorMessage` are mutually exclusive (`errorMessage` takes precedence). |

src/controls/taxonomyPicker/ITaxonomyPicker.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import { ExtensionContext } from '@microsoft/sp-extension-base';
99
* PropertyFieldTermPickerHost properties interface
1010
// */
1111
export interface ITaxonomyPickerProps {
12-
/**
13-
* Property field label displayed on top
14-
*/
12+
/**
13+
* Property field label displayed on top
14+
*/
1515
label: string;
1616
/**
1717
* TermSet Picker Panel title
@@ -79,6 +79,11 @@ export interface ITaxonomyPickerProps {
7979
*/
8080
placeholder?: string;
8181

82+
/**
83+
* Specifies if the initial values will be validated, when the component is loaded
84+
*/
85+
validateOnLoad?: boolean;
86+
8287
/**
8388
* The method is used to get the validation error message and determine whether the input value is valid or not.
8489
* Mutually exclusive with the static string errorMessage (it will take precedence over this).
@@ -118,6 +123,10 @@ export interface ITaxonomyPickerProps {
118123
export interface ITaxonomyPickerState {
119124
termSetAndTerms? : ITermSet;
120125
errorMessage?: string;
126+
/**
127+
* Error message populated in the component. errorMessage takes precedence over this.
128+
*/
129+
internalErrorMessage?: string;
121130
openPanel?: boolean;
122131
loaded?: boolean;
123132
activeNodes?: IPickerTerms;

src/controls/taxonomyPicker/TaxonomyPicker.tsx

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ export class TaxonomyPicker extends React.Component<ITaxonomyPickerProps, ITaxon
5555
this.onSave = this.onSave.bind(this);
5656
this.termsChanged = this.termsChanged.bind(this);
5757
this.termsFromPickerChanged = this.termsFromPickerChanged.bind(this);
58+
this.termsService = new SPTermStorePickerService(this.props, this.props.context);
59+
}
60+
61+
/**
62+
* componentDidMount lifecycle hook
63+
*/
64+
public componentDidMount() {
65+
this.validateTerms();
5866
}
5967

6068
/**
@@ -87,11 +95,47 @@ export class TaxonomyPicker extends React.Component<ITaxonomyPickerProps, ITaxon
8795
}
8896
}
8997

98+
/**
99+
* it checks, if all entries still exist in term store. if allowMultipleSelections is true. it have to validate all values
100+
*/
101+
private async validateTerms(): Promise<void> {
102+
103+
const {
104+
hideDeprecatedTags,
105+
hideTagsNotAvailableForTagging,
106+
initialValues,
107+
validateOnLoad,
108+
termsetNameOrID
109+
} = this.props;
110+
111+
let isValidateOnLoad = validateOnLoad && initialValues && initialValues.length >= 1;
112+
if (isValidateOnLoad) {
113+
114+
let notFoundTerms: string[] = [];
115+
const termSet = await this.termsService.getAllTerms(termsetNameOrID, hideDeprecatedTags, hideTagsNotAvailableForTagging);
116+
const allTerms = termSet.Terms;
117+
118+
for (let i = 0, len = initialValues.length; i < len; i++) {
119+
const pickerTerm = initialValues[i];
120+
121+
if (!allTerms.filter(t => t.Id === pickerTerm.key).length) {
122+
notFoundTerms.push(pickerTerm.name);
123+
}
124+
}
125+
126+
if (notFoundTerms.length) {
127+
this.setState({
128+
internalErrorMessage: strings.TaxonomyPickerTermsNotFound.replace('{0}', notFoundTerms.join(', '))
129+
});
130+
}
131+
}
132+
}
133+
90134
/**
91135
* Loads the list from SharePoint current web site
92136
*/
93137
private loadTermStores(): void {
94-
this.termsService = new SPTermStorePickerService(this.props, this.props.context);
138+
95139

96140
if (this.props.termActions && this.props.termActions.initialize) {
97141
this.props.termActions.initialize(this.termsService);
@@ -334,6 +378,7 @@ export class TaxonomyPicker extends React.Component<ITaxonomyPickerProps, ITaxon
334378
const {
335379
activeNodes,
336380
errorMessage,
381+
internalErrorMessage,
337382
openPanel,
338383
loaded,
339384
termSetAndTerms
@@ -361,7 +406,7 @@ export class TaxonomyPicker extends React.Component<ITaxonomyPickerProps, ITaxon
361406
</div>
362407
</div>
363408

364-
<FieldErrorMessage errorMessage={errorMessage} />
409+
<FieldErrorMessage errorMessage={errorMessage || internalErrorMessage} />
365410

366411
<Panel
367412
isOpen={openPanel}

src/loc/en-us.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ define([], () => {
4545
"TaxonomyPickerMenuTermSet": "Menu for Term Set",
4646
"TaxonomyPickerInLabel": "in",
4747
"TaxonomyPickerTermSetLabel": "Term Set",
48+
"TaxonomyPickerTermsNotFound": "The next selected term(s) could not be found in the term store: {0}",
4849

4950
peoplePickerComponentTooltipMessage: "People Picker",
5051
peoplePickerComponentErrorMessage: "Required Field",

src/loc/mystrings.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ declare interface IControlStrings {
2626
TaxonomyPickerMenuTermSet: string;
2727
TaxonomyPickerInLabel: string;
2828
TaxonomyPickerTermSetLabel: string;
29+
TaxonomyPickerTermsNotFound: string;
2930

3031
ListItemPickerSelectValue: string;
3132

0 commit comments

Comments
 (0)