E-Commerce Product Weight Required if item Needs Shipping

Marla Krause asked on July 24, 2017 19:22

This feels like it should be stupid simple, but for the life of me I can't get this to work. So what I want to do is to require the Product Weight to be filled in if the SKUNeedsShipping checkbox is checked.

On the SKU Weight field I have tried to put validation that will check to see if SKUWeight.Value == null, this errors when I enter a value, not when it's null. If I change it to look for != null it does nothing. Based on these tests I would assume that #1. this test logic must be what is valid, not what is invalid, like I would assume. And #2 that the validation is only being run when the field isn't null, not helpful.

So I then tried to add my validation to the SKUNeedsShipping field. From that field checking SKUWeight.Value != null requires that a value always be supplied, but I can't make it dependent on whether the checkbox is checked. This is one of many ways I've tried: SKUNeedsShipping.Value == true && SKUWeight.value != null

Does anybody know how to do this?



Correct Answer

Marla Krause answered on July 25, 2017 14:44

Ok, so here is what I finally got to work.

(SKUNeedsShipping.Value.ToBool() == true && SKUWeight.Value.ToString() != null) || (SKUNeedsShipping.Value.ToBool() == false && (SKUWeight.Value.ToString() != null || SKUWeight.Value.ToString() == null))

Based on Brenden's response that I needed to cast the object I figured out how to cast the object to a boolean. As you can see, I needed to state all of the "True" cases.

Thanks for the push in the right direction Brenden!

0 votesVote for this answer Unmark Correct answer

Recent Answers

Brenden Kehren answered on July 24, 2017 20:35

The .Value property is an object so it's best to cast it to a specific type.

For instance something like ValidationHelper.GetBoolean(SKUNeedsShipping.Value, false) == true.

0 votesVote for this answer Mark as a Correct answer

Marla Krause answered on July 25, 2017 13:28

That's not working Brenden. I'm now getting errors in the event log:

Error while evaluating expression: (ValidationHelper.GetBoolean(SKUNeedsShipping.Value, false) == true && ValidationHelper.GetString(SKUWeight.Value, false) != null|(user)mkrause|(hash)78dff887d177251eb1ce11e0a66036e26167d7acf8fa369d03abaca5ec6e39a3

CMS.MacroEngine.EvaluationException: Exception occured while evaluation of the expression '(ValidationHelper.GetBoolean(SKUNeedsShipping.Value, false) == true && ValidationHelper.GetString(SKUWeight.Value, false) != null|(user)mkrause|(hash)78dff887d177251eb1ce11e0a66036e26167d7acf8fa369d03abaca5ec6e39a3': Method 'GetBoolean' not found

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on July 25, 2017 15:03

Unfortunately, your original question wasn't clear you were using macros. The solution I provided will work with a C# example. Either way, you need to type the object as you discovered.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.