Milk Admin
Getting started
Abstracts Class
Advanced
Modules
Expressions
Extensions
Form
List
Model
Related Content
Complete Examples
1) Total calculation and discount
Model:
public function configure($rule)
{
$rule->int('qty')->required();
$rule->float('price')->required();
$rule->bool('is_vip');
$rule->float('subtotal')->calcExpr('[qty] * [price]');
$rule->float('discount')->calcExpr('IF [is_vip] == 1 THEN ROUND([subtotal] * 0.10, 2) ELSE 0 ENDIF');
$rule->float('total')->calcExpr('[subtotal] - [discount]');
$rule->string('invoice_note')
->requireIf('[is_vip] == 1')
->error('Notes required for VIP customers');
}
FormBuilder:
$form->field('subtotal')->calcExpr('[qty] * [price]')->disabled();
$form->field('discount')->calcExpr('IF [is_vip] == 1 THEN ROUND([subtotal] * 0.10, 2) ELSE 0 ENDIF')->disabled();
$form->field('total')->calcExpr('[subtotal] - [discount]')->disabled();
$form->field('invoice_note')->requireIf('[is_vip] == 1', 'Notes required for VIP customers');
2) Visibility and conditional required
Show and make VAT number required only if customer type is "company".
// Model
$rule->string('customer_type')->required();
$rule->string('vat_number')->requireIf('[customer_type] == "company"');
// FormBuilder
$form->showIf('vat_number', '[customer_type] == "company"');
$form->field('vat_number')->requireIf('[customer_type] == "company"', 'VAT number required');
3) Date validation
Ensure that the end date is equal to or later than the start date.
// Model
$rule->date('start_date')->required();
$rule->date('end_date')
->validateExpr('[start_date] <= [end_date]', 'End date must be later');
// FormBuilder
$form->field('end_date')
->validateExpr('[start_date] <= [end_date]', 'End date must be later');
Tip: use the same expression in Model and FormBuilder to get consistent validation on both levels.
Loading...