Ok i would like to make the simplest way possible to hide variables and execute code without moving to another page.
So my main aim here is to only show error messages after the first submit, the next aim is to minimise code and use best practices. I am building this up as the default code for cabbit generator and to make sure i am using best practise for my code.
Cabbit controller for the blog
# /blog/create/
public function create()
{
# Create a Blog post
$Blog = Blog::create(array('title' => $_REQUEST['title'], 'body' => $_REQUEST['body']));
# Error Checking
if (count($Blog->errors) > 0)
$this->Cabbit->Template->errors = join(', ',$Blog->errors->full_messages()) . "\n\n";
# Show create view
$this->init();
}
The corresponding view
<?php if(!empty($errors)) {echo $errors;}; ?>
<form action="/blog/create/" method="post">
<p><label for="title">Title: </label><br /><input id="title" type="text" name="title" value="<?php if (!empty ($_REQUEST['title'])){ echo $_REQUEST['title'];} ?>" /></p>
<p><label for="body">Body: </label><br /><textarea id="body" name="body" cols="30" rows="10"><?php if (!empty ($_REQUEST['body'])){ echo $_REQUEST['body'];} ?></textarea></p>
<input type="hidden" name="save" value="true" />
<p><input type="submit" value="Post" /></p>
</form>
The corresponding model
class Blog extends ActiveRecord\Model
{
static $validates_presence_of = array(array('title'), array('body'));
static $validates_length_of = array(array('title', 'within' => array(10,128)));
}