简体   繁体   English

CodeIgniter-使用form_validation.php配置数组显示自定义验证错误

[英]CodeIgniter - Displaying Custom Validation Errors Using form_validation.php Config Array

I've seen several related questions posted on StackOverflow, but nobody seems to have found a solution. 我已经在StackOverflow上看到了几个相关的问题,但是似乎没有人找到解决方案。

I've been following the CodeIgniter user guide and several tutorials to create and validate a contact form using a custom form_validation.php config array. 我一直在遵循CodeIgniter用户指南和一些教程,以使用自定义form_validation.php配置数组创建和验证联系表单。 The validation works properly, except that it displays the default validation message over my custom message. 验证工作正常,除了它会在我的自定义消息上显示默认验证消息。

What am I missing? 我想念什么?

Config - form_validation.php 配置-form_validation.php

<?php  defined('BASEPATH') OR exit('No direct script access allowed');

$config = array(
    'contact/index' => array(
        array(
                'field' => 'name',
                'label' => 'Name',
                'rules' => 'trim|required|alpha_numeric_spaces',
                'errors' => array(
                    'required' => 'Please enter a %s.',
                    'alpha' => 'Your name may only contain letters and spaces.',
                )
        ),
        array(
                'field' => 'email',
                'label' => 'Email',
                'rules' => 'trim|required|valid_email',
                'errors' => array(
                    'required' => 'Please enter an %s address.',
                    'valid_email' => 'Please enter a valid %s address.',
                )
        ),
        array(
                'field' => 'subject',
                'label' => 'Subject',
                'rules' => 'trim|required',
                'errors' => array(
                    'required' => 'Please enter a %s.',
                )
        ),
        array(
                'field' => 'message',
                'label' => 'Message',
                'rules' => 'trim|required',
                'errors' => array(
                    'required' => 'Please enter a %s.',
                )
        ),
        array(
                'field' => 'g-recaptcha-response',
                'label' => 'recaptcha',
                'rules' => 'callback_recaptcha',
                'errors' => array(
                    'recaptcha' => 'Click the checkbox to prove you\'re human.',
                )
        )
    )
);

Controller - Contact.php 控制器-Contact.php

<?php
class Contact extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->load->helper(array('form'));
        $this->load->library(array('session', 'form_validation', 'recaptcha', 'email'));

        $this->config->load('form_validation', TRUE);

        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        $this->form_validation->set_rules($this->config->item('form_validation', 'validation_rules'));

        $data['title'] = 'Contact Me';
        $data['description'] = 'Use this contact form.';
        $data['siteKey'] = $this->config->item('recaptcha-site-key');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('templates/header', $data);
            $this->load->view('contact/index');
            $this->load->view('templates/footer', $data);
        }
        else
        {
            $name = $this->input->post('name');
            $reply_to_email = $this->input->post('email');
            $subject = $this->input->post('subject');
            $message = $this->input->post('message');

            $from_email = 'me@mydomain.com';
            $to_email = 'me@gmail.com';

            $this->email->from($from_email, $name);
            $this->email->to($to_email);
            $this->email->reply_to($reply_to_email);
            $this->email->subject($subject);
            $this->email->message($message);

            if ($this->email->send())
            {
                $this->session->set_flashdata('msg','<div class="result">Your message has been successfully delivered.</div>');
                redirect('contact/index');
            }
            else
            {
                $this->session->set_flashdata('msg','<div class="result">Your message could not be sent. Please try again later.</div>');
                redirect('contact/index');
            }
        }
    }

    public function recaptcha ($response)
    {
        $this->load->library('Recaptcha');

        if (!$this->recaptcha->verify($response)) {
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
}
?>

View - contact/index.php 查看-contact / index.php

<?php echo form_open('contact', 'id="contact-form"'); ?>
    <fieldset>
        <ol>
            <li>
                <label for="name">Name:</label>
                <input id="name" type="text" name="name" value="<?php echo set_value('name'); ?>" placeholder="Full Name" />
                <?php echo form_error('name'); ?>
            </li>

            <li>
                <label for="email">Email Address:</label>
                <input id="email" type="email" name="email" value="<?php echo set_value('email'); ?>" placeholder="Email Address" />
                <?php echo form_error('email'); ?>
            </li>

            <li>
                <label for="subject">Subject:</label>
                <input id="subject" type="text" name="subject" value="<?php echo set_value('subject'); ?>" placeholder="Subject" />
                <?php echo form_error('subject'); ?>
            </li>

            <li>
                <label for="message">Message:</label>
                <textarea id="message" name="message" value="<?php echo set_value('message'); ?>" placeholder="Enter your Message"></textarea>
                <?php echo form_error('message'); ?>
            </li>

            <li>
                <label for="recaptcha">reCAPTCHA:</label>
                <div id="recaptcha" class="g-recaptcha" data-sitekey="<?php echo $siteKey; ?>" ></div>
                <?php echo form_error('g-recaptcha-response'); ?>
            </li>

            <li>
                <input class="button" type="submit" name="send" id="send" value="Send Message">
            </li>
        </ol>
    </fieldset>
<?php echo form_close(); ?>
<?php echo $this->session->flashdata('msg'); ?>

I just figured it out. 我只是想通了。 In the 'name' errors array in form_validation.php 在form_validation.php的“名称”错误数组中

'alpha' => 'Your name may only contain letters and spaces.',

needs to be changed to 需要更改为

'alpha_numeric_spaces' => 'Your name may only contain letters and spaces.',

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在form_validation.php中为每个表单分隔配置数组,以避免一次显示所有错误-CodeIgniter - How to separate config array for each form in form_validation.php to avoid displaying all errors at once - CodeIgniter 在自定义form_validation.php中使用帮助器函数-CodeIgniter 3 - Use helper function in custom form_validation.php - CodeIgniter 3 使用codeingiter在form_validation.php中创建自定义验证回调 - Create custom validation callback in form_validation.php using codeingiter CodeIgniter“ form_validation.php”不能按预期工作吗? - CodeIgniter “form_validation.php” not working as expected? 在CodeIgniter中检查配置文件或form_validation.php中的已发布数据 - Check for posted data inside the config file or form_validation.php in CodeIgniter Codeigniter如何获取config / form_validation.php数据? - codeigniter how can I get access to config/form_validation.php data? 无法使用语言类在 Codeigniter 3 中的 form_validation.php 中设置自定义消息 - Fail to use language class to set custom message in form_validation.php in Codeigniter 3 使用`form_validation.php`时如何使用CodeIgniter的`set_value()`函数? - How do I use CodeIgniter's `set_value()` function when using `form_validation.php`? Codeigniter表单验证不显示错误 - codeigniter form validation not displaying errors CodeIgniter:表单验证和显示错误 - CodeIgniter: Form Validation and displaying errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM