CodeIgniter,Smarty和Form Helper。不触发form_validation-> run()

时间:2012-07-26 18:22:53

标签: codeigniter smarty codeigniter-form-helper

我在Smarty中使用CodeIgniter。我遇到的问题是使用Form helper时。我使用了the code found here

因此,当我实际使用CI原生form_openform_close打开并关闭表单代码时,表单提交后form_validation->run()仍然始终为false

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

class User extends CI_Controller {

public function index()
{
    $this->register();
}

function register()
{
    $data['title'] = 'Register';
    $this->load->library('form_validation');
    //$this->form_validation->set_rules()
    if($this->form_validation->run() == FALSE)
    {
        //the form has not been submitted or there are errors
        $this->parser->parse("register", $data); 
    }
    else
    {
        //validated and submitted
        die();
    }
}

function login()
{
    $data['title'] = 'Login';
    $this->parser->parse("login", $data);
}
}

模板是

Registration page
{form url='user/register'}

<table>
    <tr>
        <td>Username</td>
        <td><input type="text" name="reg_username" id="reg_username"></td>
    </tr>
    <tr>
        <td>Password</td>
        <td><input type="password" name="reg_password" id="reg_password"></td>
    </tr>
    <tr>
        <td>First Name</td>
        <td><input type="text" name="reg_firstname" id="reg_firstname"></td>
    </tr>
    <tr>
        <td>Last Name</td>
        <td><input type="text" name="reg_lastname" id="reg_lastname"></td>
    </tr>
    <tr>
        <td></td>
        <td><input type="submit" value="submitted"/></td>
    </tr>
</table>

{form}

我希望在提交表单时执行die()命令不是 - 请告诉我我做错了什么?

P.S。我用来将Smarty集成到CodeIgniter中的方法是https://github.com/Vheissu/Ci-Smarty

1 个答案:

答案 0 :(得分:2)

您的register()方法必须set at least one validation rule。例如,像:

$this->form_validation->set_rules('reg_username', 'Username', 'required');

来自Form_validation class documentation

  

由于您还没有告诉Form Validation类验证任何内容,因此默认情况下它返回FALSE(布尔值false)。如果run()函数已成功应用您的规则而没有任何失败,则TRUE函数仅返回{{1}}。