Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


onValidateValue

An event raised to validate or adjust the field's value before committing it.
Read time 1 minuteLast updated 12 days ago

Definition

public event BaseField<TValueType>.ValidateValueHandler onValidateValue

Remarks

Use this event to modify or validate a value before committing it. For example, you can clamp, sanitize, or replace the input value to match specific constraints. To reject an input value, return a fallback such as the current _value.
Registered callbacks run whenever the field's value changes, either through the _value setter or BaseField<TValueType>.SetValueWithoutNotify. The value returned by each callback replaces the incoming value and is then stored. If the change occurs from the _value setter, it reports the stored value as _newValue. If the change occurs from BaseField<TValueType>.SetValueWithoutNotify, it doesn't report a ChangeEvent<T>.
If you register more than one callback, they compose in their registration order. Each callback receives the previous callback's result, and the final result is stored and reported.
To assign a value while bypassing these callbacks, wrap the assignment in BaseField<TValueType>.IgnoreValidation().
The following example clamps a field's value to a maximum before returning it:

Examples

using UnityEngine;using UnityEngine.UIElements;namespace MyNamespace{ public class BaseFieldValidateValueExample { // Clamps the field's value to a maximum before it is committed. public IntegerField CreateCappedField() { var field = new IntegerField("Capped value"); const int max = 1000; field.onValidateValue += v => Mathf.Min(v, max); return field; } }}