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):


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:

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’);
‘label’=>’Event Title’,
array(‘notBlank’,’Please enter an event title!’)
‘type’ =>’date’,
‘label’ =>’Start Date’,
‘valid’ =>array(
array(‘notBlank’,’Please select a start date!’)
‘value’=>’create event’

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:

class MySite_App_Base_Helper_FormDate extends Solar_View_Helper_FormElement {

public function formDate($info){
return ‘ . ' name="' . $this-/>_view->escape($this->_name) . ‘”‘
. ‘ value=”‘ . $this->_view->escape($this->_value) . ‘”‘
. $this->_view->attribs($this->_attribs)

Post navigation


  1. just a note: adding the javascript could be automatized using $this->_view->js()->addInlineScript() inside the FormDate helper. then, when calling the helper, the necessary inline javascript script would be added to the page head. solar beauty! :)

  2. This Solar thing seems pretty nice. At least the form features, doesn’t seem to have that many other built-in things. There’s just one thing, though. It doesn’t look like you have much to say on how the form will end up looking like markup-wise. I think I would prefer something that perhaps generates the input elements, but would let me pick where to put them. Something like $this->forms[‘create_form’]->inputs[‘foobar’]

    What would be really cool would be a class that could build a form, a parser and JS validators based on a DB table or a row. Well, it might need some assistance, since the table column names wouldn’t probably be very good labels on the form… so perhaps it isn’t very feasible afterall.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>