]> _ Git - cubist_cms-back.git/commitdiff
wip #4370 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 24 Mar 2021 16:31:23 +0000 (17:31 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 24 Mar 2021 16:31:23 +0000 (17:31 +0100)
src/app/Magic/Fields/SelectFromArray.php
src/resources/views/fields/select2_from_array.blade.php

index 8f683342e98a529942cc7af185978b3e963a5bdf..d571dacd451666214c73a9481c59546368484797 100644 (file)
@@ -24,6 +24,14 @@ class SelectFromArray extends Field
         return array_merge(parent::getDefaultAttributes(), ['options' => $this->getOptions(), 'options_aliases' => $this->_options_aliases, 'allows_null' => $this->_allowNull, 'allows_multiple' => $this->_multiple]);
     }
 
+    protected function _postSetAttributes()
+    {
+        parent::_postSetAttributes();
+        if ($this->getAttribute('default') !== null && $this->getAttribute('allows_null')) {
+            $this->setAttribute('allows_null', false);
+        }
+    }
+
     public function getOptions()
     {
         return $this->_options;
index a0dcdaf9c06517731518b40198a49ef9a3b97ff3..d4822947803230d4d8a6191311928619bd108007 100644 (file)
@@ -1,5 +1,5 @@
 @php
-    $o=$field['value'] ?? old(square_brackets_to_dots($field['name']));
+    $o=$field['value'] ?? old(square_brackets_to_dots($field['name']))??$field['default'];
 
     $is_null=null===$o;
     if(!function_exists('select2_from_array_selected')){
 
 <!-- select2 from array -->
 @include('crud::fields.inc.wrapper_start')
-    <label>{!! $field['label'] !!}</label>
-    <select
-        name="{{ $field['name'] }}@if (isset($field['allows_multiple']) && $field['allows_multiple']==true)[]@endif"
-        style="width: 100%"
-        @include('crud::fields.inc.attributes', ['default_class' =>  'form-control select2_from_array'])
-        @if (isset($field['allows_multiple']) && $field['allows_multiple']==true)multiple @endif
-    >
+<label>{!! $field['label'] !!}</label>
+<select
+    name="{{ $field['name'] }}@if (isset($field['allows_multiple']) && $field['allows_multiple']==true)[]@endif"
+    style="width: 100%"
+    @include('crud::fields.inc.attributes', ['default_class' =>  'form-control select2_from_array'])
+    @if (isset($field['allows_multiple']) && $field['allows_multiple']==true)multiple @endif
+>
 
-        @if (isset($field['allows_null']) && $field['allows_null']==true)
-            @if($is_null)
-                <option value="" selected>-</option>
-            @else
-                <option value="">-</option>
-            @endif
+    @if (isset($field['allows_null']) && $field['allows_null']==true)
+        @if($is_null)
+            <option value="" selected>-</option>
+        @else
+            <option value="">-</option>
         @endif
+    @endif
 
-        @if (count($field['options']))
-            @foreach ($field['options'] as $key => $value)
-                @php
-                    $selected=select2_from_array_selected($key,$o);
-                @endphp
-                @if($selected)
-                    <option value="{{ $key }}" selected>{{ $value }}</option>
-                @else
-                    <option value="{{ $key }}">{{ $value }}</option>
-                @endif
-            @endforeach
-        @endif
-    </select>
-
-    {{-- HINT --}}
-    @if (isset($field['hint']))
-        <p class="help-block">{!! $field['hint'] !!}</p>
+    @if (count($field['options']))
+        @foreach ($field['options'] as $key => $value)
+            @php
+                $selected=select2_from_array_selected($key,$o);
+            @endphp
+            @if($selected)
+                <option value="{{ $key }}" selected>{{ $value }}</option>
+            @else
+                <option value="{{ $key }}">{{ $value }}</option>
+            @endif
+        @endforeach
     @endif
+</select>
+
+{{-- HINT --}}
+@if (isset($field['hint']))
+    <p class="help-block">{!! $field['hint'] !!}</p>
+@endif
 @include('crud::fields.inc.wrapper_end')
 
 {{-- ########################################## --}}