How to validate data before storing
If you want to operate a validation check before storing data in the database, you have to:
- declare and write a rule for the model in Delphi
rule.pasunit - define the node
Rules:for the model
example form HelloKitto:
# rule.pas
type
TCheckDuplicateInvitations = class(TKRuleImpl)
public
procedure BeforeAdd(const ARecord: TKRecord); override;
end;
procedure TCheckDuplicateInvitations.BeforeAdd(const ARecord: TKRecord);
begin
if ARecord.Store.Count('INVITEE_ID', ARecord.FieldByName('INVITEE_ID').Value) > 1 then
RaiseError(_('Cannot invite the same girl twice.'));
end;# model Invitation.yaml
ModelName: Invitation
Fields:
.....
Rules:
CheckDuplicateInvitations:Including field values in error messages
Field.AsString returns a user-friendly string for every data type, so error messages built with values read from the record show formatted values rather than raw internals. In particular, Date, Time and DateTime fields are formatted using the application's FormatSettings (driven by UserFormats/Date and UserFormats/Time in Config.yaml):
procedure TCheckEndDateAfterStartDate.BeforeAddOrUpdate(const ARecord: TKRecord);
var
LStart, LEnd: TKField;
begin
LStart := ARecord.FieldByName('StartDate');
LEnd := ARecord.FieldByName('EndDate');
if not LStart.IsNull and not LEnd.IsNull and (LEnd.AsDate <= LStart.AsDate) then
raise Exception.CreateFmt(
_('%s must be greater than %s'),
[LEnd.ViewField.DisplayLabel, LStart.AsString]);
// → "End Date must be greater than 01/07/2026"
end;If you ever see a raw float like 46204 instead of a formatted date in a message, the value is being read via VarToStr directly on the Variant; switch to Field.AsString and the framework's TEFDateDataType.ValueToString will format it correctly.
Field-change rules and partial date typing
AfterFieldChange rules attached to a date field fire only when the user leaves the input with a complete value. The framework filters out partial values fired by the browser while the user is still typing a year (0002-07-01) before they reach the server, so rules see only complete, parsable dates. See Form — partial date / time values during typing for details.
