简体   繁体   中英

form_validation CodeIgniter

I wonder what did i do wrong so that whatever email address i put in the form, it returns "Unable to access an error message corresponding to your field name Email Address.(email)" and the form cannot be sent successfully.

<?php echo validation_errors('<p class="error">'); ?>

    <?php echo form_open('home/send'); ?>

        <table>
            <tr>
                <th>
                    <label for="name">Name</label>
                </th>
                <td>
                    <input type="text" name="name" id="name" value="<?php echo set_value('name'); ?>">
                </td>
            </tr>
            <tr>
                <th>
                    <label for="email">Email</label>
                </th>
                <td>
                    <input type="text" name="email" id="email" value="<?php echo set_value('email'); ?>">
                </td>
            </tr>
            <tr>
                <th>
                    <label for="message">Message</label>
                </th>
                <td>
                    <textarea name="message" id="message"><?php echo set_value('message'); ?></textarea>
                </td>
            </tr>
        </table>
        <?php echo form_submit('submit', 'Submit'); ?>

    <?php echo form_close(); ?>



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

    $this->form_validation->set_rules('name', 'Name', 'trim|required');
    $this->form_validation->set_rules('email', 'Email Address', 'trim|required|email');
    $this->form_validation->set_rules('message', 'Message', 'trim|required');


    if($this->form_validation->run() == false) {

        $data['page_title'] = "Contact Mike";
        $data['section'] = "contact";

        $this->load->view('templates/header', $data);
        $this->load->view('contact', $data);
        $this->load->view('templates/footer', $data);

    } else {}

use the form_error() function in view file

<?php echo form_error('username'); ?>
    <input type="text" name="username" value="<?php echo set_value('username'); ?>" size="50" />

Rule should be valid_email , according to documentation:

$this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email'); 

You have placed non-existing 'email' rule there...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM