Skip to content

Commit 3633a04

Browse files
committed
Fixes a bug in the Laravel-Collective selectMonth and selectRange
1 parent b927ed0 commit 3633a04

File tree

3 files changed

+32
-34
lines changed

3 files changed

+32
-34
lines changed

src/HtmlGenerators/LaravelCollectiveHtml.php

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
namespace CrestApps\CodeGenerator\HtmlGenerators;
43

54
use CrestApps\CodeGenerator\HtmlGenerators\HtmlGeneratorBase;
@@ -21,7 +20,6 @@ protected function getFieldMinValueWithName($minValue)
2120
{
2221
return is_null($minValue) ? '' : sprintf(" 'min' => '%s',", $minValue);
2322
}
24-
2523
/**
2624
* Gets the maxValue attribute.
2725
*
@@ -33,7 +31,6 @@ protected function getFieldMaxValueWithName($maxValue)
3331
{
3432
return is_null($maxValue) ? '' : sprintf(" 'max' => '%s',", $maxValue);
3533
}
36-
3734
/**
3835
* Get the minLength attribute.
3936
*
@@ -45,7 +42,6 @@ protected function getFieldMinLengthName($minLength)
4542
{
4643
return empty($minLength) ? '' : sprintf(" 'minlength' => '%s',", $minLength);
4744
}
48-
4945
/**
5046
* Gets the maxLength attribute.
5147
*
@@ -57,7 +53,6 @@ protected function getFieldMaxLengthName($maxLength)
5753
{
5854
return empty($maxLength) ? '' : sprintf(" 'maxlength' => '%s',", $maxLength);
5955
}
60-
6156
/**
6257
* Gets the required attribute.
6358
*
@@ -69,7 +64,6 @@ protected function getFieldRequired($required)
6964
{
7065
return $required ? sprintf(" 'required' => %s,", ($required ? 'true' : 'false')) : '';
7166
}
72-
7367
/**
7468
* Get the placeholder attribute.
7569
*
@@ -81,7 +75,6 @@ protected function getFieldPlaceHolder(Label $placeholder = null)
8175
{
8276
return is_null($placeholder) ? '' : sprintf(" 'placeholder' => %s,", $this->getTitle($placeholder));
8377
}
84-
8578
/**
8679
* Get the placeholder attribute for a menu.
8780
*
@@ -94,7 +87,6 @@ protected function getFieldPlaceHolderForMenu(Label $placeholder = null, $name =
9487
{
9588
return $this->getFieldPlaceHolder($placeholder);
9689
}
97-
9890
/**
9991
* Get the multiple attribute.
10092
*
@@ -106,7 +98,6 @@ protected function getFieldMultiple($isMulti)
10698
{
10799
return $isMulti ? "'multiple' => 'multiple'," : '';
108100
}
109-
110101
/**
111102
* It gets converts an array to a stringbase array for the views.
112103
*
@@ -119,12 +110,9 @@ protected function getFieldItems(Field $field)
119110
if ($field->hasForeignRelation() && $field->isOnFormView) {
120111
return sprintf('$%s', $field->getForeignRelation()->getCollectionName());
121112
}
122-
123113
$labels = $field->getOptionsByLang();
124-
125114
return sprintf('[%s]', implode(',' . PHP_EOL, $this->getKeyValueStringsFromLabels($labels)));
126115
}
127-
128116
/**
129117
* Gets a plain title from a giving label.
130118
*
@@ -137,7 +125,6 @@ protected function getPlainTitle(Label $label, $raw = false)
137125
{
138126
return sprintf(!$raw ? "'%s'" : "%s", $label->text);
139127
}
140-
141128
/**
142129
* Gets the fields value
143130
*
@@ -151,13 +138,10 @@ protected function getFieldValue($value, $name)
151138
{
152139
if (!is_null($value)) {
153140
$modelVariable = $this->getSingularVariable($this->modelName);
154-
155141
return sprintf("(!isset(\$%s->%s) ? '%s' : null)", $modelVariable, $name, $value);
156142
}
157-
158143
return 'null';
159144
}
160-
161145
/**
162146
* Gets checked item attribute.
163147
*
@@ -175,7 +159,6 @@ protected function getCheckedItem($value, $name, $defaultValue)
175159
$value
176160
);
177161
}
178-
179162
/**
180163
* Gets selected value attribute.
181164
*
@@ -187,7 +170,7 @@ protected function getCheckedItem($value, $name, $defaultValue)
187170
*/
188171
protected function getSelectedValue($name, $valueAccessor, $defaultValue)
189172
{
190-
return sprintf(" (%s == %s ? true : null) ", $this->getRawOptionValue($name, $defaultValue), $valueAccessor);
173+
return sprintf(" (%s == %s ? true : null) ", $this->getRawOptionValue($name, 'null'), 'null');
191174
}
192175

193176
/**
@@ -203,7 +186,6 @@ protected function getMultipleCheckedItem($value, $name, $defaultValue)
203186
{
204187
return sprintf(" (%s ? true : null) ", $this->getMultipleRawOptionValue($name, $value, $defaultValue));
205188
}
206-
207189
/**
208190
* Gets a raw value for a giving field's name.
209191
*
@@ -215,12 +197,10 @@ protected function getMultipleCheckedItem($value, $name, $defaultValue)
215197
protected function getRawOptionValue($name, $value)
216198
{
217199
$modelVariable = $this->getSingularVariable($this->modelName);
218-
219-
$valueString = is_null($value) ? 'null' : sprintf("'%s'", $value);
220-
221-
return sprintf("old('%s', isset(\$%s->%s) ? \$%s->%s : %s)", $name, $modelVariable, $name, $modelVariable, $name, $valueString);
200+
$valueString = is_null($value) || $value == 'null' ? 'null' : sprintf("'%s'", $value);
201+
$accessor = $this->getDefaultValueAccessor($modelVariable, $name, $valueString);
202+
return sprintf("old('%s', %s)", $name, $accessor);
222203
}
223-
224204
/**
225205
* Gets a raw value for a giving field's name.
226206
*
@@ -234,21 +214,37 @@ protected function getMultipleRawOptionValue($name, $value, $defaultValue)
234214
{
235215
$modelVariable = $this->getSingularVariable($this->modelName);
236216
$valueString = 'null';
237-
238217
if (!is_null($value)) {
239218
$valueString = starts_with('$', $value) ? sprintf("%s", $value) : sprintf("'%s'", $value);
240219
}
241-
242220
$defaultValueString = '[]';
243-
244221
if (!empty($defaultValue)) {
245222
$joinedValues = implode(',', Helpers::wrapItems((array) $defaultValue));
246223
$defaultValueString = sprintf('[%s]', $joinedValues);
247224
}
248-
249-
return sprintf("in_array(%s, old('%s', isset(\$%s->%s) ? \$%s->%s : %s))", $valueString, $name, $modelVariable, $name, $modelVariable, $name, $defaultValueString);
225+
$accessor = $this->getDefaultValueAccessor($modelVariable, $name, $defaultValueString);
226+
return sprintf("in_array(%s, old('%s', %s))", $valueString, $name, $accessor);
227+
}
228+
/**
229+
* Gets the best value accessor for the view
230+
*
231+
* @param string $modelVariable
232+
* @param string $property
233+
* @param string $value
234+
*
235+
* @return string
236+
*/
237+
protected function getDefaultValueAccessor($modelVariable, $property, $value)
238+
{
239+
if (Helpers::isNewerThanOrEqualTo('5.5')) {
240+
$template = sprintf('optional($%s)->%s', $modelVariable, $property);
241+
if (is_null($value) || in_array($value, ['null', '[]'])) {
242+
return $template;
243+
}
244+
return $template . ' ?: ' . $value;
245+
}
246+
return sprintf("isset(\$%s->%s) ? \$%s->%s : %s", $modelVariable, $property, $modelVariable, $property, $value);
250247
}
251-
252248
/**
253249
* Gets selected value attribute.
254250
*
@@ -262,7 +258,6 @@ protected function getMultipleSelectedValue($name, $valueAccessor, $defaultValue
262258
{
263259
return sprintf(" (%s ? true : null) ", $name);
264260
}
265-
266261
/**
267262
* Gets the html steps attribute.
268263
*
@@ -274,7 +269,6 @@ protected function getStepsValue($value)
274269
{
275270
return ($value) > 0 ? "'step' => \"any\"," : '';
276271
}
277-
278272
/**
279273
* Gets an instance of ViewLabelsGenerator
280274
*

src/HtmlGenerators/StandardHtml.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,11 @@ protected function getMultipleRawOptionValue($name, $value, $defaultValue)
289289

290290
$accessor = $this->getDefaultValueAccessor($modelVariable, $name, $defaultValueString);
291291

292-
return sprintf("in_array(%s, old('%s', %s))", $valueString, $name, $accessor);
292+
if (Helpers::isNewerThanOrEqualTo('5.5')) {
293+
return sprintf("in_array(%s, old('%s', %s) ?? [])", $valueString, $name, $accessor);
294+
}
295+
296+
return sprintf("in_array(%s, (array) old('%s', %s))", $valueString, $name, $accessor);
293297
}
294298

295299
/**

src/Traits/CommonCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ protected function getPathToTemplates($template = null)
541541
$path = Helpers::getPathWithSlash($basePath);
542542

543543
if (!$this->isFileExists($path)) {
544-
throw new Exception('Invalid template name or the templates is invalid. Make sure the following path exists: "' . $path . '"');
544+
throw new Exception('Invalid template. Make sure the following path exists: "' . $path . '"');
545545
}
546546

547547
return $path;

0 commit comments

Comments
 (0)