文本区域验证最大规则在 laravel 6 中不起作用
Posted
技术标签:
【中文标题】文本区域验证最大规则在 laravel 6 中不起作用【英文标题】:Text area validation max rule not working in laravel 6 【发布时间】:2020-04-18 20:56:17 【问题描述】:我尝试在textarea
中使用设置为 2000 的 max
规则,但我输入了超过 2000 个字符,验证不适用于其他输入字段,并且 使用空输入值重新加载页面强>即使我尝试少于 2000 个字符的数量,问题仍然存在,当我用空 textarea
添加所需的验证工作时,我也尝试将最大值更改为大小,但仍然存在同样的问题。
我想在下一个请求中使用旧输入值验证 textarea
,如果字符数超过 2000 则显示错误消息。
我可以在 html 中使用 javascript
或 maxlength
属性,但想从服务器端执行。
textarea
有 974 个字符时所有输入验证失败
刀片
<form method="POST" action=" route('problems.store', auth()->user()) ">
@csrf
<h6 class="heading-small text-muted mb-4"> __('Problem information') </h6>
<div class="pl-lg-4">
<!-- alert -->
@if ($errors->any())
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<ul>
@foreach ($errors->all() as $error)
<li> $error </li>
@endforeach
</ul>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
@endif
<!-- Account Number input -->
<div class="form-group $errors->has('Account_Number') ? ' has-danger' : '' ">
<label class="form-control-label"
for="input-account-number"> __('Account Number') </label>
<div class="input-group input-group-alternative mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="far fa-user-circle"></i></span>
</div>
<input class="form-control $errors->has('Account_Number') ? ' is-invalid' : '' "
placeholder=" __('Account Number') " type="text" name="Account_Number"
value=" old('Account_Number') " autofocus>
</div>
@if ($errors->has('Account_Number'))
<span class="invalid-feedback" style="display: block;" role="alert">
<strong> $errors->first('Account_Number') </strong>
</span>
@endif
</div>
<!-- Account Name input -->
<div class="form-group $errors->has('Account_Name') ? ' has-danger' : '' ">
<label class="form-control-label"
for="input-account-name"> __('Account Name') </label>
<div class="input-group input-group-alternative mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="far fa-id-card"></i></span>
</div>
<input class="form-control $errors->has('Account_Name') ? ' is-invalid' : '' "
placeholder=" __('Account Name') " type="text" name="Account_Name"
value=" old('Account_Name') " autofocus>
</div>
@if ($errors->has('Account_Name'))
<span class="invalid-feedback" style="display: block;" role="alert">
<strong> $errors->first('Account_Name') </strong>
</span>
@endif
</div>
<!-- Email input -->
<div class="form-group $errors->has('email') ? ' has-danger' : '' ">
<label class="form-control-label" for="input-email"> __('Email') </label>
<div class="input-group input-group-alternative mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="far fa-envelope"></i></span>
</div>
<input class="form-control $errors->has('email') ? ' is-invalid' : '' "
placeholder=" __('Email') " type="email" name="email"
value=" old('email') ">
</div>
@if ($errors->has('email'))
<span class="invalid-feedback" style="display: block;" role="alert">
<strong> $errors->first('email') </strong>
</span>
@endif
</div>
<!-- Date input -->
<div class="form-group $errors->has('Date') ? ' has-danger' : '' ">
<label class="form-control-label" for="input-data"> __('Date') </label>
<div class="input-group input-group-alternative mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="far fa-calendar-alt"></i></span>
</div>
<input class="form-control $errors->has('Date') ? ' is-invalid' : '' "
placeholder=" __('Date') " type="date" name="Date" value=" old('Date') "
min="2005-01-01" max="2025-12-31" autofocus>
</div>
@if ($errors->has('Date'))
<span class="invalid-feedback" style="display: block;" role="alert">
<strong> $errors->first('Date') </strong>
</span>
@endif
</div>
<!-- problem description input -->
<div class="form-group $errors->has('problem_descrip') ? ' has-danger' : '' ">
<label class="form-control-label"
for="input-problem-description"> __('Problem Description') </label>
<div class="input-group input-group-alternative mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="far fa-file-alt"></i></span>
</div>
<!-- the problem here -->
<textarea class="form-control $errors->has('problem_descrip') ? ' is-invalid' : '' "
name="problem_descrip" rows="2"
placeholder=" __('Descrip the problem here') "> old('problem_descrip') </textarea>
</div>
@if ($errors->has('problem_descrip'))
<span class="invalid-feedback" style="display: block;" role="alert">
<strong> $errors->first('problem_descrip') </strong>
</span>
@endif
</div>
<!-- added by input -->
<div class="form-group">
<label class="form-control-label" for="input-added_by"> __('Added By') </label>
<div class="input-group input-group-alternative mb-3">
<div class="input-group-prepend">
<span class="input-group-text" style="background-color: #e9ecef"><i lass="fas fa-user-tie"></i></span>
</div>
<input class="form-control" type="text" value=" auth()->user()->name " disabled>
</div>
</div>
<hr class="my-4"/>
<h6 class="heading-small text-muted mb-4"> __('Solved') </h6>
<!-- Comment input -->
<div class="form-group $errors->has('comment') ? ' has-danger' : '' ">
<label class="form-control-label" for="input-comment"> __('Comment') </label>
<div class="input-group input-group-alternative">
<div class="input-group-prepend">
<span class="input-group-text"><i class="far fa-comment"></i></i></span>
</div>
<textarea class="form-control $errors->has('comment') ? ' is-invalid' : '' "
name="comment" rows="2"
placeholder=" __('comment on the problem or solution') "> old('comment') </textarea>
</div>
@if ($errors->has('comment'))
<span class="invalid-feedback" style="display: block;" role="alert">
<strong> $errors->first('comment') </strong>
</span>
@endif
</div>
<!-- added by input -->
<label class="form-control-label" for="input-added_by"> __('Solved By') </label>
<div class="input-group input-group-alternative mb-2">
<div class="input-group-prepend">
<span class="input-group-text" style="background-color: #e9ecef"><i class="fas fa-user-tie"></i></span>
</div>
<input class="form-control" type="text" value=" auth()->user()->name " disabled>
</div>
<!-- Solved checkbox -->
<div class="custom-control custom-checkbox mb-3">
<div class="input-group $errors->has('Sovled') ? ' has-danger' : '' mb-2">
<input type='hidden' value='0' name='Sovled'>
<input class="custom-control-input" id="customCheck1" name="Sovled" value="1"
type="checkbox">
<label class="custom-control-label" for="customCheck1"> __('Solved') </label>
</div>
@if ($errors->has('Sovled'))
<span class="text-danger ml--4" role="alert">
<b> $errors->first('Sovled') </b>
</span>
@endif
</div>
<!-- submit button -->
<div class="text-center">
<button type="submit" class="btn btn-primary mt-4"> __('Add') </button>
</div>
</div>
</form>
控制器中使用的方法
public function store(Request $request, User $user)
$rules = [
'Account_Number' => ['required', 'numeric'],
'Account_Name' => ['required', 'string'],
'email' => ['required', 'email', 'max:20'],
'Date' => ['required', 'date_format:Y-m-d'],
'problem_descrip' => ['required', 'min:1', 'max:2000'], // max not work
'comment' => ['nullable', 'string', 'required_if:Sovled,1', 'min:0', 'max:2000'], // something
'Sovled' => ['nullable', 'numeric', new Checkbox($request->get('comment'), $request->get('Sovled'))]
];
$customMessages = [
'comment.required_if' => 'The :attribute field can not be blank when solved box is checked you need to write the solution.'
];
$result = Validator::make($request->all(), $rules, $customMessages);
if ($result->fails())
return redirect()->back()->withErrors($result)->withInput($request->input());
dd($request->all());
//$request->user->id
问题
【问题讨论】:
阅读此页laravel-news.com/testing-length-validation-laravel 他们使用|分隔规则而不是 ', ' 类似:'problem_descrip' => ['required|min:1|max:2000'] @Triby 谢谢亲爱的这种方式 problem_descrip' => ['required|min:1|max:2000'] 不起作用它给我一个错误我已经尝试了这个问题描述' =>' required|min:1|max:2000' 但仍然有同样的问题我已经阅读了测试长度页面,但我在那里找不到答案或解决方案。我想知道为什么会这样。 试试这个'problem_descrip' => 'required|min:1| max:2000', // max not work
@Omi 我尝试清除浏览器缓存的东西仍然有同样的问题。
还有其他规则有效吗?
【参考方案1】:
看起来像是会话问题,尝试将会话驱动程序从 cookie 更改为 .env
中的文件
SESSION_DRIVER=file
【讨论】:
【参考方案2】:试试这个,你必须添加别名 STRING,因为最大检测到文件大小。祝你好运,如果对您的问题有帮助,请按 UP 按钮:)。
$rules = [
'Account_Number' => ['required', 'numeric'],
'Account_Name' => ['required', 'string'],
'email' => ['required', 'email', 'max:20'],
'Date' => ['required', 'date_format:Y-m-d'],
'problem_descrip' => ['required', 'string', 'min:1', 'max:2000'], //Add string here
'comment' => ['nullable', 'string', 'required_if:Sovled,1', 'min:0', 'max:2000'], // something
'Sovled' => ['nullable', 'numeric', new Checkbox($request->get('comment'), $request->get('Sovled'))]
];
【讨论】:
问题出在我使用的模板上。以上是关于文本区域验证最大规则在 laravel 6 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章