====== How to Validate ======
**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:
{loop Post.errors}
- {error}
{end}
{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;
}
}
===== More information =====
* [[understanding-validations|Understanding Validations]]
* [[http://api.akelos.org/ActiveRecord/Base/AkActiveRecord.html#methodvalidate|Validation API Documentation]] has all sorts of details about what methods are available.
Note: When using validatesConfirmationOf() make sure you also use validatesPresenceOf() if you want to make sure the field exists.