Skip to content
This repository has been archived by the owner on Nov 28, 2023. It is now read-only.

Maybe the most important widget: the field

fballiano edited this page Aug 31, 2012 · 1 revision

P4A_Field is a widget that allows users to input data. Every field has a type between:

  • text: a single line data input
  • date: a single line data input for dates, values are automatically formatted respecting your locale settings. This type features a nice date picker too.
  • checkbox: the name is self-explanatory
  • select: HTML combobox
  • radio: a group of HTML radio buttons
  • password: HTML input type="password"
  • file: file upload with image/audio/video preview
  • textarea: multiline data input
  • rich_textarea: multiline data input featuring rich text editing controls
  • multicheckbox: a group of checkboxes
  • multiselect: HTML combobox featuring multiple selections
  • hidden: HTML input type="hidden"
  • label: converts the field to a simple data output, user can simply read the value

How to change a field type

Use the setType method:

$field->setType("checkbox");

Sample usage

$this->build("p4a_field", "field")
	->setType("text") // optional, text is the default type
	->setWidth(200) // optional, fields already have a default width
	->setLabel("Field") //optional, fields already have an auto-generated label
        ->setTooltip("Please enter you name here...") //optional, set a tooltip for your field
	;
$frame->anchor($this->field);

Particular field types

to be written

Getting and setting data to a field

You can use the setNewValue, getValue, getNewValue methods to get and set data to a field. Keep in mind that the getValue method gives you the current (or old) value and logical the getNewValue the newly changed data.

$this->build("p4a_field", "txt_myfield")
	->setType("text") // optional, text is the default type
	->setWidth(200) // optional, fields already have a default width
	->setLabel("MyField"); //optional, fields already have an auto-generated label

$this->txt_myfield->setNewValue("this is my text");

$sMyNewVar = $this->txt_myfield->getNewValue();
$sMyOldVar = $this->txt_myfield->getValue();

//The same applies to DB fields
$this->fields->my_db_field->setNewValue("hey this is some new text in my field!");

Using an array as data source

You can use a array as source for the following field types:

  • select
  • radio
  • multicheckbox
  • multiselect
//build an array. You can also use an array with different value/key like array("id"=>0, "value"=>"item1")
$aTestArray[] = array("value"=>"item1");
$aTestArray[] = array("value"=>"item2");
$aTestArray[] = array("value"=>"item3");
		
$this->build("p4a_array_source", "db_array") //build the array source
	->setPk("value")
	->load($aTestArray);
			
$this->build("p4a_field", "msl_field") //build the select field
	->setType("select")
	->setLabel("Select")
	->setSource($this->db_array) //setting the source for the field
	->setSourceDescriptionField("value") //set the description field
	->setSourceValueField("value"); //set the value field
			
$this->display("main", $this->msl_field); //display the field

Adding a simple input mask

You can add a simple input mask for alphanumeric characters like this:

$this->build("p4a_field", "txt_field")
	->setLabel("Customer Code")
	->setInputMask("aaa-99999/a");
			
$this->display("main", $this->txt_field);

User get this on his field -__/_ and can enter only a-z, A-Z and 0-9

Adding validation to your fields

You can add validation to you fields with the addValidator method. P4A implements different validators from the Zend framework. To see a list of all available validators take a look at the code-reference->p4a_validate.

$this->fields->my_db_field->addValidator(new P4A_Validate_Between(0,9)); //check if value is between 0 and 9

The validation will take place when trying to save the record. When a value is not valid, a red canvas will been drawn around the input field.

Disable or enable a field (or widget)

You can easly disable or enable a field for editing like this:

$this->my_field->disable();
$this->my_field->enable();

Setting the length limit of a text field

You can limit the amount of characters for a text field like this:

$this->my_field->setProperty("maxlength", "10");