Validation
Ensure data integrity with validation rules
Validation Guide
Learn how to implement validation rules to ensure data quality and integrity in your Appivo applications.
Validation Overview
Appivo provides validation at multiple levels:
| Level | When It Runs | Purpose |
|---|---|---|
| Attribute | On model save | Ensure field values are valid |
| Widget | On user input | Immediate feedback |
| Form | On submit | Validate complete form |
| Business Rule | In actions | Custom business logic |
Attribute Validation
Built-in Validations
Configure validations when defining model attributes:
| Validation | Description | Applies To |
|---|---|---|
| Required | Must have a value | All types |
| Unique | No duplicates | All types |
| Min Length | Minimum characters | Text, Long Text |
| Max Length | Maximum characters | Text, Long Text |
| Min Value | Minimum number | Integer, Decimal |
| Max Value | Maximum number | Integer, Decimal |
| Pattern | Regex matching | Text types |
Validation Examples
Required Email
Attribute: email
Type: Email
Validations:
- Required: true
- Unique: true
Constrained Number
Attribute: quantity
Type: Integer
Validations:
- Required: true
- Min Value: 1
- Max Value: 1000
Pattern Matching
Attribute: product_code
Type: Text
Validations:
- Required: true
- Pattern: ^[A-Z]{3}-[0-9]{4}$
- Error Message: "Must be format ABC-1234"
Widget Validation
Input Widget Validation
Configure validation on input widgets:
| Property | Description |
|---|---|
| required | Field is mandatory |
| minLength | Minimum input length |
| maxLength | Maximum input length |
| pattern | Regex pattern |
| errorMessage | Custom error text |
Real-Time Feedback
Widgets provide immediate validation feedback:
- Red border on invalid input
- Error message displayed below
- Icon indicating status
- Tooltip with details
Validation Timing
| Setting | Behavior |
|---|---|
| On Blur | Validate when field loses focus |
| On Change | Validate on every keystroke |
| On Submit | Validate only when form submits |
Form Validation
Complete Form Validation
When a form is submitted:
- All required fields checked
- Format validations applied
- Cross-field validations run
- Business rules executed
- Errors collected and displayed
Error Display
Options for showing validation errors:
| Style | Description |
|---|---|
| Inline | Error below each field |
| Summary | All errors at top |
| Both | Inline and summary |
| Toast | Notification popup |
Handling Validation Errors
Best practices for error handling:
- Show clear, specific error messages
- Highlight the invalid field
- Focus on first error field
- Don't clear valid data
- Allow correction and retry
Cross-Field Validation
Comparing Fields
Validate one field against another:
Password Confirmation
Attribute: confirm_password
Validation:
- Must equal: password
- Error: "Passwords must match"
Date Range
Attribute: end_date
Validation:
- Must be after: start_date
- Error: "End date must be after start date"
Conditional Validation
Make validation depend on other values:
Attribute: business_id
Validation:
- Required if: customer_type equals "Business"
- Error: "Business ID required for business customers"
Business Rule Validation
Validation in Actions
Add custom validation in SCRIPT actions:
exports.execute = async function(context, params) {
const data = params.record;
// Custom validation
if (data.quantity > data.stock_available) {
return {
success: false,
error: 'Quantity exceeds available stock'
};
}
if (data.discount > 50 && !params.user.hasRole('Manager')) {
return {
success: false,
error: 'Discounts over 50% require manager approval'
};
}
return { success: true };
};
Async Validation
Validate against external data:
// Check if customer has credit
const customer = await context.getRecord('Customer', data.customer_id);
if (data.order_total > customer.credit_limit) {
return {
success: false,
error: 'Order exceeds customer credit limit'
};
}
Error Messages
Writing Good Error Messages
| Bad | Good |
|---|---|
| "Invalid" | "Email must be a valid address" |
| "Error" | "Please enter a quantity between 1 and 100" |
| "Required" | "Customer name is required" |
| "Wrong format" | "Phone must be in format (123) 456-7890" |
Localized Messages
Support multiple languages:
Validation: Required
Messages:
en: "This field is required"
sv: "Detta fält är obligatoriskt"
de: "Dieses Feld ist erforderlich"
Common Validation Patterns
Email Validation
Type: Email
Validations:
- Required: true
- Unique: true
- Pattern: ^[^\s@]+@[^\s@]+\.[^\s@]+$
Phone Validation
Type: Phone
Validations:
- Pattern: ^\+?[0-9\s-]{10,}$
- Error: "Please enter a valid phone number"
URL Validation
Type: URL
Validations:
- Pattern: ^https?://[^\s]+$
- Error: "Please enter a valid URL starting with http:// or https://"
Password Strength
Attribute: password
Validations:
- Min Length: 8
- Pattern: (?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])
- Error: "Password must include uppercase, lowercase, and numbers"
Best Practices
Validation Design
- Validate early - Catch errors as soon as possible
- Be specific - Tell users exactly what's wrong
- Be helpful - Explain how to fix the error
- Be consistent - Use same patterns throughout
- Don't over-validate - Only validate what matters
User Experience
- Don't prevent typing
- Show validation status visually
- Allow form submission to see all errors
- Preserve valid input on errors
- Make corrections easy
Performance
- Debounce onChange validation
- Avoid expensive async validation on keystroke
- Cache validation results when appropriate
- Batch multiple validations
Next Steps
- Data Modeling - Define validation at model level
- User Interfaces - Configure widget validation
- Rules and Actions - Business rule validation