モデルをヒットする前に、投稿データを検証する必要があります。検証 check()を実行していないため、検証ルールは実行されていません。
私は次のようなことをします:
// ./application/classes/controller/user
class Controller_User extends Controller
{
public function action_register()
{
if (isset($_POST) AND Valid::not_empty($_POST)) {
$post = Validation::factory($_POST)
->rule('name', 'not_empty');
if ($post->check()) {
try {
echo 'Success';
/**
* Post is successfully validated, do ORM
* stuff here
*/
} catch (ORM_Validation_Exception $e) {
/**
* Do ORM validation exception stuff here
*/
}
} else {
/**
* $post->check() failed, show the errors
*/
$errors = $post->errors('registration');
print '<pre>';
print_r($errors);
print '</pre>';
}
}
}
}
Registration.php は、「長さ」のスペルミスを修正することを除いて、ほとんど同じままです。
// ./application/messages/registration.php
return array(
'name' => array(
'not_empty' => 'Please enter your username.',
),
'password' => array(
'matches' => 'Passwords doesn\'t match',
'not_empty' => 'Please enter your password'
),
'email' => array(
'email' => 'Your email isn\'t valid',
'not_empty' => 'Please enter your email'
),
'about-me' => array(
'max_length' => 'You cann\'ot exceed 300 characters limit'
),
'_external' => array(
'username' => 'This username already exist'
)
);
次に、空の「名前」フィールドを送信すると、次が返されます。
Array
(
[name] => Please enter your username.
)