Skip to content

Filled validation in custom Nette\Forms\Control instance #346

Closed

Description

@hrach

I have a custom form control implementing Nette\Forms\Control interface.

I want to add a common rule to check if that control is filled. Sadly, the default implementation delegates to the static Validator class that requires a BaseControl subtype.

public static function validateFilled(Controls\BaseControl $control): bool

I worked around this by providing custom rules that override the filled check:

class CustomControlRules extends Rules
{
	public static function validateRule(Rule $rule): bool
	{
		if ($rule->validator === Form::Filled) {
			$control = $rule->control;
			if (!$control instanceof \MyCustomImplementation) {
				throw new InvalidStateException();
			}
			return $control->isFilled();
		} else {
			return parent::validateRule($rule);
		}
	}
}

But, this is getting forbidden by the recent (Forms 3.2) changes that are making the Rules class final.

The simple solution is to remove the final once again, but since I got to report this, I'd ask you to consider some proper LSP solution -> ideally moving the required methods to the interface.

Activity

  1. dg commented on Nov 25, 2025

    @dg
    Member

    Yeah, feel free to suggest some proper solutions.

  2. hrach commented on Nov 29, 2025

    @hrach
    ContributorAuthor

    Which solution do you prefer?

    1. Add isFilled() to Control (BC break)
    2. Add isFilled() to Control but commented out (not a BC break, at least for now), add use method_exists
    3. Add ValidatedControl interface and implement in BaseControl
      a. add there just validateFilled & validateMinMax (to extract this datetime logic)
      b. add there more
  3. hrach commented on Nov 29, 2025

    @hrach
    ContributorAuthor

    Another option implemented in #347.

  4. hrach commented on Feb 20, 2026

    @hrach
    ContributorAuthor

    @dg any opinion on this? thx.

  5. hrach commented on Apr 30, 2026

    @hrach
    ContributorAuthor

    @dg gentle reminder

  6. added 2 commits that reference this issue on Oct 28, 2026
    59e21b9
    34d5359
  7. added 4 commits that reference this issue on Oct 29, 2026
    5aeea0a
    84ebecb
    f8a185d
    1fd46a4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions