Skip to content

Custom Validation Rules not processed if no value in field #11151

@jonathan-bird

Description

@jonathan-bird

Bug description

If you create a new custom validation rule and set it in a blueprint. Eg.

validate:
  - 'new App\Rules\TemplateDefaultOrNotSet()'

Then it won't run the validate() method unless there's a value set in the field.

It should be run regardless of whether there is a value.

How to reproduce

Create a new custom rule:

<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\ValidationRule;

class TemplateDefaultOrNotSet implements ValidationRule, DataAwareRule
{
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        $fail('It will not hit this.');
     }
}

Then go to blueprint yaml file and set this (or just set new App\Rules\TemplateDefaultOrNotSet() in the Blueprint view):

validate:
  - 'new App\Rules\TemplateDefaultOrNotSet()'

And then once saved, create a new page and don't set any value in that field, and you will notice that it won't hit that validate() method (although it will hit a __construct() if set in the custom rule)

Logs

No response

Environment

Environment
Application Name: xxxxx
Laravel Version: 11.33.2
PHP Version: 8.3.13
Composer Version: 2.7.7
Environment: local
Debug Mode: ENABLED
URL: xxxx-statamic.test
Maintenance Mode: OFF
Timezone: Australia/Brisbane
Locale: en

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: NOT CACHED

Drivers
Broadcasting: log
Cache: redis
Database: mysql
Logs: stack / daily
Mail: smtp
Queue: sync
Session: redis

Livewire
Livewire: v3.5.12

Statamic
Addons: 7
Sites: 1
Stache Watcher: Enabled (auto)
Static Caching: Disabled
Version: 5.38.1 PRO

Statamic Addons
aerni/advanced-seo: 2.9.3
aerni/livewire-forms: 9.4.1
digital-bird/statamic-toc: 1.0.2
jonassiewertsen/statamic-livewire: 3.8.1
mitydigital/sitemapamic: 3.2.0
mitydigital/statamic-two-factor: 2.3.0

Installation

Fresh statamic/statamic site via CLI

Additional details

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions