Adding Custom Fields
This chapter focuses on a single field and what can you do with it by extending Form_Field class or by relying on capabilities of Agile Toolkit and enhancing existing one.
class GreetingForm extends Form {
function init(){
parent::init();
$this->addField('line','name')->validateNotNull();
$this->addSubmit('Greet');
if($this->isSubmitted()){
$this->js()->univ()->alert('Hello, '.$this->get('name'))->execute();
}
}
}
Accessing existing form fields, changing label
By default label is taken form field name, although you can specify it as a 3rd argument to addField() method. You can change label of existing field by using setCaption() method
Result |
Code
$f=$p->add('GreetingForm'); |
Customize Field Attributes
You can specify any additional attribute to a HTML element which is responsible for drawing field input.
Result |
Code
$f=$p->add('GreetingForm'); |
Disable field
Disabling field by calling disable() is preferred over setting the attribute "disabled" for 2 reasons: 1) this ensures that field data will not be saved, even if it's present in POST, 2) some fields may contain multiple input elements and will want to re-define this method.
Disabled fields do not handle JavaScript events
Result |
Code
$f=$p->add('GreetingForm'); |
Disabling Enter
Formely a method in Form, this is now implemented as JavaScript enhancement.
Result |
Code
$f=$p->add('GreetingForm'); |
Working with field template
Like any other object in Agile Toolkit, you can change template text or insert objects into template.
Result |
Code
$f=$p->add('GreetingForm'); |
Adding Hints
Although hint is also added by using a template, there is a special function for it.
Result |
Code
$f=$p->add('GreetingForm'); |
Default Value and Reloading Fields
set() will change default value. Calling get() after isSubmitted() will properly prioritize submitted value. This example also show-cases field reloading
Result |
Code
$f=$p->add('GreetingForm'); |
Detect which button was clicked
This example also show-cases field reloading
Result |
Code
$f=$p->add('Form'); |
Standard Separator
Separator will close and reopen <fieldset> tag. This can be used to separate fields visually, but is also used for splitting fields into multiple columns where CSS supports it
Result |
Code
$f=$p->add('GreetingForm'); |
Preventing data loss
While some browsers already look for un-saved form data and will warn you when you attempt to close tab, there are no technique to do same thing through AJAX. Agile Toolkit automatically checks for any un-saved fields when AJAX attempts to destroy it and asks user to confirm it.
Result |
Code
$f=$p->add('GreetingForm'); |
Hide field line with JavaScript
You might want to hide the whole line with javascript
Result |
Code
$f=$p->add('GreetingForm'); |
Cross-submit
While you keep the form object, you can bind it to any other object's event
Result |
Code
$f=$p->add('GreetingForm'); |
Home-made slider implementation
While you keep the form object, you can bind it to any other object's event
Result |
Code
|
Creating Field Class
Creating your own field class might still be necessary sometimes.
Home-made slider implementation
While you keep the form object, you can bind it to any other object's event
Result |
Code
|
class Form_Field_DatePicker extends Form_Field {
public $options=array();
function getInput($attr=array()){
// $this->value contains date in MySQL format
// we need it in locale format
$this->js(true)->datepicker(array_merge(array(
'duration'=>0,
'showOn'=>'button',
'buttonImage'=>$this->api->locateURL('images','calendar.gif'),
'buttonImageOnly'=> true,
'changeMonth'=>true,
'changeYear'=>true,
'dateFormat'=>$this->api->getConfig('locale/date_js','dd/mm/yy')
),$this->options));
return parent::getInput(array_merge(
array(
'value'=>$this->value?(date($this->api->getConfig('locale/date','d/m/Y'),strtotime($this->value))):'',
),$attr
));
}
function set($value){
// value can be valid date format, as in config['locale']['date']
if(!$value)return parent::set($value);
list($d,$m,$y)=explode('/',$value);
if($y)$value=join('/',array($m,$d,$y));
elseif($m)$value=join('/',array($m,$d));
$value=date('Y-m-d',strtotime($value));
return parent::set($value);
}
function get(){
$value=parent::get();
// date cannot be empty string
if($value=='')return null;
return $value;
}
}
class GreetingForm extends Form {
function init(){
parent::init();
$this->addField('line','name')->validateNotNull();
$this->addSubmit('Greet');
if($this->isSubmitted()){
$this->js()->univ()->alert('Hello, '.$this->get('name'))->execute();
}
}
}
