by Janis Volbergs, June 16th,2012
In this article I will talk about different list-type form fields, about their initialization, linking with model and finetuning. So, lets get started.
In Agile Toolkit you will find following form fields that implement list selectors out-of-the box:
All of the list selectors extend more abstract ValueList field, which:
* handles value list fetching
* handles post data loading and value serialization
ValueList is Smart - if model is set for the field, during getValueList call, it would automatically fetch list of records matching the model.
Okay, let's now turn to some real life examples. Let's say we have a Model_A with following implementation:
<?php
class Model_A extends Model_Table {
public $entity_code = "aa";
public $title_field = "b";
function init(){
parent::init();
$this->addField("b");
$this->addField("x")
->defaultValue(0)
->listData(array(1=>1,2=>2,3=>3))
->display(array("form" => "radio"));
$field = $this->addField("y")
->defaultValue(0)
->listData(array(1=>1,2=>2,3=>3));
// $field->emptyText(null);
}
}
Now, let's create a simple page and add CRUD
<?php
class page_e extends Page {
function init(){
parent::init();
$c=$this->add("CRUD");
$c->setModel("A");
}
}
And this is the result:
As you can see, Field X is displayed as Radio in form, whereas Y is displayed as normal dropdown. One thing that might be worrying, is that the radio list has "Please, Select" as default value. Good news are - we can easily remove that from both Radio and Dropdown in the following way:
<?php
//cut-start
$this->addField("x")
->emptyText(null)
//cut-end
Now, we've got following result:
Simple, isn't it? Now, let's try out checkbox list. First, let's amend our Model:
<?php
//cut-start
$this->addField("x")
->emptyText(null)
->defaultValue(0)
->listData(array(1=>1,2=>2,3=>3))
->display(array("form" => "checkboxlist"));
//cut-end
And that would result in following form
If you use in your code hasOne functionallity, when form will be drawn, setModel call will be made on the DropDown field and the ValueList field will fetch values properly from database. Let's take a look at our other model:
<?php
class Model_B extends Model_Table {
public $entity_code = "bb";
public $title_field = "c";
function init(){
parent::init();
$this->addField("c");
$this->hasOne("A", "a_id");
}
}
That results in nice form:
Now, let's say you would like to get rid of the "Please, select" - how do we do that? Correct, by adding ->emptyText(null)
<?php
//cut-start
$this->hasOne("A", "a_id")->emptyText(null);
//cut-end
And that gives us following result:
With Agile Toolkit 4.2, we have more control over list type fields. Dynamically fetched, or statically set, same rulles apply. In Model you have full controll over the look and feel as well as the empty text.
There are more tricks that can be done, e.g. autocomplete with dynamic or static lists, event binding on value change etc. Will give more examples in other blog post.
Good luck, and see you around
© since 2010