如何在OctoberCMS后台验证表单

3
我扩展了October 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');

               });
}
}

我已经阅读了文档,但我没有理解它。

你是在修改默认的October CMS表单吗?还是通过插件创建的表单? - prola
如果您想添加验证? - Ashish Detroja
1个回答

0
我发现的方法是在插件的boot()方法中绑定到页面验证事件,像这样:
        \CMS\Classes\Page::extend(function($model) {
        $model->bindEvent('model.beforeValidate', function() use ($model) {
            if ($model->url == "/") {
                $model->rules['main_title'] = 'required';
            }
        });
    });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接