CodeIgniter:具有许多输入的表单的控制器结构

时间:2012-03-05 22:10:00

标签: forms model-view-controller codeigniter codeigniter-form-helper

我正在使用CodeIgniter开发一个网站,并且我正在尝试遵循“Fat Model / Skinny 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'
        );

$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();
?>

我觉得我的控制器过于冗长,但如果我计划使用表单助手生成表单输入,我无法想到如何组织表示表单所需的信息的替代方案在我看来。这是做事的正确方法,还是有更好的方法?

2 个答案:

答案 0 :(得分:2)

仅仅因为Codeigniter提供所有这些助手并不意味着你必须使用它们!

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

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

编辑 我想补充一点,原因是它更清洁,因为你可以控制输出,因为CI可能符合某些规格。

我的问题中没有问题。

这只是愚蠢的

$options = array();
$options[''] = 'State / Province / Region';

答案 1 :(得分: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);

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

这是一个问题,我一直试图尽可能地保留DRY,在哪里定义表单数据?我想有时我们会忘记“MVC”中“V”的力量。你可以定义视图中的所有视图逻辑。

idtabindexplaceholder等内容仅在视图中有用且有用。表单验证规则和数据检查/准备等内容属于Controller / Model层。

表单助手函数很有用,但有时原始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属性非常愚蠢。