Template Variables

TV Values and OnBeforeDocFormSave

light fare

TV Values

Getting a TV value from the database is easy. But what if you want to work with a TV's value before it's saved? Perhaps to validate the value or manipulate it in some way before saving it? A Slack conversation pointed the way.

jason, on Slack asked...

So now the million dollar question, is it possible to access tv values within onBeforeDocFormSave? Trying to validate and then display errors if a tv value isn’t a certain format.

The Solution:

Bruno17 says...

You can read the TV-value from REQUEST.

    $images = $modx->getOption('tv12',$_REQUEST,'');

What's the Story?

If the event is invoked before the form's fields are saved to the database, you can't get them using the usual API functions. The form's fields have all been posted however, just not processed, so they can now be access via the $_REQUEST array, including values for TV fields.

The $modx->getOption() function lets you get values from more than just settings or element properties, although this feature doesn't appear to be well-documented except for BobRay's excellent blog post.