0

This question already has an answer here:

I've created an item validator that checks to see if there is a value in one of two fields (based on this answer). It works, but I would like it to prevent saving of the item if it's invalid. In order to do that, I set it to fail with a FatalError, which causes it to be evaluated prior to saving.

Unfortunately, when my validator executes, the item it evaluates is the previously-saved item not the current state of the item.

protected override ValidatorResult Evaluate()
{
    Item item = this.GetItem();
    if (item == null)
        return ValidatorResult.Valid;

    string field1Name = "field 1";
    string field2Name = "field 2";
    string field1 = item[field1Name];
    string field2 = item[field2Name];

    if (string.IsNullOrEmpty(field1) && string.IsNullOrEmpty(field2))
    {
        this.Text = this.GetText($"You must enter a value in either the {field1Name} field or the {field2Name} field");
        return this.GetFailedResult(ValidatorResult.FatalError);
    }

    return ValidatorResult.Valid;
}

Is there a way to get the "in-memory" version of the item with populated values?

marked as duplicate by Dan Sinclair, Vlad Iobagiu, Mark Cassidy Mar 8 at 14:42

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Yep, absolutely. Not sure how I missed that one. Please VTC as dupe. :) – Dan Sinclair Mar 8 at 14:35

Browse other questions tagged or ask your own question.