Custom Form Helpers in Solar

Another thing that Solar makes easy is the creation of custom form helpers. These can be anything outside of the usual input types (like text, checkbox, textarea, etc) and can be used to make helpful, more complex inputs for your app.

In a previous post, I worked some with the Solar_Form class to create a login form. This just used simple input fields (text and password) to create the login form. I came to another part of my application where I needed a calendar helper to fill in some fields. I wanted to eliminate some of the user issues that could come with invalid dates being entered, so I opted to create a custom form field instead. The field is a normal text field, marked as READONLY with a link that launches a Javascript popup with the calendar picker.

First, we need a little structure – here’s how the application is structured (basically):

MyApp
 App
  Base
   Helper
    FormDate.php
   Layout
   Locale
   View
  Event
  Event.php
   Helper
   Layout
   Locale
   View
    index.php

There’s a few things involved here:

  • The form helper class – FormDate.php
  • The Event.php class (controller) where the form will be created
  • The View for the Event controller to output the form and some Javascript

We’ll start with building the form since that’s more familiar territory:

[php]
class MySite_App_Event extends MySite_App_Base {
protected $_action_default = ‘index';
protected $_layout = ‘default';
protected $_view =’index';
public $forms;

public function actionIndex(){
$form = Solar::factory(‘Solar_Form’);
$form->setElements(array(
‘event_title’=>array(
‘type’=>’text’,
‘label’=>’Event Title’,
‘require’=>true,
‘valid’=>array(
array(‘notBlank’,’Please enter an event title!’)
)
),
‘event_start’=>array(
‘type’ =>’date’,
‘label’ =>’Start Date’,
‘require’=>true,
‘valid’ =>array(
array(‘notBlank’,’Please select a start date!’)
)
),
‘sub’=>array(
‘type’=>’submit’,
‘value’=>’create event’
)
));
$form->feedback=NULL;
$this->forms[‘create_form’]=$form;
}
}
[/php]

If you look close, you’ll spot something custom in there – on the event_start element, there’s a type of “date”. This how our special form helper is called. Solar does a little magic here – it looks at the types of each of the elements and looks in a few places for something that matches that type. Most of the elements are Solar’s default types so it falls back to the ones in the Solar directories. The “date” type, however, isn’t one of them and the app will break if you leave it like this.

So, what’s a developer to do? Well, create a custom form handler of course! This is where the FormDate.php file comes into play. Here’s what ours contains:

[php]
class MySite_App_Base_Helper_FormDate extends Solar_View_Helper_FormElement {

public function formDate($info){
$this->_prepare($info);
return ‘ . ' name="' . $this-/>_view->escape($this->_name) . ‘”‘
. ‘ value=”‘ . $this->_view->escape($this->_value) . ‘”‘
. $this->_view->attribs($this->_attribs)
. ‘ READONLY/> Proudly powered by WordPress. Theme: Flat by YoArts.