ValidateInputValidate the input of the field when the value changes.
-
string callbackis the callback function to validate the data.Parameters:
- If the function accepts no arguments, then no argument will be passed
- If the function accepts required arguments, the first required argument will receive the field's value. If there is another required argument and the field is inside a list/array, the index will be passed.
- If the function only has optional arguments, it will try to pass the field's value and index if possible, otherwise the default value of the parameter will be passed.
Return:
- If return type is
string, thennullor empty string for valid, otherwise, the string will be used as the error message - If return type is
bool, thentruefor valid,falsefor invalid with message "`{label}` is invalid`"
-
AllowMultiple: Yes
using SaintsField; // string callback [ValidateInput(nameof(OnValidateInput))] public int _value; private string OnValidateInput() => _value < 0 ? quot;Should be positive, but gets {_value}" : null; // property validate [ValidateInput(nameof(boolValidate))] public bool boolValidate; // bool callback [ValidateInput(nameof(BoolCallbackValidate))] public string boolCallbackValidate; private bool BoolCallbackValidate() => boolValidate; // with callback params [ValidateInput(nameof(ValidateWithReqParams))] public int withReqParams; private string ValidateWithReqParams(int v) => quot;ValidateWithReqParams: {v}"; // with optional callback params [ValidateInput(nameof(ValidateWithOptParams))] public int withOptionalParams; private string ValidateWithOptParams(string sth="a", int v=0) => quot;ValidateWithOptionalParams[{sth}]: {v}"; // with array index callback [ValidateInput(nameof(ValidateValArr))] public int[] valArr; private string ValidateValArr(int v, int index) => quot;ValidateValArr[{index}]: {v}";