Display field-level validation
Combine built-in validity with a business rule attached to a named control.
valFields.Target selects which field receives ErrorText. Setting ErrorText to an empty string clears that field’s previous message. The age rule is applied only after numAge.IsValid succeeds. A final status should summarize the result, while the individual field retains the reason it needs attention.
Step 1Prepare a safe practice project#
Extract the archive into a new writable folder, separate from your own application. Open ValidationProvider.mcproj in McBasic™. Keep every declared source, layout, resource and migration file together.
To reconstruct it yourself, create a DesktopApp project and add the files declared below. In the complete project, follow these steps as a guided rebuild: replace each named source file once and Check after all its procedures are present.
Exact project configuration
{
"schemaVersion": 1,
"projectId": "591ed385-26a9-4e20-89f7-dd22aa261026",
"name": "ValidationProvider",
"kind": "DesktopApp",
"language": {
"dialect": "McBasic1",
"requireExplicit": true,
"compare": "Binary",
"defaultArrayBase": 0
},
"application": {
"bundleIdentifier": "com.mcbasic.samples.validationprovider",
"version": "0.1.0",
"buildNumber": "1",
"startupFormId": "form-main",
"shutdownMode": "OnStartupFormClosed"
},
"forms": [
{
"id": "form-main",
"typeName": "frmMain",
"layout": "Forms/frmMain.form.json",
"code": "Forms/frmMain.bas"
}
],
"modules": [],
"classes": [],
"tests": [],
"references": [],
"resources": [],
"targets": [
"osx-arm64"
],
"capabilities": []
}Step 2Build the Form and set its properties#
frmMain
Select the Form first and set its properties. Add containers before their children. These are initial Designer values, not statements to paste into a procedure.
| Form property | Value |
|---|---|
Caption | "ValidationProvider — McBasic" |
Height | 440 |
Width | 520 |
| Type | Name | Parent |
|---|---|---|
| TextBox | txtName | Form |
| TextBox | txtEmail | Form |
| NumericBox | numAge | Form |
| MaskedTextBox | mtxPhone | Form |
| Label | lblStatus | Form |
| CommandButton | cmdValidate | Form |
| ValidationProvider | valFields | Component tray |
txtName · exact Inspector values
| Property | Value |
|---|---|
AccessibleName | "Customer name" |
Enabled | True |
Height | 32 |
Left | 24 |
MultiLine | False |
ReadOnly | False |
TabIndex | 0 |
Text | "" |
Top | 24 |
Visible | True |
Width | 440 |
WordWrap | False |
Required | True |
| Anchor edges | Left, Top |
txtEmail · exact Inspector values
| Property | Value |
|---|---|
AccessibleName | "Email address" |
Enabled | True |
Height | 32 |
Left | 24 |
MultiLine | False |
ReadOnly | False |
TabIndex | 1 |
Text | "" |
Top | 80 |
Visible | True |
Width | 440 |
WordWrap | False |
MaxLength | 80 |
Required | True |
| Anchor edges | Left, Top |
numAge · exact Inspector values
| Property | Value |
|---|---|
Left | 24 |
Top | 136 |
Width | 180 |
Height | 32 |
TabIndex | 2 |
Minimum | 0 |
Maximum | 100 |
Value | 18 |
Step | 1 |
DecimalPlaces | 0 |
Culture | "Invariant" |
| Anchor edges | Left, Top |
mtxPhone · exact Inspector values
| Property | Value |
|---|---|
Left | 24 |
Top | 192 |
Width | 220 |
Height | 32 |
TabIndex | 3 |
Mask | "000-0000" |
PromptChar | "_" |
Text | "" |
| Anchor edges | Left, Top |
lblStatus · exact Inspector values
| Property | Value |
|---|---|
Caption | "Enter a name, email and optional phone, then choose Validate." |
Enabled | True |
Height | 64 |
Left | 24 |
TabIndex | 4 |
Top | 248 |
Visible | True |
Width | 440 |
| Anchor edges | Left, Top |
cmdValidate · exact Inspector values
| Property | Value |
|---|---|
Caption | "Validate fields" |
Enabled | True |
Height | 32 |
Left | 24 |
TabIndex | 5 |
Top | 336 |
Visible | True |
Width | 220 |
| Anchor edges | Left, Top |
valFields · exact Inspector values
| Property | Value |
|---|---|
Target | "" |
ErrorText | "" |
Step 3Connect the events explicitly#
frmMain
| Object | Event | Procedure | Parameters |
|---|---|---|---|
cmdValidate | Click | cmdValidate_Click | Click() |
Step 4Enter the complete behavior#
Open the named source file. Enter declarations first, then every procedure. Expand a procedure for its full body, or copy the complete file. Helpers are required: do not stop after entering the first Click handler.
Forms/frmMain.bas
Enter these declarations at file scope, before the procedures.
Option ExplicitPrivate Sub cmdValidate_Click()
Enter the complete action, including its input checks and calls to helper procedures.
Private Sub cmdValidate_Click()
valFields.Target = "txtEmail"
If Len(Trim(txtEmail.Text)) > 0 And Len(Trim(txtEmail.Text)) < 3 Then
valFields.ErrorText = "Email is too short."
Else
valFields.ErrorText = ""
End If
valFields.Target = "numAge"
valFields.ErrorText = ""
If numAge.IsValid Then
If numAge.Value < 18 Then
valFields.ErrorText = "Age must be at least 18."
End If
End If
If mtxPhone.IsValid And valFields.IsValid Then
lblStatus.Caption = "Ready to save."
Else
lblStatus.Caption = CStr(valFields.ErrorCount) & " field(s) need attention: " & valFields.FirstError
End If
End SubCopy the complete Forms/frmMain.bas file
Replace the file once; do not append duplicate procedures.
Option Explicit
Private Sub cmdValidate_Click()
valFields.Target = "txtEmail"
If Len(Trim(txtEmail.Text)) > 0 And Len(Trim(txtEmail.Text)) < 3 Then
valFields.ErrorText = "Email is too short."
Else
valFields.ErrorText = ""
End If
valFields.Target = "numAge"
valFields.ErrorText = ""
If numAge.IsValid Then
If numAge.Value < 18 Then
valFields.ErrorText = "Age must be at least 18."
End If
End If
If mtxPhone.IsValid And valFields.IsValid Then
lblStatus.Caption = "Ready to save."
Else
lblStatus.Caption = CStr(valFields.ErrorCount) & " field(s) need attention: " & valFields.FirstError
End If
End SubStep 5Keep resources and storage reproducible#
No external resource or migration files are declared. Any inline migration, seed data or local JSON is present in the code above. Runtime application data and private preferences are not included in the archive.
Step 6Check, run and test the result#
Save all files and run Check. Resolve diagnostics before Run. Developer CLI users can work from the extracted folder:
mcbasic check ValidationProvider.mcproj
mcbasic run ValidationProvider.mcprojAcceptance checklist
- Enter a short nonempty email and run validation.
- Enter an age below 18; verify the age rule reports an error.
- Enter a malformed phone number; the mask’s validity remains part of the final decision.
- Correct the fields and validate again: stale custom errors must clear.
Step 7Make one change and repeat the tests#
Add a business rule for an upper age limit without bypassing NumericBox.IsValid.
Save a working Git checkpoint first. Change one behavior, Check again and repeat the acceptance checklist. Compare with the downloaded solution if a regression appears.
20f4bfed6b · Build manifest