laravel - 模态刀片验证未显示
问题描述
我正在通过模态刀片表单更新列,我对验证有点困惑:
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();
解决方案
只需添加required
和autofocus
<textarea>
推荐阅读
- javascript - pub/sub 模式的模块与对象构造函数的权衡?
- node.js - MongoDB 查询以查找具有变体的文档
- python - 如何使用 IMAP 获取正文消息
- json - 解析 avsc 文件以获取所需格式的键值对
- reactjs - 通过在 Reactjs 上运行数组来创建组件
- java - E/AndroidRuntime:致命异常:主 PID:19302 java.lang.RuntimeException:无法启动活动 ComponentInfo
- python - pyahk python setup.py egg_info 错误代码为 1 错误
- python - Scraper 仅将最后一个 URL 的数据输出到 CSV
- intellij-idea - 如何在 Intellij 中调试 Clojure Web 应用程序?
- git - 如何将每个文件从 repo 发送到实例?