newbie360's avatar

How to get the custom validation rule error key?

For example in Filament has a form with post title input

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('title')
                    ->required()
                    ->string()
                    ->rules([
                        'between:8,255',
                        new NotHasSymbol(),
                    ])
                    ->unique(ignoreRecord: true),
            ]);
    }

and write a Test with use https://pestphp.com/docs/datasets

test('can not pass validation', function (
    string $attribute,
    string | array | null $value,
    string $rule
) {
    $component = Livewire::test(CreatePost::class)
        ->fillForm([
            $attribute => $value,
        ])
        ->call('create');

    $component->assertHasFormErrors([$attribute => $rule]);
})
->with([
    ['title', null, 'required'],
    ['title', ['array'], 'string'],
    ['title', 'a', 'between'],
    ['title', fn() => str('a')->repeat(256)->toString(), 'between'],
    ['title', '@#', 'if NotHasSymbol rule fail, what is the error key ??'], // <-----------
]);

Testing result

  ✓ can not pass validation with ('title', null, 'required')
  ✓ can not pass validation with ('title', ['array'], 'string')
  ✓ can not pass validation with ('title', 'a', 'between')
  ✓ can not pass validation with ('title', Closure, 'between')
0 likes
2 replies
aleahy's avatar
aleahy
Best Answer
Level 25

Have you tried:

['title', '@#', NotHasSymbol::class]
1 like
newbie360's avatar

@aleahy Cool it work!! Thanks

finally code

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('title')
                    ->rules([
                        'bail',
                        'required',
                        'string',
                        'min:8',
                        'max:255',
                        new NotHasSymbol(),
                    ])
                    ->unique(ignoreRecord: true),

                Forms\Components\TextInput::make('body')
                    ->rules([
                        'bail',
                        'required',
                        'string',
                        'min:10',
                        'max:65535',
                    ]),
            ]);
    }

Test

test('can not pass the validation', function (
    string $attribute,
    string | array | null $value,
    string $rule
) {
    $component = Livewire::test(CreatePost::class)
        ->fillForm([
            $attribute => $value,
        ])
        ->call('create');

    $component->assertHasFormErrors([$attribute => $rule]);
})
->with([
    'title has required rule' => ['title', null, 'required'],
    'title has string rule' => ['title', ['array'], 'string'],
    'title has min rule' => ['title', 'a', 'min'],
    'title has max rule' => ['title', fn() => str('a')->repeat(256)->toString(), 'max'],
    'title has NotHasSymbol rule' => ['title', '@#', NotHasSymbol::class],
    'title has unique rule' => ['title', fn() => Post::factory()->create()->title, 'unique'],

    'body has required rule' => ['body', null, 'required'],
    'body has string rule' => ['body', ['array'], 'string'],
    'body has min rule' => ['body', 'a', 'min'],
    'body has max rule' => ['body', fn() => str('a')->repeat(65536)->toString(), 'max'],
]);

Test result

  ✓ can not pass the validation with dataset "title has required rule"
  ✓ can not pass the validation with dataset "title has string rule"
  ✓ can not pass the validation with dataset "title has min rule"
  ✓ can not pass the validation with dataset "title has max rule"
  ✓ can not pass the validation with dataset "title has NotHasSymbol rule"
  ✓ can not pass the validation with dataset "title has unique rule"
  ✓ can not pass the validation with dataset "body has required rule"
  ✓ can not pass the validation with dataset "body has string rule"
  ✓ can not pass the validation with dataset "body has min rule"
  ✓ can not pass the validation with dataset "body has max rule"

Please or to participate in this conversation.