HTML:输入 YouTube 视频嵌入 link 无效
HTML: Input YouTube Video embed link not working
构建一个 laravel 应用程序,用户还可以在其中添加 YouTube 视频(嵌入 links)。
如何验证用户提供的 link 是否真的是嵌入的 youtube 视频 link?
告知用户有关正确的 youtube 的正确方法是什么 link(我的意思是例如:https://www.youtube.com/embed/Ou7rPcoqXWA
像这样。
当用户输入错误 link 时,现在显示
随消息 "Trying to get property '**' of non-object (View: C:\xampp\htdocs\project\resources\views\front\posts.blade.php)"
抛出 ErrorException
那么,我应该怎么做才能让用户知道 link 是不正确的
和
如果他们输入不正确的 link,它不应该在视图(前端)中 show-up。
表单中的输入字段有 class、名称、标题、占位符(YouTube 视频 URL(仅粘贴嵌入的 youtube 代码 | 例如:https://youtu.be/xBht9TG7ySw))和类型.
&这是在查看blade
iframe src="{{$post->video}}" frameborder="0" allowfullscreen="allowfullscreen" height="315" width="100%"></iframe>
使用 Youtube 的 API 怎么样? .
毕竟,这将意味着使用一些官方,这比解析一些 HTML 页面更不可能改变。
更多信息:YouTube APIs and Tools - Developer's Guide: PHP
Retrieving a specific video entry 看起来很有趣:如果你像这样向 URL 发送请求:
http://gdata.youtube.com/feeds/api/videos/videoID
(当然用视频ID替换"videoID")
如果视频有效,您将获得一些 ATOM 供稿; "Invalid id" 如果不是
而且,我坚持认为:这样,您依赖于记录在案的 API,而不是当今存在但不受保证的某种行为。
Regular Expressions 可用于此类验证。
对于 client-side 验证:
function validateYoutubeURL(url){
var regex=/https?:\/\/youtu(be\.com|\.be)\/.+/;
return regex.test(url.toLowerCase());
}
public function store(Request $request){
$this->validate($request, [
'field_name'=> [
'required',
'regex:/https?:\/\/youtu(be\.com|\.be)\/.+/',
]
]);
}
为了通知用户错误 URL,请使用 this(服务器端验证):
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
重要:
Note: When using the regex / not_regex patterns, it may be necessary to specify rules in an array instead of using pipe delimiters, especially if the regular expression contains a pipe character.
对于您问题的第三部分,请post您的视图和控制器。
构建一个 laravel 应用程序,用户还可以在其中添加 YouTube 视频(嵌入 links)。
如何验证用户提供的 link 是否真的是嵌入的 youtube 视频 link?
告知用户有关正确的 youtube 的正确方法是什么 link(我的意思是例如:https://www.youtube.com/embed/Ou7rPcoqXWA 像这样。
当用户输入错误 link 时,现在显示
随消息 "Trying to get property '**' of non-object (View: C:\xampp\htdocs\project\resources\views\front\posts.blade.php)"
抛出 ErrorException
那么,我应该怎么做才能让用户知道 link 是不正确的 和 如果他们输入不正确的 link,它不应该在视图(前端)中 show-up。
表单中的输入字段有 class、名称、标题、占位符(YouTube 视频 URL(仅粘贴嵌入的 youtube 代码 | 例如:https://youtu.be/xBht9TG7ySw))和类型.
&这是在查看blade
iframe src="{{$post->video}}" frameborder="0" allowfullscreen="allowfullscreen" height="315" width="100%"></iframe>
使用 Youtube 的 API 怎么样? .
毕竟,这将意味着使用一些官方,这比解析一些 HTML 页面更不可能改变。
更多信息:YouTube APIs and Tools - Developer's Guide: PHP
Retrieving a specific video entry 看起来很有趣:如果你像这样向 URL 发送请求:
http://gdata.youtube.com/feeds/api/videos/videoID
(当然用视频ID替换"videoID")
如果视频有效,您将获得一些 ATOM 供稿; "Invalid id" 如果不是
而且,我坚持认为:这样,您依赖于记录在案的 API,而不是当今存在但不受保证的某种行为。
Regular Expressions 可用于此类验证。 对于 client-side 验证:
function validateYoutubeURL(url){
var regex=/https?:\/\/youtu(be\.com|\.be)\/.+/;
return regex.test(url.toLowerCase());
}
public function store(Request $request){
$this->validate($request, [
'field_name'=> [
'required',
'regex:/https?:\/\/youtu(be\.com|\.be)\/.+/',
]
]);
}
为了通知用户错误 URL,请使用 this(服务器端验证):
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
重要:
Note: When using the regex / not_regex patterns, it may be necessary to specify rules in an array instead of using pipe delimiters, especially if the regular expression contains a pipe character.
对于您问题的第三部分,请post您的视图和控制器。