我需要帮助将验证错误传递到codeigniter(版本3.1.8)中的smarty(3.1.33)模板。
似乎在smarty模板中,您不能直接使用来自表单帮助程序的codeigniter函数,例如validation_errors()
或form_error('fieldname').
,至少在我看来,在smarty模板{validation_errors()}
中类似这样不会产生任何结果。
因此,我尝试使用$this->form_validation->error_array()
在控制器中获取错误消息,并将其分配给通过 $ this-> smartyci-> assign 分配给模板的变量。方法。尽管我提交了一个空白表格,但是模板中没有显示错误消息。
如果您有任何想法,请告诉我。
非常感谢,
下面是我的代码:
控制器( Home.php ):
class Home extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library( 'smartyci' );
$this->load->library('form_validation');
$this->load->helper('form');
}
public function index()
{
$title = "Court Monitor";
$date = date("Y");
//$this->smartyci->useCached('home.tpl' );
$this->smartyci->assign('data', array(
"logged_in" => false,
"date" => $date,
"title" => $title
));
$this->smartyci->display('home.tpl');
//print_r($result);
}
............
public function register_email ()
{
$title = "Registration";
$date = date("Y");
$this->form_validation->set_rules('nume', "Nume", "required");
$this->form_validation->set_rules('prenume', "Prenume", "required");
$this->form_validation->set_rules('email', "Email", "required|valid_email");
$this->form_validation->set_message('required', 'This field is required');
if ($this->form_validation->run() === false) {
$this->smartyci->assign('data', array(
"logged_in" => false,
"date" => $date,
"title" => $title,
"errors" => $this->form_validation->error_array()
));
$this->smartyci->display('register_email.tpl');
} else {
redirect('login');
}
}
模板( register_email.tpl ):
{extends "layout.tpl"}
{block name=title}
{$data.title}
{/block}
{block name=content}
<div class="well well-sm text-center">
<h3>Monitor Dosare</h3>
<p class="intro">Bine ați venit la modulul nostru de urmărire a
dosarelor aflate pe rolul instanțelor judectorești</p>
</div>
<div>
{foreach $data.errors as $error}
<p>{$erorr}</p>
{/foreach}
</div>
<form class="col-xs-12 col-sm-10 col-md-8 col-lg-6
register-form" name="register" action="register_email" method="post" novalidate>
<legend class="text-center">Introduceți datele pentru înregistrare</legend>
<div class="form-group">
<label class="">Nume </label>
<input class="form-control" type="text" name="nume" value=""/>
</div>
<div class="form-group">
<label>Prenume </label>
<input class="form-control" type="text" name="prenume" value=""/>
</div>
<div class="form-group">
<label>E-mail </label>
<input class="form-control" type="text" name="email" value=""/>
</div>
<div class="form-group">
<label>Parola</label>
<input class="form-control" type="password" name="parola"/>
</div>
<div class="form-group">
<label>Confirmare Parola</label>
<input class="form-control" type="password" name="confirm_parola" />
</div>
<div class="form-group g-recaptcha" data-sitekey="6Le3pmEUAAAAAGHc84q7hEhsR_iBmoFsbQhj5uCy"></div>
<input type="submit" name="submit" class="btn btn-primary" value="Înregistrează-mă!" />
</form>
{/block}