@@ -112,7 +112,7 @@ public function bake($name)
112112 $ primaryKey = $ this ->getPrimaryKey ($ model );
113113 $ displayField = $ this ->getDisplayField ($ model );
114114 $ fields = $ this ->getFields ($ model );
115- $ validation = $ this ->getValidation ($ model );
115+ $ validation = $ this ->getValidation ($ model, $ associations );
116116 $ rulesChecker = $ this ->getRules ($ model , $ associations );
117117 $ behaviors = $ this ->getBehaviors ($ model );
118118
@@ -444,9 +444,10 @@ public function getHiddenFields($model)
444444 * Generate default validation rules.
445445 *
446446 * @param \Cake\ORM\Table $model The model to introspect.
447+ * @param array $associations The associations list.
447448 * @return array The validation rules.
448449 */
449- public function getValidation ($ model )
450+ public function getValidation ($ model, $ associations = [] )
450451 {
451452 if (!empty ($ this ->params ['no-validation ' ])) {
452453 return [];
@@ -459,8 +460,16 @@ public function getValidation($model)
459460
460461 $ validate = [];
461462 $ primaryKey = (array )$ schema ->primaryKey ();
462-
463+ $ foreignKeys = [];
464+ if (isset ($ associations ['belongsTo ' ])) {
465+ foreach ($ associations ['belongsTo ' ] as $ assoc ) {
466+ $ foreignKeys [] = $ assoc ['foreignKey ' ];
467+ }
468+ }
463469 foreach ($ fields as $ fieldName ) {
470+ if (in_array ($ fieldName , $ foreignKeys )) {
471+ continue ;
472+ }
464473 $ field = $ schema ->column ($ fieldName );
465474 $ validation = $ this ->fieldValidation ($ fieldName , $ field , $ primaryKey );
466475 if (!empty ($ validation )) {
0 commit comments