There are 2 ways to add field validation to blocks. You have to use one or the other. If the blade directive is present it will be used over the class.
The first and easiest option is to add the directives to your block form blade file:
There are 2 possible directives to use, these can be combined.
@twillBlockValidationRules
for validating non translatable fields.
@twillBlockValidationRulesForTranslatedFields
for validating translatable fields.
Both take an array of [field => rules]
values. All Laravel validation rules are supported.
Example:
1@twillBlockValidationRules([2 'text' => 'required'3])4@twillBlockValidationRulesForTranslatedFields([5 'title' => 'required'6])
If you need more control you can use the block class to set validation rules, or even hook into the validation process.
See Block classes documentation for more details about the block class.
1<?php 2 3namespace App\Twill\Block; 4 5use A17\Twill\Services\Blocks\Block; 6 7class ExampleBlock extends Block 8{ 9 public $rulesForTranslatedFields = [10 'title' => 'required|string',11 ];12 13 public $rules = [14 'text' => 'required',15 ];16}