Form Validation; At least 1 of 3 fields must NOT be null

Simon Goldsmith asked on June 13, 2017 17:37

I have 3 custom fields (HeaderImage, HeaderVideo, HeaderVideoURL) that I have added to the PageType Page(Menu Item) so that every page has either a video (Media selection or external url) or image header area displayed.

What I would like to do is validate these 3 fields together so that 1 of the 3 is required at all times.

Is there a way using the ENABLED CONDITION field to set a fields as 'required' dynamically based on the values of the other 2 fields?

THanks Simon

Correct Answer

Trevor Fayas answered on June 14, 2017 16:24

May help to know that even disabled (not enabled) fields need to be validated, but hidden fields are not validated.

So if you have a textbox that needs to be at least 10 characters long, if it's hidden it won't do a check on it.

The other tricky part though is that any empty value does not have validation rules applied. So if you make it required, it must have a value, if you don't and don't put any value then it doesn't check the rules.

So here's what you have to do:

  1. Create a custom validation rule that takes 3 parameters, Field1, Field2, Field2
  2. Set the Condition to !String.IsNullOrWhiteSpace(Fields["{Field1}"].Value) || !String.IsNullOrWhiteSpace(Fields["{Field2}"].Value) || !String.IsNullOrWhiteSpace(Fields["{Field3}"].Value)
  3. Make sure you set "Requires Context" checked.
  4. On your form, set the 3 fields to NOT required (so you can put a null value)
  5. Create another 4th field, call it "VideoChecker" of type bool, make it required and set the default value to true. You can optionally wrap this with a div style="display:none" tag.
  6. Add the custom validation rule to this VideoChecker, and put in for the 3 fields the Field names of the 3 you want to check.

This worked for me, basically you're forcing a validation through the VideoChecker field.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Zach Perry answered on June 13, 2017 17:59

The way I have done it in the past is create a alternative form for insert and update, add a field to that form named Validation.Set that field to required, a bool or something, For that field do a validation check if the other fields are empty, if they are, throw an error.

0 votesVote for this answer Mark as a Correct answer

Simon Goldsmith answered on June 13, 2017 18:10

Maybe I am misunderstanding your explanation, but this isn't for a front-end custom form, this is validation on a cms form based on Page Type creation...?

0 votesVote for this answer Mark as a Correct answer

Zach Perry answered on June 13, 2017 18:33

If you create an alternative form for a page type, name it update, it is the form used for updating pages of that type, if you name it insert it is used when creating a new page of that type.

Documentation on creating alternative forms

1 votesVote for this answer Mark as a Correct answer

Simon Goldsmith answered on June 14, 2017 10:12 (last edited on June 14, 2017 10:15)

Thanks Zac!

As I have never followed this train of thought can you confirm this is the correct approach please?

1) Create alternative forms for UPDATE and INSERT. 2) Add a new BOOL field for custom validation - Does this need to be displayed in the form? 3) How do I use this new field to validate the others I am not seeing the link?

Many thanks Simon

0 votesVote for this answer Mark as a Correct answer

Simon Goldsmith answered on June 26, 2017 09:47

Ahhh awesome. Thanks for the explanation. I've never had to do this yet so I thanks again! :D

0 votesVote for this answer Mark as a Correct answer

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