PHP带有自由文本的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>

如果新记录的验证失败,我将无法保留旧值。 它会继续还原为不选择任何内容,因此用户必须再次选择。 请提供帮助,以便用户无需再次输入,只需输入那些无效的输入即可。

解决方法

这是我的工作代码

<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>

希望这对其他人也有帮助