Skip to content

Rules not exported after non-static rule #278

Open
@jtojnar

Description

@jtojnar

Version: 3.1.3

Bug Description

Since 1592cf6, no rules after a callback rule are passed to the frontend.

I understand that this is to prevent conflict between filters (which are just impure rules) and other rules but it leads to confusing behaviour, where rules suddenly terminate from frontend point of view, even if the callback is just a pure function.

Steps To Reproduce

test('', function () {
	$form = new Form;
	$input = $form->addText('text');
	$input->addRule(function ($input) {
		return true;
	});
	$input->addRule(Form::EMAIL);
	Assert::same([
		['op' => ':email', 'msg' => 'Please enter a valid email address.'],
	], Helpers::exportRules($input->getRules()));
});

Expected Behavior

I think we should give callbacks passed to addRule a benefit of a doubt (that they might be pure) and not terminate the rule export.

But yeah, I understand the paranoid point of view (all functions are potentially effectful/impure and we cannot trust them not to modify the control) since PHP cannot distinguish pure and effectful functions.

Possible Solutions

  • We could add isFilter/impure property to Rule, set it to true in addFilter, and terminate export only for impure rules.
  • The behaviour could be further emphasised in docs and the hack would be recommended:
    $this->addCondition(true) // not to block the export of rules to JS

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions