@@ -189,7 +189,7 @@ abstract class BaseModel
189
189
190
190
/**
191
191
* Whether rules should be removed that do not exist
192
- * in the passed in data. Used between inserts/ updates.
192
+ * in the passed data. Used in updates.
193
193
*
194
194
* @var bool
195
195
*/
@@ -701,13 +701,23 @@ public function insert($data = null, bool $returnID = true)
701
701
{
702
702
$ this ->insertID = 0 ;
703
703
704
+ // Set $cleanValidationRules to false temporary.
705
+ $ cleanValidationRules = $ this ->cleanValidationRules ;
706
+ $ this ->cleanValidationRules = false ;
707
+
704
708
$ data = $ this ->transformDataToArray ($ data , 'insert ' );
705
709
706
710
// Validate data before saving.
707
- if (! $ this ->skipValidation && ! $ this ->cleanRules ()->validate ($ data )) {
711
+ if (! $ this ->skipValidation && ! $ this ->validate ($ data )) {
712
+ // Restore $cleanValidationRules
713
+ $ this ->cleanValidationRules = $ cleanValidationRules ;
714
+
708
715
return false ;
709
716
}
710
717
718
+ // Restore $cleanValidationRules
719
+ $ this ->cleanValidationRules = $ cleanValidationRules ;
720
+
711
721
// Must be called first so we don't
712
722
// strip out created_at values.
713
723
$ data = $ this ->doProtectFields ($ data );
@@ -773,6 +783,10 @@ public function insert($data = null, bool $returnID = true)
773
783
*/
774
784
public function insertBatch (?array $ set = null , ?bool $ escape = null , int $ batchSize = 100 , bool $ testing = false )
775
785
{
786
+ // Set $cleanValidationRules to false temporary.
787
+ $ cleanValidationRules = $ this ->cleanValidationRules ;
788
+ $ this ->cleanValidationRules = false ;
789
+
776
790
if (is_array ($ set )) {
777
791
foreach ($ set as &$ row ) {
778
792
// If $data is using a custom class with public or protected
@@ -789,8 +803,11 @@ public function insertBatch(?array $set = null, ?bool $escape = null, int $batch
789
803
$ row = (array ) $ row ;
790
804
}
791
805
792
- // Validate every row..
793
- if (! $ this ->skipValidation && ! $ this ->cleanRules ()->validate ($ row )) {
806
+ // Validate every row.
807
+ if (! $ this ->skipValidation && ! $ this ->validate ($ row )) {
808
+ // Restore $cleanValidationRules
809
+ $ this ->cleanValidationRules = $ cleanValidationRules ;
810
+
794
811
return false ;
795
812
}
796
813
@@ -811,6 +828,9 @@ public function insertBatch(?array $set = null, ?bool $escape = null, int $batch
811
828
}
812
829
}
813
830
831
+ // Restore $cleanValidationRules
832
+ $ this ->cleanValidationRules = $ cleanValidationRules ;
833
+
814
834
return $ this ->doInsertBatch ($ set , $ escape , $ batchSize , $ testing );
815
835
}
816
836
@@ -832,7 +852,7 @@ public function update($id = null, $data = null): bool
832
852
$ data = $ this ->transformDataToArray ($ data , 'update ' );
833
853
834
854
// Validate data before saving.
835
- if (! $ this ->skipValidation && ! $ this ->cleanRules ( true )-> validate ($ data )) {
855
+ if (! $ this ->skipValidation && ! $ this ->validate ($ data )) {
836
856
return false ;
837
857
}
838
858
@@ -906,7 +926,7 @@ public function updateBatch(?array $set = null, ?string $index = null, int $batc
906
926
}
907
927
908
928
// Validate data before saving.
909
- if (! $ this ->skipValidation && ! $ this ->cleanRules ( true )-> validate ($ row )) {
929
+ if (! $ this ->skipValidation && ! $ this ->validate ($ row )) {
910
930
return false ;
911
931
}
912
932
@@ -1027,7 +1047,7 @@ public function onlyDeleted()
1027
1047
public function replace (?array $ data = null , bool $ returnSQL = false )
1028
1048
{
1029
1049
// Validate data before saving.
1030
- if ($ data && ! $ this ->skipValidation && ! $ this ->cleanRules ( true )-> validate ($ data )) {
1050
+ if ($ data && ! $ this ->skipValidation && ! $ this ->validate ($ data )) {
1031
1051
return false ;
1032
1052
}
1033
1053
@@ -1581,7 +1601,11 @@ protected function transformDataToArray($data, string $type): array
1581
1601
// properties representing the collection elements, we need to grab
1582
1602
// them as an array.
1583
1603
if (is_object ($ data ) && ! $ data instanceof stdClass) {
1584
- $ data = $ this ->objectToArray ($ data , ($ type === 'update ' ), true );
1604
+ // If it validates with entire rules, all fields are needed.
1605
+ $ onlyChanged = ($ this ->skipValidation === false && $ this ->cleanValidationRules === false )
1606
+ ? false : ($ type === 'update ' );
1607
+
1608
+ $ data = $ this ->objectToArray ($ data , $ onlyChanged , true );
1585
1609
}
1586
1610
1587
1611
// If it's still a stdClass, go ahead and convert to
0 commit comments