You need to install the example component created in previous article.

 

Back-End - Part 2 edit single record

Please note: this page is under update!
Contents here may refer both to current or previous version of Joomla! component builder, and contents may be related to Joomla! 1.5 version.

The Joomla! MVC component generated has a full functional edit, but, of course, this is only a basic editing. You have to modify code for:

  • add javaScript client side validation (if custom validation is required)
  • add PHP server side validation (if custom validation is required)
  • add JTable validation php code (if special checks are required)
  • check data filtering before store data in to table (if html or raw code is allowed)
  • manage 'Select List' values
  • manage related table fields (customList.php for foreign keys, only for not team version)

We will modify only the 'book' section of our test component, we don't care about the other (authors and editors) because modification are quite identical.

 

Validation set up in Joomla Component generationAdd javaScript client side validation

When you select the fields for editing, you will see a drop down menu in the Validator client side coloum; username, password, email and numeric are joomla (mootools) validators, so no code is needed.

When you select the custom type, then a mootools javascript is added by Marco's Component Maker for Joomla! in \models\forms\[single object name].js; this js includes java script skeleton for input validation :

window.addEvent('domready', function() {
    document.formvalidator.setHandler('[field name]', function (value) {
            return true;
            //regex=/^[^0-9]+$/;
            //return regex.test(value);
    }); });


You have to complete it with the needed validation code. Every handler is identified by the field name, return true if value is acceptable, false otherwise.

 

Add PHP server side validation

You can check submitted values by selecting the custom value in the Validator server side coloum.

When select the custom validator then a [record name][fieldname].php file will be created in the \models\rules directory, you have to implement the test function code: return true if value is acceptable, false otherwise.

see: \models\rules\authorbookpublished.php

public function test(&$element, $value, $group = null, &$input = null, &$form = null){
	return ( ($value>0) && ($value<1000));
}

By adding this code we accept a number of published books between 0 and 1000 for each author.

 

Add JTable validation php code

You can add server side data consistency also by overriding the public check() method in JTable instance (by example in \tables\book.php); the check() method is called by the store() method in model, invoked by the save() method in controller.

This was the standard input validation in pre 2.5 Joomla! versions, but when you need complex validation code this is the only way.

 

Check data filtering

In Joomla 2.5 & Joomla 3.x, there in no need of data filtering at MVC code level. input filter is managed by framework. Use filter attribute in form definition.
see xml files in \models\forms\.

 

Manage 'Select List' values

If you set the 'Render As' field configuration to 'Select List' remember to add list values in form definition. see xml files in \models\forms\.

 

Manage related table fields (foreign keys)

If you set the 'Render As' field configuration to 'customList.php' a file [record_name][Field_name].php is written into the \models\fields\ directory.

You have to edit these files to supply the list of values required for the field. The component generator write code for quering the db, you have to insert correct fields and table name in to the sql code. Anyway you can change the function to use other data.

If you are using the internal team version of the component creator, and you selected a foreign key, you have no need to modify the custom list code. fields and table name are already present.

The function must returns an array of JHTML select.opion elements.

see: \models\fields\bookauthorid.php

You have to modify the SQL code from:

$query->select('`!foreignTablePK!` as `key`, `!foreignTableValue!` as `value`');
$query->from('`#__!foreignTable!`');
$query->order('`!foreignTableValue!`');

 to:

$query->select('`author_id` as `key`, `name` as `value`');
$query->from('`#__mybook_authors`');
$query->order('`name`');

Of course you have to modify also  bookeditorid.php by inserting the right references to editor_id -> name fields and #__mybook_authors table.



The end

Ok, now I think you are able to write your own more useful components.

You can also read Create components for Joomla! - Managing the front end