BlogTutorial:Add Page

From Aspen Documentation

Now, we need to create the ability to actually add a new blog entry. In this step we'll create a basic form and code that processes the data for that.

Files we'll be editing in this step:

  • modules/Blog/Blog_Admin.php
  • modules/Blog/templates/add.tpl.php

Form Processing Code

Open for Editing: modules/Blog/Blog_Admin.php

We're now going to be editing the add function. There many ways you can choose to handle forms in the framework, but we're going to cover the most common.

	public function add(){ 
 
		// tell our form handler that we'll be using
		// fields present in the "blog" table
		$this->APP->form->loadTable('blog');
 
		// if form has been submitted
		if($this->APP->form->isSubmitted()){
 
                        // set the date posted value to the current
			$this->APP->form->setCurrentValue('date_posted', date("Y-m-d H:i:s"));
 
			// insert a new record with available data
			if($this->APP->form->save()){
				// if successful insert, redirect to the list
				$this->APP->router->redirect('view');
			}
		}
 
		// make sure the template has access to all current values
		$data['values'] = $this->APP->form->getCurrentValues();
 
		$this->APP->template->addView($this->APP->template->getTemplateDir().DS . 'header.tpl.php');
		$this->APP->template->addView($this->APP->template->getModuleTemplateDir().DS . 'add.tpl.php');
		$this->APP->template->addView($this->APP->template->getTemplateDir().DS . 'footer.tpl.php');
		$this->APP->template->display($data);
 
	}

Let's quickly look at each step so we understand what's happening.

$this->APP->form->loadTable('blog');

This first process tells the form class that it will be handling data that relates to fields in the blog table. Behind the scenes the form class loads the current table structure into memory so it knows what to expect.

if($this->APP->form->isSubmitted()){

isSubmitted returns either true or false if there is POST value for an element named "submit". It's a good idea to name your form submit button "submit", as it will likely never need to be changed. If that value is present, the form has been submitted and we need to process it.

This function loads the POST values that match the expected database fields automatically.

Note: This method requires that your form field names match their related database table field names.

$this->APP->form->setCurrentValue('date_posted', date("Y-m-d H:i:s"));

Then, we force the date_posted field to have a value of the current datetime. Since this value didn't come from our form, we use the setCurrentValue function to set it manually. This will override any values coming from our form unless you move the placement of the statement.

if($this->APP->form->save()){

This function instructs the form (and model) class to insert the current values into the table we opened earlier. This function returns the new record ID upon success.

$this->APP->router->redirect('view');

Upon success, the redirect function instructs the system to send the user to the view page where we're already listing out the records.

Creating the Form

Create a new template file for our add form modules/Blog/templates/add.tpl.php.

Next, we're going to build a standard html form.

<h3>Add Blog Record</h3>
 
<form action="<?php print $this->createFormAction(); ?>" method="post">
	<p>
		<label for="entry_title">Entry Title:</label><br />
		<input type="text" name="entry_title" id="entry_title" value="<?php print $values['entry_title']; ?>" class="text" />
	</p>
	<p>
		<label for="entry_body">Entry Body:</label><br />
		<input type="text" name="entry_body" id="entry_body" value="<?php print $values['entry_body']; ?>" class="text" />
	</p>
	<p>
		<label for="date_posted">Date Posted:</label><br />
		<input type="text" name="date_posted" id="date_posted" value="<?php print $values['date_posted']; ?>" class="text" />
	</p>
	<p><input type="submit" name="submit" value="Add" /></p>
</form>

That form is almost entirely standard html, so there's nothing new you need to learn with this. The only difference is that we're setting the form action using:

<?php print $this->createFormAction(); ?>

This function generates a form action url - using the current page and any id that's set in the GET parameters. This particular function will automatically encode the url so that we maintain W3C validation.

Save and upload both files.

Try It

Go ahead and try out your form. Upon pressing "submit" you should see your new record in the list.

Next: Edit Entry Page