我正在用模态叶片形式更新一列,我对验证有点困惑:
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">×</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();
答案 0 :(得分:0)
CSS类invalid-feedback的默认值是display:none在bootstrap css中没有,添加d-block类(假设bootstap 4)或另一个类以覆盖此CSS值,并将display设置为block:
@if ($errors->has('motivo_baja'))
<span class="invalid-feedback d-block" role="alert">
<strong>{{ $errors->first('motivo_baja') }}</strong>
</span>
@endif
答案 1 :(得分:0)
您的控制器use Validator;
中需要Validator类
尝试类似的事情
$rules = array (
'motivo_baja' => 'required' );
$error = Validator::make($request->all(), $rules;
if ($error->fails())
{
return response()->json(['errors' => $error->errors()->all()]);
}
答案 2 :(得分:0)
只需将required
和autofocus
添加到<textarea>