Skip to content

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

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

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