我已经扩展了10月cms后端,因此它有更多元字段,我想验证这些字段,这就是我创建额外字段的方式:
(plugin.php)
public function boot()
{
Event::listen('backend.form.extendFields', function($widget)
{
if (!$widget->model instanceof \Cms\Classes\Page) return;
//cms page fields
$widget->addFields([
'settings[str_seo_title]' =>[
'label' => 'Meta Title',
'tab' => 'SEO',
'type' => 'text'
],
'settings[str_seo_description]' =>[
'label' => 'Meta Description',
'tab' => 'SEO',
'size' => 'small',
'type' => 'textarea'
],
'settings[str_seo_keywords]' =>[
'label' => 'Meta Keywords',
'tab' => 'SEO',
'type' => 'text'
],
'settings[str_canonical_url]' => [
'label' => 'Canonical URL',
'type' => 'text',
'tab' => 'SEO',
],
'settings[str_robot_index]' => [
'label' => 'Robot Index',
'type' => 'dropdown',
'tab' => 'SEO',
'options' => ["index"=>"index","noindex"=>"noindex"],
'default' => 'index',
'span' => 'left'
],
'settings[str_robot_follow]' => [
'label' => 'Robot Follow',
'type' => 'dropdown',
'tab' => 'SEO',
'options' => ["follow"=>"follow","nofollow"=>"nofollow"],
'default' => 'follow',
'span' => 'right'
],
'settings[str_local_og_sitename]' => [
'label' => 'Site name',
'tab' => 'Open Graph',
'type' => 'text',
'placeholder' => 'Example: strong answer',
],
'settings[str_local_og_title]' => [
'label' => 'Title',
'type' => 'text',
'placeholder' => 'Example: New features',
'tab' => 'Open Graph',
],
'settings[str_local_og_description]' => [
'label' => 'Description',
'type' => 'textarea',
'size' => 'small',
'placeholder' => 'Example: seo plugin is a plugin that handles meta tags',
'tab' => 'Open Graph',
],
'settings[str_local_og_url]' => [
'label' => 'Page url',
'placeholder' => 'Example: http://www.strong-answer.com/',
'type' => 'text',
'tab' => 'Open Graph',
],
'settings[str_local_og_type]' => [
'label' => 'Type',
'type' => 'text',
'placeholder' => 'Example: website',
'tab' => 'Open Graph',
],
'settings[str_local_og_author]' => [
'label' => 'Author',
'type' => 'text',
'placeholder' => 'Example: Strong Answer',
'tab' => 'Open Graph',
],
'settings[str_local_og_img]' => [
'label' => 'Image path',
'type' => 'text',
'placeholder' => 'http://october.com/storage/app/media/Koala.jpg',
'tab' => 'Open Graph',
],
'settings[str_local_og_gl_title]' => [
'label' => 'Name for the post',
'type' => 'text',
'placeholder' => 'Example: My post title',
'tab' => 'Google Tags',
],
'settings[str_local_og_gl_description]' => [
'label' => 'The description of your post',
'type' => 'textarea',
'size' => 'small',
'placeholder' => 'Example: seo plugin is a plugin that handles meta tags',
'tab' => 'Google Tags',
],
'settings[str_local_og_gl_img]' => [
'label' => 'The image of your post',
'type' => 'text',
'placeholder' => 'Example: http://october.com/storage/app/media/Koala.jpg',
'tab' => 'Google Tags',
],
'settings[str_local_og_gl_page]' => [
'label' => 'Business page link',
'type' => 'text',
'placeholder' => 'Example: www.epicsite.com',
'tab' => 'Google Tags',
],
'settings[str_local_og_tt_card]' => [
'label' => 'Summary of your card',
'type' => 'textarea',
'size' => 'small',
'placeholder' => 'Example: Happy 3rd anniversary #TBT!',
'tab' => 'Twitter Tags',
],
'settings[str_local_og_tt_site]' => [
'label' => 'Twitter user',
'type' => 'text',
'placeholder' => 'Example: @stronganswer',
'tab' => 'Twitter Tags',
],
'settings[str_local_og_tt_title]' => [
'label' => 'Twitter title',
'type' => 'text',
'placeholder' => 'Example: Small Island Developing States Photo Submission',
'tab' => 'Twitter Tags',
],
'settings[str_local_og_tt_description]' => [
'label' => 'Twitter description',
'type' => 'textarea',
'size' => 'small',
'placeholder' => 'Example: seo plugin is a plugin that handles meta tags',
'tab' => 'Twitter Tags',
],
'settings[str_local_og_tt_img]' => [
'label' => 'Twitter image',
'type' => 'text',
'placeholder' => 'Example: http://october.com/storage/app/media/Koala.jpg',
'tab' => 'Twitter Tags',
],
'settings[str_local_og_fb_appid]' => [
'label' => 'Facebook App Id',
'type' => 'text',
'placeholder' => 'Example: 302184056577324',
'tab' => 'Facebook Tags',
],
'settings[str_local_og_fb_admins]' => [
'label' => 'Facebook Admins',
'type' => 'text',
'placeholder' => 'Example: 1234,2314',
'tab' => 'Facebook Tags',
]
], 'primary');
});
}
}
我已阅读文档,但我没有说明。
答案 0 :(得分:0)
我发现的方法是在插件的boot()方法中绑定到Page validation事件,如下所示:
\CMS\Classes\Page::extend(function($model) {
$model->bindEvent('model.beforeValidate', function() use ($model) {
if ($model->url == "/") {
$model->rules['main_title'] = 'required';
}
});
});