@@ -112,20 +112,33 @@ public function setValues($data, bool $erase = false)
112112 public function getValues ($ returnType = null , array $ controls = null )
113113 {
114114 $ form = $ this ->getForm (false );
115- if ($ form && $ form ->isSubmitted () && !$ form ->isValid ()) {
116- trigger_error (__METHOD__ . '() invoked but the form is not valid. ' , E_USER_WARNING );
115+ if ($ form && ($ submitter = $ form ->isSubmitted ())) {
116+ if (!$ this ->isValid ()) {
117+ trigger_error (__METHOD__ . '() invoked but the form is not valid. ' , E_USER_WARNING );
118+ }
119+ if ($ controls === null && $ submitter instanceof SubmitterControl) {
120+ $ controls = $ submitter ->getValidationScope ();
121+ }
117122 }
123+ $ returnType = $ returnType === true ? self ::ARRAY : $ returnType ;
124+ return $ this ->getUnsafeValues ($ returnType , $ controls );
125+ }
118126
119- if ($ returnType === self ::ARRAY || $ returnType === true || $ this ->mappedType === self ::ARRAY ) {
120- $ returnType = self ::ARRAY ;
121- $ obj = new \stdClass ;
122127
123- } elseif (is_object ($ returnType )) {
128+ /**
129+ * Returns the potentially unvalidated values submitted by the form.
130+ * @param string|object|null $returnType 'array' for array
131+ * @param Control[]|null $controls
132+ * @return object|array
133+ */
134+ public function getUnsafeValues ($ returnType , array $ controls = null )
135+ {
136+ if (is_object ($ returnType )) {
124137 $ obj = $ returnType ;
125138
126139 } else {
127140 $ returnType = ($ returnType ?? $ this ->mappedType ?? ArrayHash::class);
128- $ obj = new $ returnType ;
141+ $ obj = $ returnType === self :: ARRAY ? new \ stdClass : new $ returnType ;
129142 }
130143
131144 $ rc = new \ReflectionClass ($ obj );
@@ -142,7 +155,7 @@ public function getValues($returnType = null, array $controls = null)
142155 $ type = $ returnType === self ::ARRAY && !$ control ->mappedType
143156 ? self ::ARRAY
144157 : ($ rc ->hasProperty ($ name ) ? Nette \Utils \Reflection::getPropertyType ($ rc ->getProperty ($ name )) : null );
145- $ obj ->$ name = $ control ->getValues ($ type , $ controls );
158+ $ obj ->$ name = $ control ->getUnsafeValues ($ type , $ controls );
146159 }
147160 }
148161
@@ -195,8 +208,8 @@ public function validate(array $controls = null): void
195208 $ params = Nette \Utils \Callback::toReflection ($ handler )->getParameters ();
196209 $ types = array_map ([Nette \Utils \Reflection::class, 'getParameterType ' ], $ params );
197210 $ args = isset ($ types [0 ]) && !$ this instanceof $ types [0 ]
198- ? [$ this ->getValues ($ types [0 ])]
199- : [$ this , isset ($ params [1 ]) ? $ this ->getValues ($ types [1 ]) : null ];
211+ ? [$ this ->getUnsafeValues ($ types [0 ], $ controls )]
212+ : [$ this , isset ($ params [1 ]) ? $ this ->getUnsafeValues ($ types [1 ], $ controls ) : null ];
200213 $ handler (...$ args );
201214 }
202215 }
0 commit comments