validate method

FieldResultFree validate(
  1. Object? fieldValue
)

Implementation

FieldResultFree validate(Object? fieldValue) {
  var fieldResult = FieldResultFree(this);
  var value = ObjectDescovery.descovr(
    fieldValue,
    type,
    def: defaultValue,
  );

  if (fix != null) {
    value = fix?.call(value);
  }

  if (validators.isEmpty || readonly) {
    fieldResult.success &= true;
    fieldResult.failed = false;
    fieldResult.errors = [];
    fieldResult.error = '';
    fieldResult.errorHtml = '';
    fieldResult.valid = 'is-valid';
    fieldResult.value = value;
    fieldResult.parsedValue = value;
  } else {
    validators.forEach((validator) {
      FieldValidateResult result = validator(value);
      fieldResult.success &= result.success;
      fieldResult.failed = !fieldResult.success;
      if (!result.success) {
        fieldResult.errors.addAll(result.errors);
      }
      fieldResult.error = fieldResult.errors.join(',');
      fieldResult.errorHtml = fieldResult.errors.join('<br/>');
      fieldResult.valid = fieldResult.success ? 'is-valid' : 'is-invalid';
      fieldResult.value = fieldValue;
      fieldResult.parsedValue = value;
    });
  }
  return fieldResult;
}