This repository was archived by the owner on Jan 30, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +27
-0
lines changed Expand file tree Collapse file tree 3 files changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -119,6 +119,10 @@ public function isValid($context = null)
119
119
$ allowEmpty = $ this ->allowEmpty ();
120
120
$ continueIfEmpty = $ this ->continueIfEmpty ();
121
121
122
+ if (! $ hasValue && ! $ required ) {
123
+ return true ;
124
+ }
125
+
122
126
if (! $ hasValue && $ required && ! $ this ->hasFallback ()) {
123
127
$ this ->setErrorMessage ('Value is required ' );
124
128
return false ;
Original file line number Diff line number Diff line change @@ -396,6 +396,10 @@ public function isValid($context = null)
396
396
return true ;
397
397
}
398
398
399
+ if (! $ hasValue && ! $ required ) {
400
+ return true ;
401
+ }
402
+
399
403
if (! $ hasValue && $ required ) {
400
404
$ this ->setErrorMessage ('Value is required ' );
401
405
return false ;
Original file line number Diff line number Diff line change @@ -175,6 +175,25 @@ public function testRequiredWithoutFallbackAndValueNotSetThenFail()
175
175
$ this ->assertEquals (['Value is required ' ], $ input ->getMessages (), 'getMessages() value not match ' );
176
176
}
177
177
178
+ public function testNotRequiredWithoutFallbackAndValueNotSetThenIsValid ()
179
+ {
180
+ $ input = $ this ->input ;
181
+ $ input ->setRequired (false );
182
+ $ input ->setAllowEmpty (false );
183
+ $ input ->setContinueIfEmpty (true );
184
+
185
+ // Validator should not to be called
186
+ $ input ->getValidatorChain ()
187
+ ->attach ($ this ->createValidatorMock (null , null ))
188
+ ;
189
+ $ this ->assertTrue (
190
+ $ input ->isValid (),
191
+ 'isValid() should be return always true when is not required, and no data is set. Detail: ' .
192
+ json_encode ($ input ->getMessages ())
193
+ );
194
+ $ this ->assertEquals ([], $ input ->getMessages (), 'getMessages() should be empty because the input is valid ' );
195
+ }
196
+
178
197
public function testNotEmptyValidatorNotInjectedIfContinueIfEmptyIsTrue ()
179
198
{
180
199
$ input = new Input ('foo ' );
You can’t perform that action at this time.
0 commit comments