Skip to content
This repository
Browse code

Added support for simplified required validation on 'create' or 'upda…

…te'. Added testcases for new 'required' attributes and conflicts with the 'on' key. Fixes #230.
  • Loading branch information...
commit 4f3b2664910104ef86589c75f7158aa5d5cc39eb 1 parent b6f99bc
Thomas Ploch authored February 07, 2012
7  lib/Cake/Model/Model.php
@@ -3016,13 +3016,16 @@ public function invalidFields($options = array()) {
3016 3016
 				}
3017 3017
 				$validator = array_merge($default, $validator);
3018 3018
 
3019  
-				if (!empty($validator['on'])) {
  3019
+				if (!empty($validator['on']) || in_array($validator['required'], array('create', 'update'))) {
3020 3020
 					if ($exists === null) {
3021 3021
 						$exists = $this->exists();
3022 3022
 					}
3023  
-					if (($validator['on'] == 'create' && $exists) || ($validator['on'] == 'update' && !$exists)) {
  3023
+					if ($validator['on'] == 'create' && $exists || $validator['on'] == 'update' && !$exists) {
3024 3024
 						continue;
3025 3025
 					}
  3026
+					if ($validator['required'] == 'create' && !$exists || $validator['required'] == 'update' && $exists) {
  3027
+						$validator['required'] = true;
  3028
+					}
3026 3029
 				}
3027 3030
 
3028 3031
 				$valid = true;
177  lib/Cake/Test/Case/Model/ModelValidationTest.php
@@ -814,4 +814,181 @@ function testStateValidation() {
814 814
 		$this->assertFalse($Article->validates());
815 815
 	}
816 816
 
  817
+/**
  818
+ * Test for 'required' => [create|update] in validation rules.
  819
+ *
  820
+ * @return void
  821
+ */
  822
+	function testStateRequiredValidation() {
  823
+		$this->loadFixtures('Article');
  824
+		$Article = new Article();
  825
+
  826
+		// no title field present
  827
+		$data = array(
  828
+			'Article' => array(
  829
+				'body' => 'Extra Fields Body',
  830
+				'published' => '1'
  831
+			)
  832
+		);
  833
+
  834
+		$Article->validate = array(
  835
+			'title' => array(
  836
+				'notempty' => array(
  837
+					'rule' => 'notEmpty',
  838
+					'required' => 'create'
  839
+				)
  840
+			)
  841
+		);
  842
+
  843
+		$Article->create($data);
  844
+		$this->assertFalse($Article->validates());
  845
+
  846
+		$Article->save(null, array('validate' => false));
  847
+		$data['Article']['id'] = $Article->id;
  848
+		$Article->set($data);
  849
+		$this->assertTrue($Article->validates());
  850
+
  851
+		unset($data['Article']['id']);
  852
+		$Article->validate = array(
  853
+			'title' => array(
  854
+				'notempty' => array(
  855
+					'rule' => 'notEmpty',
  856
+					'required' => 'update'
  857
+				)
  858
+			)
  859
+		);
  860
+
  861
+		$Article->create($data);
  862
+		$this->assertTrue($Article->validates());
  863
+
  864
+		$Article->save(null, array('validate' => false));
  865
+		$data['Article']['id'] = $Article->id;
  866
+		$Article->set($data);
  867
+		$this->assertFalse($Article->validates());
  868
+	}
  869
+
  870
+/**
  871
+ * Test that 'required' and 'on' are not conflicting
  872
+ *
  873
+ * @return void
  874
+ */
  875
+	function testOnRequiredConflictValidation() {
  876
+		$this->loadFixtures('Article');
  877
+		$Article = new Article();
  878
+
  879
+		// no title field present
  880
+		$data = array(
  881
+			'Article' => array(
  882
+				'body' => 'Extra Fields Body',
  883
+				'published' => '1'
  884
+			)
  885
+		);
  886
+
  887
+		$Article->validate = array(
  888
+			'title' => array(
  889
+				'notempty' => array(
  890
+					'rule' => 'notEmpty',
  891
+					'required' => 'create',
  892
+					'on' => 'create'
  893
+				)
  894
+			)
  895
+		);
  896
+
  897
+		$Article->create($data);
  898
+		$this->assertFalse($Article->validates());
  899
+
  900
+		$Article->validate = array(
  901
+			'title' => array(
  902
+				'notempty' => array(
  903
+					'rule' => 'notEmpty',
  904
+					'required' => 'update',
  905
+					'on' => 'create'
  906
+				)
  907
+			)
  908
+		);
  909
+
  910
+		$Article->create($data);
  911
+		$this->assertTrue($Article->validates());
  912
+
  913
+		$Article->validate = array(
  914
+			'title' => array(
  915
+				'notempty' => array(
  916
+					'rule' => 'notEmpty',
  917
+					'required' => 'create',
  918
+					'on' => 'update'
  919
+				)
  920
+			)
  921
+		);
  922
+
  923
+		$Article->create($data);
  924
+		$this->assertTrue($Article->validates());
  925
+
  926
+		$Article->validate = array(
  927
+			'title' => array(
  928
+				'notempty' => array(
  929
+					'rule' => 'notEmpty',
  930
+					'required' => 'update',
  931
+					'on' => 'update'
  932
+				)
  933
+			)
  934
+		);
  935
+
  936
+		$Article->create($data);
  937
+		$this->assertTrue($Article->validates());
  938
+
  939
+		$Article->validate = array(
  940
+			'title' => array(
  941
+				'notempty' => array(
  942
+					'rule' => 'notEmpty',
  943
+					'required' => 'create',
  944
+					'on' => 'create'
  945
+				)
  946
+			)
  947
+		);
  948
+
  949
+		$Article->save(null, array('validate' => false));
  950
+		$data['Article']['id'] = $Article->id;
  951
+		$Article->set($data);
  952
+		$this->assertTrue($Article->validates());
  953
+
  954
+		$Article->validate = array(
  955
+			'title' => array(
  956
+				'notempty' => array(
  957
+					'rule' => 'notEmpty',
  958
+					'required' => 'update',
  959
+					'on' => 'create'
  960
+				)
  961
+			)
  962
+		);
  963
+
  964
+		$Article->set($data);
  965
+		$this->assertTrue($Article->validates());
  966
+
  967
+		$Article->validate = array(
  968
+			'title' => array(
  969
+				'notempty' => array(
  970
+					'rule' => 'notEmpty',
  971
+					'required' => 'create',
  972
+					'on' => 'update'
  973
+				)
  974
+			)
  975
+		);
  976
+
  977
+		$Article->set($data);
  978
+		$this->assertTrue($Article->validates());
  979
+
  980
+		$Article->validate = array(
  981
+			'title' => array(
  982
+				'notempty' => array(
  983
+					'rule' => 'notEmpty',
  984
+					'required' => 'update',
  985
+					'on' => 'update'
  986
+				)
  987
+			)
  988
+		);
  989
+
  990
+		$Article->set($data);
  991
+		$this->assertFalse($Article->validates());
  992
+	}
  993
+
817 994
 }

0 notes on commit 4f3b266

Please sign in to comment.
Something went wrong with that request. Please try again.