【问题标题】:How to validate a form at octobercms backend如何在 octobercms 后端验证表单
【发布时间】:2016-09-25 11:47:09
【问题描述】:

我扩展了 october cms 后端,因此它有更多元字段,我想验证这些字段,这就是我创建额外字段的方式:

(插件.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 表单?还是您通过插件创建的?
  • 如果要添加验证?

标签: php validation laravel octobercms


【解决方案1】:

我发现的方法是在插件的 boot() 方法中绑定页面验证事件,如下所示:

        \CMS\Classes\Page::extend(function($model) {
        $model->bindEvent('model.beforeValidate', function() use ($model) {
            if ($model->url == "/") {
                $model->rules['main_title'] = 'required';
            }
        });
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    • 2021-09-22
    • 2016-07-29
    • 2019-08-09
    相关资源
    最近更新 更多