PHP 带有自由文本的 select 选项的旧值

PHP Old value for select option with free-text

我有一个 select 选项,带有自由文本选项,例如 this radio button with free text

<div class="form-body">
    <div class="form-group row @error('warna') has-error @enderror">
        <label class="control-label text-right col-md-3">Buta Warna</label>
        <div class="col-md-3">
            <input type=radio id="warna1" name="warna" value="Negatif"
                @if(isset($mcu_form_tensi)){{ $mcu_form_tensi->warna == 'Negatif' ? 'checked' : ''}}@endif>
            Negatif</option><br>
            <input type=radio id="warna2" name="warna" value="Partial"
                @if(isset($mcu_form_tensi)){{ $mcu_form_tensi->warna == 'Partial' ? 'checked' : ''}}@endif>
            Partial</option><br>
            <input type=radio id="warna3" name="warna" value="Total"
                @if(isset($mcu_form_tensi)){{ $mcu_form_tensi->warna == 'Total' ? 'checked' : ''}}@endif>
            Total</option><br>
            <input type=radio id="warna4" name="warna" value="Other" @if(isset($mcu_form_tensi))
                @if($mcu_form_tensi->warna != 'Negatif' && $mcu_form_tensi->warna != 'Partial' &&
            $mcu_form_tensi->warna != 'Total') checked @endif @endif> Other</option><br>

            <input id="txt_warna_other" type="text" name="warna" placeholder=""
                class="form-control @error('warna') has-error @enderror" autocomplete="warna"
                value="@if(isset($mcu_form_tensi)) {{ $mcu_form_tensi->warna }} @endif"
                @if(isset($mcu_form_tensi)) @if($mcu_form_tensi->warna == 'Negatif' ||
            $mcu_form_tensi->warna == 'Partial' || $mcu_form_tensi->warna == 'Total') readonly @endif
            @endif>


            @error('warna')
            <div class="text-danger">
                <small>{{ $message }}</small>
            </div>
            @enderror
        </div>
    </div>
</div>

如果新记录验证失败,我无法保留旧值。 它一直返回选择 none,所以用户必须重新选择。 请帮忙,这样用户就不需要再次输入,他们只输入那些无效的输入。

这是我的工作代码

<div class="form-group row @error('warna') has-error @enderror">
    <label class="control-label text-right col-md-3">Buta Warna</label>
    <div class="col-md-3">
        <input type=radio id="warna1" name="warna" value="Negatif"
            @if(isset($mcu_form_tensi)){{ $mcu_form_tensi->warna == 'Negatif' ? 'checked' : ''}}
            @elseif(old('warna')=='Negatif' ) checked @endif>
        Negatif</option><br>
        <input type=radio id="warna2" name="warna" value="Partial"
            @if(isset($mcu_form_tensi)){{ $mcu_form_tensi->warna == 'Partial' ? 'checked' : ''}}
            @elseif(old('warna')=='Partial' ) checked @endif>
        Partial</option><br>
        <input type=radio id="warna3" name="warna" value="Total"
            @if(isset($mcu_form_tensi)){{ $mcu_form_tensi->warna == 'Total' ? 'checked' : ''}}
            @elseif(old('warna')=='Total' ) checked @endif>
        Total</option><br>
        <input type=radio id="warna4" name="warna" value="Other" @if(isset($mcu_form_tensi))
            @if($mcu_form_tensi->warna != 'Negatif' && $mcu_form_tensi->warna != 'Partial' &&
        $mcu_form_tensi->warna != 'Total') checked @endif @elseif(old('warna') != 'Negatif' &&
        old('warna') != 'Partial' && old('warna') != 'Total')
        checked @endif> Other</option><br>
        <input id="txt_warna_other" type="text" name="warna" placeholder=""
            class="form-control @error('warna') has-error @enderror" autocomplete="warna"
            value="@if(isset($mcu_form_tensi)) {{ $mcu_form_tensi->warna }} @else {{old('warna')}}@endif"
            @if(isset($mcu_form_tensi)) @if($mcu_form_tensi->warna == 'Negatif' ||
        $mcu_form_tensi->warna == 'Partial' || $mcu_form_tensi->warna == 'Total') readonly @endif
        @endif>
        @error('warna')
        <div class="text-danger">
            <small>{{ $message }}</small>
        </div>
        @enderror
    </div>
</div>

希望这对其他人也有帮助