The Controller file at app/controllers/post_controller.php
class PostController extends ApplicationController { function create(){ $this->Post =& new Post($this->params["post"]); if($this->Post->save()){ $this->redirectToAction("show", array('id' => $Post->getId())); }else{ $this->Post->errors = $this->Post->getErrors(); } } //... function show(){ } }
The View file at app/views/post/show.tpl
{?Post.errors}
The post couldn't be saved due to these errors:
<ul>
{loop Post.errors}
<li>{error}</li>
{end}
</ul>
{end}
<%= form "post" %>
or even shorter:
<%= error_messages_for 'post' %>
The Model file at app/models/post.php
class Post extends ActiveRecord { function validate(){ $this->validatesPresenceOf( array('title', 'body'), "Missing required field"); $this->get('title') > 10 ? $this->addError("title", "must be at most 10 characters") : null; } }
Note: When using validatesConfirmationOf() make sure you also use validatesPresenceOf() if you want to make sure the field exists.