【问题标题】:CodeIgniter: Controller structure for forms with many inputsCodeIgniter:具有许多输入的表单的控制器结构
【发布时间】:2012-03-23 09:46:09
【问题描述】:

我正在使用 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();
?>

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

【问题讨论】:

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


    【解决方案1】:

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

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

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

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

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

    这太傻了

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

    【讨论】:

    • +1 用于原始 HTML(30 分钟前我刚刚发布了一个类似的答案,直到晚餐打断了我),但我看不出你发布的代码块有什么“愚蠢”的地方。你能解释一下吗?
    • @MadM 要回答你的问题,我个人没有看到对 var 进行类型转换,然后用值填充它的意义,这在类中可能没问题,但是这是方法中的 tmp 数组(顺便说一句谢谢 PHP6 或 5.4 什么的),这种方法可能是合理的,但为了便于阅读,我认为它是错误的。
    • 好吧,如果没有 second $options = array();,第一个值会溢出(可能应该使用不同的 var 名称)。我理解你的意思,但我仍然认为这是一种很好的做法,并且非常清楚发生了什么以避免“意外”。
    • 感谢您的回复。您和 Madmartigan 的建议对我的网站的可维护性和灵活性都更有意义。
    【解决方案2】:

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

    $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 属性只是愚蠢的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多