Page 1 of 1

loop all fields in fd

Posted: 09 Oct 2019
by Jens.Hetze
Hi SP Forms Designer team,

is there a possibility to loop all fields in fd object? I want to check all asterisks to implement a simple required validation.
Or is there a better possibility to do this?

Thanks in advance.
Jens

Re: loop all fields in fd

Posted: 10 Oct 2019
by mnikitina
Hello Jens.Hetze,

If the filed is required it will be validated by default.

If you need custom validation you can add it to your form with the help of Forms Designer's JS-framework and check whether all the required fields are filled in. Here is a sample:

Code: Select all

fd.onsubmit(function() {
	if (!fd.field('Field1').value()) {
	
		alert('Please, fill-in the Field 1');
		return false;
	}

	return true;
});
Please also have a look at the 'How to conditionally hide, disable, and make mandatory fields on SharePoint forms dynamically' article, 'Require field based on condition' part for more information.

Re: loop all fields in fd

Posted: 10 Oct 2019
by Jens.Hetze
Hi mnikitina.

Thank you for your answer. Maybe it was too little information. I know that if the fields in SharePoint are marked as required, validation will be performed by default. In my case, however, all SharePoint columns are optional because they should be filled out in several workflow steps. For this reason, I would like to retrieve all fields marked with an asterisk in SP Forms Designer and implement my own validation.
I hope that helps to understand my case.

Re: loop all fields in fd

Posted: 11 Oct 2019
by mnikitina
Jens.Hetze,

You can add the Asterisk to the Field Name in the field settings.
Capture.PNG
Capture.PNG (4.17 KiB) Viewed 2917 times
And you can add custom validation using the script. This should be done for each field separately

Code: Select all

fd.onsubmit(function() {
	if (!fd.field('Field2').value()) {
	
		alert('Please, fill-in the Field 2');
		return false;
	}

	return true;
});

fd.onsubmit(function() {
	if (!fd.field('Field2').value()) {
	
		alert('Please, fill-in the Field 2');
		return false;
	}

	return true;
});
or you can use one validator for all fields.

Code: Select all

fd.onsubmit(function() {
	if (!fd.field('Field1').value() || !fd.field('Field2').value() || !fd.field('Field3').value()) {
	
		alert('Please, fill-in required fields');
		return false;
	}

	return true;
	});

Anyway you need to specify each field that you want to validate.