PyroCMS 中未输出表单验证错误

发布于 2024-12-02 05:06:53 字数 2224 浏览 1 评论 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);
}

Using PyroCMS 1.3.1 I've built a model that's pretty much a copy-paste version of the included contact model, but with a few tweaks. When all fields are inputted correctly everything works as expected. If a field is left out or incorrectly filled in, the form does not submit - just as expected.

However, I can't seem to get the form validation message to be outputted and this is driving me crazy. I'm sure I've just missed something very basic so if anyone could point it out I'd be grateful.

View file (form.php) contains this

<?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; ?>

Controller (plugin.php) looks like this

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话墙 2024-12-09 05:06:53

所以事实证明,当我正在自定义 CodeIgniters 语言文件时,我一定搞砸了 form_validation_lang.php 的上传,因为所有条目都是空的,即 $lang['required'] = '';

所以基本上验证器会查找错误消息,发现一个空字符串,该字符串被修剪掉而无法输出。正如怀疑的一些愚蠢的事情,只是不在我预期的地方。

希望这篇文章能够帮其他人省去麻烦。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文