Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch to DynamicForm required field #1928

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 34 additions & 34 deletions src/controls/dynamicForm/DynamicForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const timeout = (ms: number): Promise<void> => {
export class DynamicForm extends React.Component<
IDynamicFormProps,
IDynamicFormState
> {
> {
private _spService: SPservice;
private _formulaEvaluation: FormulaEvaluation;
private _customFormatter: CustomFormattingHelper;
Expand Down Expand Up @@ -359,7 +359,7 @@ export class DynamicForm extends React.Component<

// When a field is required and has no value
if (field.required) {
if (field.newValue === undefined && field.value===undefined) {
if ((field.newValue === undefined || field.newValue.length === 0) && (field.value === undefined || field.value.length === 0)) {
if (
field.defaultValue === null ||
field.defaultValue === "" ||
Expand Down Expand Up @@ -599,13 +599,13 @@ export class DynamicForm extends React.Component<
else if (contentTypeId.startsWith("0x0120")) {
// We are adding a folder or a Document Set
try {
const idField = "ID";
const idField = "ID";
const contentTypeIdField = "ContentTypeId";

const library = await sp.web.lists.getById(listId);
const library = await sp.web.lists.getById(listId);
const folderFileName = this.getFolderName(objects);
const folder = !this.props.folderPath ? library.rootFolder : await this.getFolderByPath(this.props.folderPath, library.rootFolder);
const newFolder = await folder.addSubFolderUsingPath(folderFileName);
const folder = !this.props.folderPath ? library.rootFolder : await this.getFolderByPath(this.props.folderPath, library.rootFolder);
const newFolder = await folder.addSubFolderUsingPath(folderFileName);
const fields = await newFolder.listItemAllFields();

if (fields[idField]) {
Expand Down Expand Up @@ -681,8 +681,8 @@ export class DynamicForm extends React.Component<
"_"
).trim() // Replace not allowed chars in folder name and trim empty spaces at the start or end.
: ""; // Empty string will be replaced by SPO with Folder Item ID
const folder = !this.props.folderPath ? library.rootFolder : await this.getFolderByPath(this.props.folderPath, library.rootFolder);

const folder = !this.props.folderPath ? library.rootFolder : await this.getFolderByPath(this.props.folderPath, library.rootFolder);
const fileCreatedResult = await folder.files.addChunked(encodeURI(itemTitle), await selectedFile.downloadFileContent());
const fields = await fileCreatedResult.file.listItemAllFields();

Expand Down Expand Up @@ -992,13 +992,13 @@ export class DynamicForm extends React.Component<
const isEditingItem = listItemId !== undefined && listItemId !== null && listItemId !== 0;
let etag: string | undefined = undefined;

if (isEditingItem) {
if (isEditingItem) {
const spListItem = spList.items.getById(listItemId);
if (contentTypeId.startsWith("0x0120") || contentTypeId.startsWith("0x0101")) {

if (contentTypeId.startsWith("0x0120") || contentTypeId.startsWith("0x0101")) {
spListItem.select("*","FileLeafRef"); // Explainer: FileLeafRef is not loaded by default. Load it to show the file/folder name in the field.
}

item = await spListItem.get().catch(err => this.updateFormMessages(MessageBarType.error, err.message));

if (onListItemLoaded) {
Expand Down Expand Up @@ -1097,20 +1097,20 @@ export class DynamicForm extends React.Component<
let showAsPercentage: boolean | undefined;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const selectedTags: any = [];

let fieldName = field.InternalName;
if (fieldName.startsWith('_x') || fieldName.startsWith('_')) {
fieldName = `OData_${fieldName}`;
}

// If a SharePoint Item was loaded, get the field value from it
if (item !== null && item[fieldName]) {
value = item[fieldName];
stringValue = value.toString();
} else {
defaultValue = field.DefaultValue;
}

// Store choices for Choice fields
if (field.FieldType === "Choice") {
field.Choices.forEach((element) => {
Expand All @@ -1122,7 +1122,7 @@ export class DynamicForm extends React.Component<
choices.push({ key: element, text: element });
});
}

// Setup Note, Number and Currency fields
if (field.FieldType === "Note") {
richText = field.RichText;
Expand All @@ -1138,7 +1138,7 @@ export class DynamicForm extends React.Component<
cultureName = this.cultureNameLookup(numberField.CurrencyLocaleId);
}
}

// Setup Lookup fields
if (field.FieldType === "Lookup" || field.FieldType === "LookupMulti") {
lookupListId = field.LookupListId;
Expand All @@ -1161,7 +1161,7 @@ export class DynamicForm extends React.Component<
value = [];
}
}

// Setup User fields
if (field.FieldType === "User") {
if (item !== null) {
Expand Down Expand Up @@ -1200,7 +1200,7 @@ export class DynamicForm extends React.Component<
}
principalType = field.PrincipalAccountType;
}

// Setup Taxonomy / Metadata fields
if (field.FieldType === "TaxonomyFieldType") {
termSetId = field.TermSetId;
Expand Down Expand Up @@ -1242,7 +1242,7 @@ export class DynamicForm extends React.Component<
name: element.Label,
});
});

value = selectedTags;
} else {
if (defaultValue && defaultValue !== "") {
Expand All @@ -1253,31 +1253,31 @@ export class DynamicForm extends React.Component<
name: element.split("|")[0],
});
});

value = selectedTags;
stringValue = selectedTags?.map(dv => dv.key + ';#' + dv.name).join(';#');
}
}
if (defaultValue === "") defaultValue = null;
}

// Setup DateTime fields
if (field.FieldType === "DateTime") {

if (item !== null && item[fieldName]) {

value = new Date(item[fieldName]);
stringValue = value.toISOString();
} else if (defaultValue === "[today]") {
defaultValue = new Date();
} else if (defaultValue) {
defaultValue = new Date(defaultValue);
}

dateFormat = field.DateFormat || "DateOnly";
defaultDayOfWeek = (await this._spService.getRegionalWebSettings(this.webURL)).FirstDayOfWeek;
}

// Setup Thumbnail, Location and Boolean fields
if (field.FieldType === "Thumbnail") {
if (defaultValue) {
Expand Down Expand Up @@ -1333,7 +1333,7 @@ export class DynamicForm extends React.Component<
showAsPercentage: showAsPercentage,
customIcon: customIcons ? customIcons[field.InternalName] : undefined
});

// This may not be necessary now using RenderListDataAsStream
tempFields.sort((a, b) => a.Order - b.Order);
}
Expand Down Expand Up @@ -1497,19 +1497,19 @@ export class DynamicForm extends React.Component<

if (objects[fileLeafRefField] !== undefined && objects[fileLeafRefField] !== "")
folderNameValue = objects[fileLeafRefField] as string;

if (objects[titleField] !== undefined && objects[titleField] !== "")
folderNameValue = objects[titleField] as string;

return folderNameValue.replace(/["|*|:|<|>|?|/|\\||]/g, "_").trim();
}

/**
* Returns a pnp/sp folder object based on the folderPath and the library the folder is in.
* The folderPath can be a server relative path, but should be in the same library.
* @param folderPath The path to the folder coming from the component properties
* @param rootFolder The rootFolder object of the library
* @returns
* @returns
*/
private getFolderByPath = async (folderPath: string, rootFolder: IFolder): Promise<IFolder> => {
const libraryFolder = await rootFolder();
Expand All @@ -1520,7 +1520,7 @@ export class DynamicForm extends React.Component<
if (`${normalizedFolderPath}/`.startsWith(`${serverRelativeLibraryPath}/`)) {
return sp.web.getFolderByServerRelativePath(normalizedFolderPath);
}

// In other cases, expect a list-relative path and return the folder
const folder = sp.web.getFolderByServerRelativePath(`${serverRelativeLibraryPath}/${normalizedFolderPath}`);
return folder;
Expand All @@ -1539,13 +1539,13 @@ export class DynamicForm extends React.Component<
return await list.items.getById(itemId).update(objects);
}
catch (error)
{
{
if (error.status === 409 && retry < 3) {
await timeout(100);
return await this.updateListItemRetry(list, itemId, objects, retry + 1);
}

throw error;
}
}
}
}
}
Loading