表格制作网址 - 简单的产品选择器

时间:2014-10-05 19:20:33

标签: forms url submit selector silverstripe

我在SilverStripe CMS中创建了一个产品和服务文件夹和项目,一切都很好,但我想要一种产品选择器。

我认为我可以通过下拉菜单通过表单进行操作,当您选择值1时,您可以从值2中选择,但最重要的是当您点击提交时我希望形成一个链接/网址,如:http://www.example.com/value1/value2并自动去那里。

简单:让我们假设我有上部(称为SAMSUNG)然后在其中我有产品/子页面(例如:Galaxy S3,Galaxy S4,Galaxy S5等)和我希望人们选择使用这种简单的形式:首先选择制造商 - >设备类型/型号/名称 - >点击提交(转),它会直接带你到正确的地方:http://www.example.com/manufacturer/product

是否可以通过表单获取类似的URL?

1 个答案:

答案 0 :(得分:0)

这很容易。基本上你要做的就是让你的表单在某处提交,然后使用从那里提交的数据重定向到所需的URL。

在某些控件中

private static $allowed_actions = array(
    'Form'
);

public function Form() {
    $fields = new FieldList(
        new DropdownField('Manufacturer', 'Manufacturer', array(
            'Samsumg' => 'Samsumg',
            'HTC' => 'HTC',
            'Apple' => 'Apple'
        )),
        // It's necessary that first argument is Samsung as it will
        // be used to fetch the device later on
        new DropdownField('Samsumg', 'Samsumg', array(
            'GalaxyS3' => 'Galaxy S3',
            'GalaxyS4' => 'Galaxy S4',
            'GalaxyS5' => 'Galaxy S5'
        )),
        // It's necessary that first argument is HTC as it will
        // be used to fetch the device later on
        new DropdownField('HTC', 'HTC', array(
            'Desire' => 'Desire',
            'Wildfire' => 'Wildfire',
            'One' => 'One'
        )),
        // It's necessary that first argument is Apple as it will
        // be used to fetch the device later on
        new DropdownField('Apple', 'Apple', array(
            'iPhne5' => 'iPhone 5',
            'iPhone6' => 'iPhone 6',
            'iPhone6Plus' => 'iPhone 6 Plus'
        ))
    );

    $actions = new FieldList(
        new FormAction('submit', 'Search')
    );

    return new Form($this, 'Form', $fields, $actions);
}

public function submit($data, $form) {
    // Get selected manufacturer
    $manufacturer = $data['Manufacturer'];

    // Get selected device by selected manufacturer
    $device = $data[$manufacturer];

    // Redirect to the manufacturer/device URL
    return $this->redirect($manufacturer . '/' . $device);
}

在模板中使用$Form输出表单。