Error contentLib.create function

Enonic version: 6.6.0
OS: Ubuntu

Hi.

ContentLib.create is allowing me to create objects with empty attributes even though I have set them to be required in content type.

I am passing an empty string and the function is accepting it.

Please check images below.

From what I see, the content is flagged as “invalid” - this means you will not be able to publish it I guess? What happens if you try creating the content directly in master?

Also, did you set requireValid = true?

Hi tsi,

I updated my service and set requireValid to be ‘true’ and branch to be ‘master’. However ContentLib.create still creates the object in admin.

What am I missing here ?

try
	{
		var formObj = {
			parentPath: '/rfd/problemer-rapportert',
			displayName: validateInputs(req.params.name, "string") +' - '+ validateInputs(req.params.problem, "string"),
			// draft: true,
			branch: 'master',
			requireValid: true,
			contentType: app.name + ':form-problem',
			language: 'no',
			data: {
				problem: "", // required
				name: "", // required
				address: "", // required
				postcode: "", // required
				phone: "" // required
			},
			x: {},
			attachments: {}
		};

		if (typeof req.params.container  != "undefined" && req.params.problem != 'Annet')
			formObj.data.container = validateInputs(req.params.container, "string"); // not required

		if (typeof req.params.description  != "undefined")
			formObj.data.description = validateInputs(req.params.description, "string"); // not required

		// Creating draft.
		var createResult = contentLib.create(formObj);

		// Publishing.
		var publishResult = saveForm(createResult._id);
		
        return {
            redirect: url + "?result=success"
        };
	}


In fact, an empty text is different from a null value. Null means that it does not exists, while “” is a text without any character. This is reinforced on strongly typed languages, like C#. If you want to avoid creation of these objects, you should at least run a function before creating with contentLib to clean these properties.
for (var i in test) { if (test[i] === null || test[i] === undefined || test[i] === "") { delete test[i]; } }

1 Like

You should always create content in the draft branch and then publish it to master or else you can’t see/edit/delete it in the Content Studio.

1 Like

“you should at least run a function before creating with contentLib”. We are checking the inputs before creating the content. I was just surprise that it allowed me to crate a object even though we have set the attributes to be required (“occurrences minimum=“1” maximum=“1”/>”). We can clearly see that interrogation mark in first picture saying that the object wasn’t create properly.

“You should always create content in the draft branch…” yes, that’s right. We are creating in the draft branch. I only changed it to master for test purposes.