1

我正在将 CodeIgniter 与 Smarty 一起使用。我遇到的问题是使用表单助手时。我使用了这里找到的代码

所以当我打开和关闭表单标签时,实际上是使用 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()在提交表单时执行命令不是 - 请告诉我我做错了什么?

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

4

1 回答 1

2

您必须在方法中设置至少一个验证规则register()。例如,类似:

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

Form_validation 类文档中:

由于您尚未告诉 Form Validation 类验证任何内容,因此默认情况下它返回 FALSE(布尔值 false)。该run()函数仅TRUE在成功应用您的规则且没有任何失败的情况下返回。

于 2012-07-26T19:19:18.993 回答