Version: Unity 6.6 Beta (6000.6)
LanguageEnglish
  • C#

BaseField<T0>.onValidateValue

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

An event raised to validate or adjust the field's value before committing it.


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 BaseField<T0>.value.

Registered callbacks run whenever the field's value changes, either through the BaseField<T0>.value setter or BaseField<T0>.SetValueWithoutNotify. The value returned by each callback replaces the incoming value and is then stored. If the change occurs from the BaseField<T0>.value setter, it reports the stored value as ChangeEvent<T0>.newValue. If the change occurs from BaseField<T0>.SetValueWithoutNotify, it doesn't report a ChangeEvent<T0>.

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<T0>.IgnoreValidation.


The following example clamps a field's value to a maximum before returning it:

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; } } }