4

I'm building a component that, among other fields, contains "title" and "text" fields. The business logic is:

  • If both fields are empty, that is OK
  • If both fields are populated, that is OK
  • If one field is populated, but the other isn't it is NOT OK

The request is that this show up as field validation errors. I know I can write this complex logic using item validators, but item validators won't show errors in experience editor for the page because the item is a component, hence the request for field validators.

Is it possible to build a field validator that checks the value of another field on the item?

update:

Some pretty good suggestions were listed in the comments below. At the end of the day, we ended up making this into a controller rendering and handling validation logic in there. We additionally put an item validator on the component itself to prevent it from going through workflow.

  • How about writing custom field validator and in parameters field of validator put something like "DependentField=Text"? For "Title" field you will set this field validator and in code you will check also dependent field with field value of "Title" field. In Experience Editor you just need to check "Field validation" on "View" tab and you are good. Make it FatalError and nobody can save item when it's NOT OK. – Peter Procházka Mar 23 '18 at 19:36
  • This is not an answer to your question (sorry) but wondering if you can avoid this sticky scenario by simply allowing the scenario? I have found in the past that restricting the flexibility of a component often just annoys Authors more than anything. You can always default it with some text so that the author has to make a conscious decision to empty it/change it. – Jason St-Cyr Mar 23 '18 at 21:26
  • @PeterProchazka the problem is that the validator kicks in prior to save, so I'm not sure how to get the value of the other field. – Ben Lipson Mar 23 '18 at 21:30
  • @JayS i'm with you there, but they're pretty adamant on the requirement heh. – Ben Lipson Mar 23 '18 at 21:30
  • @BenLipson - And how about hooking to "item:saving" pipeline? In there check field values and abort if NOT OK. This was mentioned by Marek on another threaf here on SSE-> sitecore.stackexchange.com/questions/6704/…. Seemd like good fit for your requirements. – Peter Procházka Mar 23 '18 at 21:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.