[英]Kohana 3.3 & Kostache - Unable to display ORM Validation Errors in Form
我正在嘗試使用Kohana 3.3和Kostache作為模板系統為我的網站創建一個用戶注冊頁面。 我很難使用表單驗證來在同一頁面上顯示驗證錯誤。 現在,當我單擊表單提交按鈕並發送表單中的所有空值(用戶名,電子郵件和密碼)時,我得到的只是刷新表單,當我應該收到驗證錯誤時,例如用戶名為空,電子郵件為空等等(即使用Model_Auth_User)。
我不知道我在做什么錯。
模型:
class Model_User extends Model_Auth_User
{
public function rules()
{
return array(
'username' => array(
array('not_empty'),
array('alpha_dash'),
array('min_length', array(':value', 4)),
array('max_length', array(':value', 20)),
array(array($this, 'unique'), array('username', ':value')),
),
'email' => array(
array('not_empty'),
array('min_length', array(':value', 4)),
array('max_length', array(':value', 127)),
array('email'),
),
);
}
}
控制器:
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_User extends Controller {
public function action_index()
{
}
public function action_login()
{
$renderer = Kostache_Layout::factory();
$this->response->body($renderer->render(new View_FrontEnd_User, 'frontend/login'));
}
public function action_signup()
{
$renderer = Kostache_Layout::factory();
$this->response->body($renderer->render(new View_FrontEnd_User, 'frontend/signup'));
}
public function action_createuser()
{
$signupView = new View_FrontEnd_User();
try {
$user = ORM::factory('User');
$user->username = $this->request->post('username');
$user->password = $this->request->post('password');
$user->email = $this->request->post('email');
$user->save();
}
catch (ORM_Validation_Exception $e)
{
$errors = $e->errors();
$signupView->errors = $errors;
}
$renderer = Kostache_Layout::factory();
$this->response->body($renderer->render(new View_FrontEnd_User, 'frontend/signup'));
}
}
視圖
<?php defined('SYSPATH') or die('No direct script access.');
class View_FrontEnd_User extends View_Main
{
public $errors = array();
}
注冊小胡子
<p>
<form action="user/createuser" method="post">
<fieldset style="width: 20em;">
<legend>User Registration</legend>
<label>Enter your username</label>
<input type="text" name="username" />
<label for="username" class="error">
{{#errors}}{{username}}{{/errors}}
</label>
<label>Enter your password</label>
<input type="password" name="password" />
<label for="password" class="error">
{{#errors}}{{password}}{{/errors}}
</label>
<label>Email</label>
<input type="text" name="email" />
<input type="submit" value="Submit" class="nice blue radius button" />
</fieldset>
</form>
{{#errors}}{{.}}{{/errors}}
</p>
在此先非常感謝您提供給我的任何指導。 我已經花了幾個小時,但仍然無法正常工作:(
更改
$this->response->body($renderer->render(new View_FrontEnd_User, 'frontend/signup'));
至
$this->response->body($renderer->render($signupView, 'frontend/signup'));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.