【发布时间】:2020-03-06 00:13:24
【问题描述】:
我有一个包含三个输入字段的表单。我想在处理它们之前验证输入值。我想在处理之前验证文件名。我使用正则表达式和 alpha_dash。但是我得到一个有效文件名的错误。我希望我的文件名只包含小写字母、数字、下划线和破折号。如何检查我的文件的文件名验证?
HTML
<form action="create" method="POST" enctype="multipart/form-data">
{{csrf_field()}}
<table cellpadding="2" width="20%" align="center"
cellspacing="2">
<tr>
<td colspan=2>
<center><font size=4><b>Add the iteams please</b></font></center>
</td>
</tr>
<tr>
<td>Heading</td>
<td><input type="text" name="heading" id="heading" size="30">
{!! $errors->first('heading', '<p class="red">:message</p>') !!}
</td>
</tr>
<tr>
<td>Image</td>
<td><input type="file" name="image" id="image" size="40">
{!! $errors->first('image', '<p class="red">:message</p>') !!}
</td>
</tr>
<tr>
<td></td>
<td colspan="2"><input type="submit" name="submit" value="Add Item" /></td>
</tr>
</table>
</form>
控制器部分
- 使用正则表达式格式:
- 我收到错误消息,“图像格式无效”。
public function store(){
$this->validate(request(),[
'heading'=>'required',
'contentbody'=>'required',
‘image'=>['required','image','mimes:jpeg,png,jpg,gif,svg','max:2048','regex:/^[a-z0-9-_]+$/' ]
]);
}
- 使用 Alpa_dash:
- 我收到错误消息,“图像只能包含字母、数字和破折号”。
public function store(){
$this->validate(request(),[
'heading'=>'required',
'contentbody'=>'required',
'image'=>'required|image|mimes:jpg,png,jpeg,gif,svg|max:2048|alpha_dash'
}
请帮忙, 谢谢!
【问题讨论】:
标签: php regex laravel laravel-5 laravel-5.4