0

使用 PyroCMS 1.3.1,我构建了一个模型,它几乎是包含的联系人模型的复制粘贴版本,但有一些调整。正确输入所有字段后,一切都按预期工作。如果某个字段被遗漏或填写不正确,则表单不会提交 - 正如预期的那样。

但是,我似乎无法输出表单验证消息,这让我抓狂。我确定我只是错过了一些非常基本的东西,所以如果有人能指出我将不胜感激。

查看文件(form.php)包含这个

<?php if (validation_errors()): ?>
<div class="error-box">
    <?php echo validation_errors(); ?>
</div>
<?php elseif (isset($messages['error'])): ?>
<div class="error-box">
    <p><?php echo $messages['error']; ?></p>
</div>
<?php endif; ?>

控制器(plugin.php)看起来像这样

class Plugin_mycustommodule extends Plugin {

private $rules = array(
    array(
        'field' => 'firstname',
        'label' => 'lang:mycustommodule_firstname_label',
        'rules' => 'required|trim|max_length[80]'
    ),
    /* ... snip ... */
    array(
        'field' => 'license',
        'label' => 'lang:mycustommodule_license_label',
        'rules' => 'required'
    )
);

public function __construct()
{
    $this->lang->load('mycustommodule');
}

function form()
{
    $this->load->library('form_validation');
    $this->load->helper('form');

    $this->form_validation->set_rules($this->rules);

    // If the user has provided valid information
    if ($this->form_validation->run())
    {
        /* ... Custom processing here ... */

        // The try to send the email
        if ($this->_send_email())
        {
            $message = $this->attribute('confirmation', lang('mycustommodule_sent_text'));

            // Store this session to limit useage
            $this->session->set_flashdata('success', $message);

            redirect(current_url());
        }
        else
        {
            $message = $this->attribute('error', lang('mycustommodule_error_message'));

            $data['messages']['error'] = $message;
        }
    }

    // Set the values for the form inputs
    foreach ($this->rules as $rule)
    {
        $form_values->{$rule['field']} = set_value($rule['field']);
    }

    $data['form_values']    = $form_values;

    return $this->module_view('mycustommodule', 'form', $data, TRUE);
}
4

1 回答 1

0

So it turns out that while I was working on customizing the CodeIgniters language files I must have messed up the upload of form_validation_lang.php because all entries was empty i.e. $lang['required'] = '';

So basically the validator looked for the error message, found an empty string, which was trimmed from being outputted. As suspected something silly, just not in the place I expected.

Let's hope this post will save someone else the trouble.

于 2011-09-02T07:17:55.017 回答