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

时间:2017-04-16 23:23:13

标签: php codeigniter validation

我在StackOverflow上发布了几个相关问题,但似乎没有人找到解决方案。

我一直在关注CodeIgniter用户指南以及使用自定义form_validation.php配置数组创建和验证联系表单的几个教程。验证工作正常,但它会在我的自定义消息上显示默认验证消息。

我错过了什么?

Config - 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.',
                )
        )
    )
);

控制器 - 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;
        }
    }
}
?>

查看 - 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'); ?>

1 个答案:

答案 0 :(得分:0)

我只是想通了。在&#39;名称&#39; form_validation.php中的errors数组

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

需要更改为

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