首页 > 解决方案 > 模态刀片验证未显示

问题描述

我正在通过模态刀片表单更新列,我对验证有点困惑:

在此处输入图像描述

modal.blade.php

<div class="modal fade" id="baja_{{$id}}" tabindex="-1" role="dialog" aria-labelledby="bajaModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel" style="color: black;">Dar de Baja a {{$id}} </h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
        <div class="modal-body">
            <form method="POST" action="{{ route('contratos.baja', $id) }}">
                @csrf @method('PATCH')
                <div class="form-group">
                    <label for="motivo_baja" class="col-form-label" style="color: black; "><strong>Motivo:</strong></label>
                    <textarea class="form-control" id="motivo_baja" name="motivo_baja" style="width:100%; height: 150px;"></textarea>
                    @if ($errors->has('motivo_baja'))
                    <span class="invalid-feedback" role="alert">
                         <strong>{{ $errors->first('motivo_baja') }}</strong>
                    </span>
                    @endif
                </div>
                <div class="form-group row">
                    <button id="cerrar" type="button" class="btn btn-primary mx-auto" data-dismiss="modal">Cerrar</button>
                    <button id="dar_baja" type="submit" class="btn btn-primary mx-auto">Dar de Baja</button>
                </div>
            </form>
        </div>
    </div>
</div>

我希望验证何时textarea为空,我检查了它,但没有在这部分显示验证消息:

@if ($errors->has('motivo_baja'))
    <span class="invalid-feedback" role="alert">
         <strong>{{ $errors->first('motivo_baja') }}</strong>
    </span>
@endif

问题:

该变量errors似乎没有收到错误。但是就像我说的,当我不在 textarea 中写任何东西并发送数据时,表没有更新,所以,发生了一些事情,因为验证有效,但我不知道如何处理它。如何处理这些错误以在模式中显示它们,而不是简单地关闭它而不给出任何消息?

我的控制器ContratoController.php

public function baja(Request $request, $contrato_id){
    $request->validate([
        'motivo_baja' => 'required|max:300',
    ]);

    $contrato = Contrato::find($contrato_id);
    $contrato->motivo_baja = $request->get('motivo_baja');

    $contrato->save();

    return redirect()->route('contratos.index', $contrato->legajo_id)->with('success', 'Ok!');

}

我需要nullable迁移中制作这样的专栏:

$table->string('motivo_baja')->nullable();

标签: laravel

解决方案


只需添加requiredautofocus<textarea>


推荐阅读