Create validation with multiple If statements in Sweet

234
4
Jump to solution
2 weeks ago
RosieLaura
New Contributor II

Hi asking this on S123 board as there isn't a board set up for Sweet that I can find. 

I am trying to set up validation for when I draw a line feature in Sweet, I can apply the following validation to the width field. The line feature is a field boundary and the width field is filled in by the surveyor in the form on sweet manually. The width must be a multiple of 50cm and between 100cm and 500cm. I have written two statements that work individually but not at the same time and need some help combining them. 

 

// STATEMENT 1: validate width is in multiples of 0.5 - Check that numerical value is divisible by a number


var value = $feature["margin_width"];
var multipleOf = 50

if (value != null && value % multipleOf != 0 ) {
var msg = `Value ${value} is not multiple of ${multipleOf}`;
return msg;
}

return true;

 

// STATEMENT 2: validate width is between 1 and 5m


var Width = $feature["margin_width"];
if (Width < 0 || Width > 600) {
var msg = `Margin width ${Width} is not between 0 and 5m `;
return msg;
}
return true;

0 Kudos
2 Solutions

Accepted Solutions
RosieLaura
New Contributor II

HI Cody, Thanks for getting back so quickly!

It throws an error for an unexpected =

RosieLaura_0-1714737244055.png

 

View solution in original post

0 Kudos
CodyPatterson
Occasional Contributor III

Hey @RosieLaura 

I had mistakenly implemented the modulus function inside of the if statement, taking some Javascript and putting it in Arcade had broken it, my bad!

Here is an adjusted function:

var marginWidth = $feature["margin_width"];
var multipleOf = 0.5;

var isMultiple = (marginWidth % multipleOf == 0);
var isInRange = (marginWidth >= 1 && marginWidth <= 5);

if (isInRange && !isMultiple) {
    return "Margin width " + marginWidth + " is not a multiple of " + multipleOf + " and not between 1 and 5m.";
} else if (!isInRange) {
    return "Margin width " + marginWidth + " is not between 1 and 5m.";
} else if (!isMultiple) {
    return "Margin width " + marginWidth + " is not a multiple of " + multipleOf + ".";
}

return true;

Hopefully this will get you going!

Cody

View solution in original post

0 Kudos
4 Replies
CodyPatterson
Occasional Contributor III

Hey @RosieLaura 

Could you give this a try and let me know if it helps out?

var marginWidth = $feature["margin_width"];
var multipleOf = 0.5;

if ((marginWidth >= 1 && marginWidth <= 5) && (marginWidth % multipleOf !== 0)) {
    return `Margin width ${marginWidth} is not a multiple of ${multipleOf} and not between 1 and 5m.`;
} else if (marginWidth < 1 || marginWidth > 5) {
    return `Margin width ${marginWidth} is not between 1 and 5m.`;
} else if (marginWidth % multipleOf !== 0) {
    return `Margin width ${marginWidth} is not a multiple of ${multipleOf}.`;
}

return true;

 Hope that helps!

Cody

0 Kudos
RosieLaura
New Contributor II

HI Cody, Thanks for getting back so quickly!

It throws an error for an unexpected =

RosieLaura_0-1714737244055.png

 

0 Kudos
CodyPatterson
Occasional Contributor III

Hey @RosieLaura 

I had mistakenly implemented the modulus function inside of the if statement, taking some Javascript and putting it in Arcade had broken it, my bad!

Here is an adjusted function:

var marginWidth = $feature["margin_width"];
var multipleOf = 0.5;

var isMultiple = (marginWidth % multipleOf == 0);
var isInRange = (marginWidth >= 1 && marginWidth <= 5);

if (isInRange && !isMultiple) {
    return "Margin width " + marginWidth + " is not a multiple of " + multipleOf + " and not between 1 and 5m.";
} else if (!isInRange) {
    return "Margin width " + marginWidth + " is not between 1 and 5m.";
} else if (!isMultiple) {
    return "Margin width " + marginWidth + " is not a multiple of " + multipleOf + ".";
}

return true;

Hopefully this will get you going!

Cody

0 Kudos
RosieLaura
New Contributor II

That's brilliant thank you!

0 Kudos