unclecheese/display-logic

允许根据客户端行为分配特定表单字段的显示和隐藏条件。

安装量: 855,969

依赖项: 103

建议者: 7

安全: 0

星标: 73

关注者: 16

分支: 70

开放性问题: 19

类型:silverstripe-vendormodule


README

描述

Display Logic 模块允许您根据客户端行为添加显示或隐藏某些表单字段的条件。

示例用法

<?php
$products->displayIf("HasProducts")->isChecked();

$sizes->hideUnless("ProductType")->isEqualTo("t-shirt")
      ->andIf("Price")->isGreaterThan(10);
      
$payment->hideIf("Price")->isEqualTo(0);

$shipping->displayIf("ProductType")->isEqualTo("furniture")
           ->andIf()
              ->group()
                ->orIf("RushShipping")->isChecked()
                ->orIf("ShippingAddress")->isNotEmpty()
              ->end();

可用的比较函数

- isEqualTo
- isNotEqualTo
- isGreaterThan
- isLessThan
- contains
- startsWith
- endsWith
- isEmpty
- isNotEmpty
- isBetween
- isChecked
- isNotChecked()
- hasCheckedOption
- hasCheckedAtLeast
- hasCheckedLessThan

可用的显示条件

- displayIf()
- displayUnless()
- hideIf()
- hideUnless()

厨房水槽示例

<?php
  public function getCMSFields() {
		$fields = parent::getCMSFields();
		$fields->addFieldsToTab("Root.Test", array(
			TextField::create("Name","Name of event"),
			TextField::create("VenueSize","Size of venue"),
			$refreshments = CheckboxField::create("Refreshments","This is a large venue. Are there refreshments?"),							
			$vendors = CheckboxSetField::create("Vendors","Vendors", Member::get()->map()),				
			$tent = TextField::create("TentSize","You're going to need a tent. What size is it?"),

			OptionSetField::create("LinkType", "", array('internal' => 'Link to an internal page', 'external' => 'Link to an external page')),
			$internal = DropdownField::create("InternalLinkID", "Choose a page", SiteTree::get()->map()->toArray())->setEmptyString("-- choose --"),
			$external = TextField::create("ExternalLink", "Link to external page"),
			$label = TextField::create("LinkLabel", "Label for link"),

			$useEmbed = CheckboxField::create("UseEmbedCode","I have embed code"),
			$embed = TextareaField::create("EmbedCode","Enter the embed code.")
			
		));						
		
		$refreshments->displayIf("VenueSize")->isGreaterThan(100);
		$vendors->displayIf("Refreshments")->isChecked();
		$tent->displayIf("Vendors")->hasCheckedAtLeast(3);


		$internal->displayIf("LinkType")->isEqualTo("internal");				
		$external->displayIf("LinkType")->isEqualTo("external");
		$label->displayIf("LinkType")->isEqualTo("internal")->orIf("LinkType")->isEqualTo("external");
		
		$useEmbed->displayIf("LinkType")->isEqualTo("external");
		$embed->displayIf("UseEmbedCode")->isChecked()
					->orIf()
						->group()
							->orIf("ExternalLink")->contains("youtube.com")
							->orIf("ExternalLink")->contains("vimeo.com")
						->end();


		return $fields;
	}

厨房水槽示例,具有链式调用

<?php

use UncleCheese\DisplayLogic\Forms\Wrapper;
//...
  public function getCMSFields() {
        $f = parent::getCMSFields();
        $f->addFieldsToTab("Root.Test", array(
            TextField::create("Name","Name of event"),
            TextField::create("VenueSize","Size of venue"),
            CheckboxField::create("Refreshments","This is a large venue. Are there refreshments?")
                ->displayIf("VenueSize")->isGreaterThan(100)->end(),
            CheckboxSetField::create("Vendors","Vendors", Member::get()->map())
                ->displayIf("Refreshments")->isChecked()->end(),
            TextField::create("TentSize","You're going to need a tent. What size is it?")
                ->displayIf("Vendors")->hasCheckedAtLeast(3)->end(),
            CheckboxField::create('HasUpload', 'Has an upload'),
            
            Wrapper::create(
            	UploadField::create('FileUpload', 'Upload a file'),
            	LiteralField::create('test', '<strong>Keep the file small!</strong>')
            )->displayIf('HasUpload')->isChecked()->end(),

            OptionSetField::create("LinkType", "", array('internal' => 'Link to an internal page', 'external' => 'Link to an external page')),
            DropdownField::create("InternalLinkID", "Choose a page", SiteTree::get()->map()->toArray())->setEmptyString("-- choose --")
                ->displayIf("LinkType")->isEqualTo("internal")
                ->end(),
            TextField::create("ExternalLink", "Link to external page")
                ->displayIf("LinkType")->isEqualTo("external")
                ->end(),
            Wrapper::create(
            	ReadonlyField::create('URL','Base URL','http://example.com')
            )->displayIf('LinkType')->isEqualTo('internal')->end(),
            TextField::create("LinkLabel", "Label for link")
                ->displayIf("LinkType")->isChecked()->end(),
            CheckboxField::create("UseEmbedCode","I have embed code")
                ->displayIf("LinkType")->isEqualTo("external")              
                ->end(),
            TextareaField::create("EmbedCode","Enter the embed code.")
                ->displayIf("UseEmbedCode")->isChecked()
                    ->orIf()
                        ->group()
                            ->orIf("ExternalLink")->contains("youtube.com")
                            ->orIf("ExternalLink")->contains("vimeo.com")
                        ->end()
        ));                     

		return $f;
	}

处理非标准表单字段

有时您可能想要将显示逻辑围绕一个不使用标准 FormField 模板的表单字段,例如 GridField 或 LiteralField。在这些情况下,您可以在 UncleCheese\DisplayLogic\Forms\Wrapper 中包装表单字段。

$fields->addFieldToTab("Root.Main", Wrapper::create(
		LiteralField::create("foo","<h2>Hello</h2>")
	)
	->displayIf("Title")->isEmpty()->end()
);

displayIf() 和 hideUnless() 之间的区别是什么?

区别不大。hideUnless() 将在脚本加载之前通过 CSS 类隐藏它,以消除脚本加载前表单字段的任何闪烁。通常,当字段通常被隐藏时使用 displayIf(),而当字段通常被显示时使用 hideUnless()。相同的逻辑也适用于 hideIf() 和 displayUnless()。