File tree Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change 83
83
* $primary parameter indicates whether or not this is the root query,
84
84
* or an associated query.
85
85
*
86
+ * - `buildRules(Event $event, RulesChecker $rules)`
87
+ * Allows listeners to modify the rules checker by adding more rules.
88
+ *
86
89
* - `beforeRules(Event $event, Entity $entity, RulesChecker $rules)`
87
90
* Fired before an entity is validated using the rules checker. By stopping this event,
88
91
* you can return the final value of the rules checking operation.
@@ -1876,7 +1879,9 @@ public function rulesChecker() {
1876
1879
if ($ this ->_rulesChecker !== null ) {
1877
1880
return $ this ->_rulesChecker ;
1878
1881
}
1879
- return $ this ->_rulesChecker = $ this ->buildRules (new RulesChecker (['repository ' => $ this ]));
1882
+ $ this ->_rulesChecker = $ this ->buildRules (new RulesChecker (['repository ' => $ this ]));
1883
+ $ this ->dispatchEvent ('Model.buildRules ' , ['rules ' => $ this ->_rulesChecker ]);
1884
+ return $ this ->_rulesChecker ;
1880
1885
}
1881
1886
1882
1887
/**
Original file line number Diff line number Diff line change @@ -429,4 +429,25 @@ public function testUseAfterRules() {
429
429
430
430
$ this ->assertSame ($ entity , $ table ->save ($ entity ));
431
431
}
432
+
433
+ /**
434
+ * Tests that rules can be changed using the buildRules event
435
+ *
436
+ * @group save
437
+ * @return void
438
+ */
439
+ public function testUseBuildRulesEvent () {
440
+ $ entity = new Entity ([
441
+ 'title ' => 'An Article ' ,
442
+ 'author_id ' => 500
443
+ ]);
444
+
445
+ $ table = TableRegistry::get ('Articles ' );
446
+ $ table ->eventManager ()->attach (function ($ event , $ rules ) {
447
+ $ rules ->add ($ rules ->existsIn ('author_id ' , TableRegistry::get ('Authors ' ), 'Nope ' ));
448
+ }, 'Model.buildRules ' );
449
+
450
+ $ this ->assertFalse ($ table ->save ($ entity ));
451
+ }
452
+
432
453
}
You can’t perform that action at this time.
0 commit comments