CodeIgniter表单验证始终返回false

时间:2013-03-31 19:47:35

标签: codeigniter validation

我一直在搜索网页并用我的代码进行几个小时的修补,似乎无法弄清楚为什么这不起作用。

我有一个模板库,用于扫描template.html中的{@tags},然后运行与该标记关联的功能,为该{@tag}的内容创建数据,然后替换{@tag}的内容生成,所以我可以在我的模板中添加类似于部件的小部件。

在我的帐户/访问页面上,我有一个登录表单和一个注册表单,现在当模板库调用widget_register()函数时,表单验证似乎没有做任何事情,数据发布,因为我可以看到来自分析器,但表单验证似乎没有对它做任何事情

帐户控制器

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

class Account extends CI_Controller {

public function index()
{
    $this->load->helper('url');
    #redirect('/');
}

public function access()
{
    $this->load->helper('url');

    #if($this->auth->loggedin()) redirect('/');

    $this->template->compile_page();
}

public function widget_register($template)
{
    $this->output->enable_profiler(TRUE);

    $this->load->helper('url');

    if($this->auth->loggedin()) redirect('/');

    if ($this->input->post('register_submit'))
    {
        $this->load->model('auth_model');

        $this->form_validation->set_rules('register_nickname', 'Nickname', 'required|min_length[3]|max_length[75]');
        $this->form_validation->set_rules('register_email_address', 'Email Address', 'required|valid_email|is_unique[users.email]');
        $this->form_validation->set_rules('register_confirm_email_address', 'Confirm Email Address', 'required|valid_email|matches[register_email_address]');
        $this->form_validation->set_rules('register_password', 'Password', 'required|min_length[5]');
        $this->form_validation->set_rules('register_confirm_password', 'Confirm Password', 'required|min_length[5]|matches[register_password]');

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

            echo 'validation successful';
            $nickname = $this->input->post('register_nickname');
            $email = $this->input->post('register_email_address');

            $generate_salt = $this->auth->generate_keys();

            $salt = $generate_salt['1'];

            $this->load->library('encrypt');

            $generate_password =  $salt . $this->input->post('register_password');
            $password = $this->encrypt->hash($generate_password);

            $generate_series_key = $this->auth->generate_keys();
            $user_series_key = $generate_series_key['1'];

            $this->db->query('INSERT INTO users (nickname, email, password, salt, register_date) VALUES ( "'.$nickname.'", "'.$email.'", "'.$password.'", "'.$salt.'", "'.time().'")');
        }
        else
        {
            echo 'invalid';
            echo validation_errors();
        }
    }

    $this->load->helper('form');

    $view_data = array();
    $view_data['form'] = form_open('account/access');
    $view_data['input_nickname'] = form_input(array('name' => 'register_nickname', 'value' => set_value('register_nickname'), 'id' => 'register_nickname', 'class' => 'common_input', 'size' => '55'));
    $view_data['input_email'] = form_input(array('name' => 'register_email_address', 'value' => set_value('register_email_address'), 'id' => 'register_email_address', 'class' => 'common_input', 'size' => '55'));
    $view_data['input_confirm_email'] = form_input(array('name' => 'register_confirm_email_address', 'value' => '', 'id' => 'register_confirm_email_address', 'class' => 'common_input', 'size' => '55'));
    $view_data['input_password'] = form_password(array('name' => 'register_password', 'value' => '', 'id' => 'register_password', 'class' => 'common_input', 'size' => '55'));
    $view_data['input_confirm_password'] = form_password(array('name' => 'register_confirm_password', 'value' => '', 'id' => 'register_confirm_password', 'class' => 'common_input', 'size' => '55'));
    $view_data['form_submit'] = form_submit('register_submit', 'Register');
    $view_data['/form'] = form_close();
    $view_data['validation_errors'] = validation_errors('<div class="error-box">', '</div>');


    return $this->parser->parse(FCPATH.'themes/'.$this->settings->settings['system_settings']['theme'].'/widgets/'.$template.'.html', $view_data, TRUE);

}

function widget_login()
{
    $this->load->helper('url');

    // user is already logged in

    if ($this->auth->loggedin())
    {
        return $this->load->view(FCPATH.'themes/'.$this->settings->settings['system_settings']['theme'].'/widgets/login/user.html', '', TRUE);
    }

    if($this->input->post('register'))
    {
        redirect('authentication/register');
    }

    // form submitted

    if ($this->input->post('login_email_address') && $this->input->post('login_password'))
    {
        $this->load->library('form_validation');
        $this->load->model('auth_model');

        $this->form_validation->set_rules('login_email_address', 'Email Address', 'required|valid_email');
        $this->form_validation->set_rules('login_password', 'Password', 'required|min_length[4]');

        if($this->form_validation->run() !== false)
        {
            // validation passed verify from db

            $remember = $this->input->post('remember') ? TRUE : FALSE;

            if($remember == 'remember');

            // check user exists and return user data

            $user_data = $this->auth_model->user_exists($this->input->post('email_address'), TRUE);

            if($user_data != FALSE)
            {
                // compare passwords

                if ($this->auth_model->check_password($this->input->post('password'), $this->input->post('email_address')))
                {
                    $this->auth->login($user_data->uid, $remember);
                    redirect('/');
                }
                else { $this->form_validation->set_custom_error('Incorrect password'); }
            }
            else { $this->form_validation->set_custom_error('There are no users with that email address'); }
        }
    }

    // show login form

    $this->load->helper('form');

    $view_data = array();
    $view_data['form'] = form_open();
    $view_data['input_email'] = form_input('login_email_address', set_value('login_email_address'), 'id="login_email_address"');
    $view_data['input_password'] = form_password('login_password', '', 'id="login_password"');
    $view_data['input_remember'] = form_checkbox('remember', 'rememberme');
    $view_data['form_submit'] = form_submit('login_submit', 'Login');
    $view_data['register_button'] = form_submit('register', 'Register');
    $view_data['/form'] = form_close();
    $view_data['validation_errors'] = validation_errors('<div class="error-box">', '</div>');

    return $this->parser->parse(FCPATH.'themes/'.$this->settings->settings['system_settings']['theme'].'/widgets/login/login.html', $view_data, TRUE);
}

function logout()
{   
    $this->load->helper('url');
    $this->session->sess_destroy();

    $this->load->helper('cookie');
    delete_cookie('aws_session');
    delete_cookie('aws_autologin_cookie');

    redirect('/');
}
}

和模板库

class Template {

private $settings;
private $_ci;
private $_controller = ''; # Controller in use
private $_method = ''; # Method in use
private $_is_mobile = FALSE; # Is the User agent a mobile?
private $cache_lifetime = '1';

private $page_output = "";
private $partials = array();
private $spts = array(); # Static page tags

function __construct()
{
    $this->_ci =& get_instance();
    $this->settings = $this->_ci->settings->settings;

    $this->_controller = $this->_ci->router->fetch_class();
    $this->_method = $this->_ci->router->fetch_method();

    $this->_ci->load->library('user_agent');
    $this->_is_mobile = $this->_ci->agent->is_mobile();

    log_message('debug', "Template Class Initialized");
}

function build_page()
{
    $page = $this->_ci->load->view(FCPATH.'themes/'.$this->settings['system_settings']['theme'].'/pages/'.$this->settings['page_settings']['template'].'.html', '', TRUE);

    $this->page_output .= $page;
}

# MAIN PAGE FUNCTIONS END ------------------------------------------------------------------------------------------

public function check_static_tags()
{
    if(preg_match_all('/{@[A-Z]{1,75}}/', $this->page_output, $matches))
    {
        $this->spts = $matches['0'];
        $this->spts = array_diff($this->spts, array('{@CONTENT}')); # removes stp_content from array list

        return TRUE;
    }
    return FALSE;
}

# Convert static tags

public function convert_static_tags()
{
    foreach($this->spts as $key => $static_tag)
    {
        $static_tag = str_replace(array('{','}'), '', $static_tag);
        $this->partials[$static_tag] = $this->build_widget($static_tag);
    }
}

# Convert widget tags

function column_widget_tags()
{
    if(preg_match_all('/{@COLUMN_[0-9]{1,11}}/', $this->page_output, $matches))
    {
        if(isset($this->settings['page_settings']['widgets']))
        {
            $columns = unserialize($this->settings['page_settings']['widgets']);
        }

        foreach($columns as $column_id => $widgets)
        {
            if(count($columns[$column_id]) > '0') // if the column contains widgets
            {
                $this->partials[''.$this->_stp.'_COLUMN_'.$column_id] = '';

                foreach($widgets as $key => $widget_id)
                {
                    // build the widget and add it to the $this->page_bits['column_id'] variable for replacing later in the compiler

                    $this->partials[''.$this->_stp.'_COLUMN_'.$column_id] .= $this->build_widget($widget_id);
                }
            }
            else
            {
                $this->partials[''.$this->_stp.'_COLUMN_'.$column_id] = '';
            }
        }
    }
}

# Build Widget

function build_widget($widget_id)
{
    if(is_numeric($widget_id))
    {
        $widget_query = $this->_ci->db->query('SELECT * FROM widgets WHERE id = "'.$widget_id.'"'); # BIND STATEMENTS
    }
    elseif(preg_match('/@[A-Z]{1,75}/', $widget_id))
    {           
        $widget_query = $this->_ci->db->query('SELECT * FROM widgets WHERE tag = "'.$widget_id.'"'); # BIND STATEMENTS
    }
    else
    {
        show_error('Could not get widget from database: '.$widget_id);
    }

    if($widget_query->num_rows() > 0)
    {
        $widget_info = $widget_query->row_array();

        // loads widgets parent controller, builds the widget (class method) and returns it

        $this->_ci->load->controller($widget_info['controller'], $widget_info['controller']);
        $method_name = 'widget_'.$widget_info['method'];
        $complete_widget = $this->_ci->$widget_info['controller']->$method_name($widget_info['template']);

        return $complete_widget;
    }
    else
    {
        show_error('The requested widget could not be loaded: '.$widget_id);
    }
}

# Replace Partials

function replace_partials()
{   
    $this->_ci->load->library('parser');
    $this->page_output = $this->_ci->parser->parse_string($this->page_output, $this->partials, TRUE);
}

## METADATA

public function prepend_metadata($line)
{
    array_unshift($this->_metadata, $line);
    return $this;
}


# Put extra javascipt, css, meta tags, etc after other head data

public function append_metadata($line)
{
    $this->_metadata[] = $line;
    return $this;
}

# Set metadata for output later

public function set_metadata($name, $content, $type = 'meta')
{
    $name = htmlspecialchars(strip_tags($name));
    $content = htmlspecialchars(strip_tags($content));

    // Keywords with no comments? ARG! comment them
    if ($name == 'keywords' AND ! strpos($content, ','))
    {
        $content = preg_replace('/[\s]+/', ', ', trim($content));
    }

    switch($type)
    {
        case 'meta':
            $this->_metadata[$name] = '<meta name="'.$name.'" content="'.$content.'" />';
        break;

        case 'link':
            $this->_metadata[$content] = '<link rel="'.$name.'" href="'.$content.'" />';
        break;
    }

    return $this;
}

# Embed page into layout wrapper

function layout()
{
    $this->_ci->load->helper('html');

    $this->append_metadata(link_tag('themes/'.$this->settings['system_settings']['theme'].'/css/layout.css')); # default stylesheet
    $this->append_metadata('<link rel="shortcut icon" href="/favicon2.ico" />'); # default favicon

    $template['template.title'] = $this->settings['page_settings']['title']; # Page title, can be overidden by the controller ?
    $template['template.metadata']  = implode("\n\t\t", $this->_metadata); # Metadata
    $template['template.body'] = $this->page_output; # The page

    return $this->_ci->parser->parse(FCPATH.'assets/layout/L6_layout_wrapper.html', $template, TRUE);
}

# Run all functions to build page and set in output class

function compile_page($content_data = '')
{
    $this->partials['@CONTENT'] = $content_data;

    $this->build_page();

    $this->column_widget_tags();

    if($this->check_static_tags())
    {
        $this->convert_static_tags();
        $this->replace_partials();
    }

    $output = $this->layout();

    $this->_ci->output->set_header('Expires: Sat, 01 Jan 2000 00:00:01 GMT');
    $this->_ci->output->set_header('Cache-Control: no-store, no-cache, must-revalidate');
    $this->_ci->output->set_header('Cache-Control: post-check=0, pre-check=0, max-age=0');
    $this->_ci->output->set_header('Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
    $this->_ci->output->set_header('Pragma: no-cache');

    # Let CI do the caching instead of the browser
    #$this->_ci->output->cache($this->cache_lifetime);

    $this->_ci->output->append_output($output);
}
}

对于这篇令人难以置信的长篇帖子感到抱歉,我真的很难过,不想错过任何代码,提前谢谢!

0 个答案:

没有答案