2

我正在使用 CodeIgniter 开发一个网站,并试图遵守“胖模型/瘦控制器”范式,但在涉及包含具有大量输入的表单的页面时遇到了一些问题。下面的代码是我用来定义地址字段输入的代码

我的控制器的一部分(我在其中定义表单输入及其属性):

$this->data['address1'] = array(
            'name' => 'address1',
            'id' => 'address1',
            'type' => 'text',
            'class' => 'field text addr',
            'tabindex' => '10',
            'value' => $this->form_validation->set_value('address1'),
            'placeholder' => 'Street Address'
        );

$this->data['address2'] = array(
            'name' => 'address2',
            'id' => 'address2',
            'type' => 'text',
            'class' => 'field text addr',
            'tabindex' => '11',
            'value' => $this->form_validation->set_value('address2'),
            'placeholder' => 'Address Line 2',
        );

$this->data['city'] = array(
            'name' => 'city',
            'id' => 'city',
            'type' => 'text',
            'class' => 'field text addr',
            'tabindex' => '12',
            'value' => $this->form_validation->set_value('city'),
            'placeholder' => 'City'
            );

$this->data['state'] = array(
            'name' => 'state',
            'id' => 'state',
            'class' => 'field addr',
            'tabindex' => '13',
            'value' => $this->form_validation->set_value('state'),
            'label' => array('class' => 'desc')
            );

$this->data['zip'] = array(
            'name' => 'zip',
            'id' => 'zip',
            'type' => 'text',
            'class' => 'field text addr',
            'tabindex' => '14',
            'maxlength' => '20',
            'value' => $this->form_validation->set_value('zip'),
            'placeholder' => 'Zip / Postal Code'
            );

$this->data['country'] = array(
            'name' => 'country',
            'id' => 'country',
            'class' => 'field addr',
            'tabindex' => '15',
            'value' => $this->form_validation->set_value('country')
            );

我的视图的一部分(减去所有用于定位表单输入的 HTML):

<?php
    echo form_open("address/add");
    echo form_input($address1);
    echo form_input($address2);
    echo form_input($city);

    $options = array();
    $options[''] = 'State / Province / Region';
    foreach($province_options AS $prov)
    {
            $options[$prov->id] = $prov->province;
    }
    echo form_dropdown('state',$options,'',$state);

    echo form_input($zip);

    $options = array();
    $options[''] = 'Country';
    foreach($country_options AS $cnt)
    {
            $options[$cnt->id] = $cnt->country;
    }
    echo form_dropdown('country',$options,'',$country);
    echo form_submit('submit', 'Submit & Continue');
    echo form_close();
?>

我觉得我的控制器过于冗长,但如果我打算使用表单助手在我的视图中生成表单输入,我想不出有什么替代方法来组织表示我的表单所需的信息. 这是做事的正确方法,还是有更好的方法?

4

2 回答 2

2

仅仅因为 Codeigniter 提供了所有这些帮助器并不意味着您必须使用它们!

您所需要的只是form_open()因为这会添加 CRSF 令牌(如果使用)。

原始 HTML 更干净,我怀疑比等待 PHP 呈现标记要快得多。

编辑: 我想补充一下,它更干净的原因是你可以控制输出,因为 CI 可能会遵守某些规范。

我认为你的问题没有问题。

这很愚蠢

$options = array();
$options[''] = 'State / Province / Region';
于 2012-03-06T00:41:35.467 回答
1

有一点逻辑可以移到控制器甚至模型层:

$options = array();
$options[''] = 'Country';
foreach($country_options AS $cnt)
{
        $options[$cnt->id] = $cnt->country;
}
echo form_dropdown('country',$options,'',$country);

可能看起来像:

echo form_dropdown('country', $countries, '', $country);

...如果您将选项移动到控制器或视图。

这是我一直遇到的一个问题,试图尽可能保持干燥,在哪里定义表单数据?我想有时我们会忘记“MVC”中“V”的力量。您可以改为在视图中定义所有视图逻辑。

和之类id的东西只在视图中是必要和有用的。表单验证规则和数据检查/准备等内容属于 Controller/Model 层。tabindexplaceholder

表单辅助函数很有用,但有时原始 HTML 更好。例如:

// Controller
$this->data['address1'] = array(
            'name' => 'address1',
            'id' => 'address1',
            'type' => 'text',
            'class' => 'field text addr',
            'tabindex' => '10',
            'value' => $this->form_validation->set_value('address1'),
            'placeholder' => 'Street Address'
        );
// View
echo form_input($address1);

或者简单地说:

<input name="address1" id="address1" tabindex="10" type="text" placeholder="Street Address" value="<?php echo set_value('address1'); ?>" class="field text addr">

去年我写了一堆应用程序,我在模型中定义了所有这些东西,现在我很后悔,因为我一直在回去对它们进行维护,所有视图逻辑在模型或控制器中都被遮蔽了. 编辑控制器或模型以更改class属性是愚蠢的。

于 2012-03-06T00:44:48.633 回答