2017-04-16 14 views
0

StackOverflowに関連する質問がいくつか掲載されていますが、誰も解決策を見つけていないようです。CodeIgniter - form_validation.phpを使用してカスタム検証エラーを表示する配列

私は、カスタムform_validation.php config配列を使用して連絡先フォームを作成し、検証するためのCodeIgniterユーザーガイドといくつかのチュートリアルに従ってきました。検証は正しく機能しますが、カスタムメッセージにデフォルトの検証メッセージが表示されます。

私には何が欠けていますか?

コンフィグ - 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 = '[email protected]'; 
      $to_email = '[email protected]'; 

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

ビュー - -

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

コントローラform_validation.php接触/ 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'); ?> 

答えて

0

私はそれを考え出しました。でform_validation.php

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

で '名前' のエラー配列が

'alpha_numeric_spaces' => 'Your name may only contain letters and spaces.', 
に変更する必要があります
関連する問題