Create Joomla! extensions - Manage the Back End- Part 2

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) &amp;&amp; ($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

Commenti   

0 #3 RODOLFO SEALES 2011-12-12 03:21
Mi congratulo in anticipo lo sviluppo di questo meraviglioso strumento per Joomla e vorrei sapere se è possibile inserire sul tuo sito un esempio completo ben spiegato, passo dopo passo, come si sviluppa un'applicazione con la MCM, a partire fine, spiegando le istruzioni e il loro effetto, come potrebbe essere esteso ad altre funzioni, ecc,,, si spera compreso l'accesso al database, uno stile di guida, potrebbe essere per un video o di forma scritta. Un buon algoritmo potrebbe essere una estensione per l'uso in classe A note scuola per soggetto, ecc ... ecc, che accede a questo database, scrivendo al database, i dati di cancellare, modicificarlo, ecc ... Apprezzo la vostra cortese attenzione a questo.
Citazione
0 #2 RODOLFO SEALES 2011-12-12 03:19
De antemano quiero felicitarlo por el desarrollo de esta maravillosa herramienta para JOOMLA y me gustaria saber si para usted es posible colocar en su sitio web un ejemplo completo muy bien explicado, paso a paso, de como se desarrolla una aplicacion con el MCM, de comienzo a final, explicando las instrucciones y su efecto, como podria ampliarse para otras funciones, etc,,,ojala incluyendo el acceso a la base de datos, a estilo de tutorial, podria ser por un video o de forma escrita. Un buen algoritmo podria ser una extension para utilizarla en la calificacion de notas de un colegio por asignaturas, etc... etc, que con esto se accede a bases de datos, escritura a la base, borrar datos, modicificarlo,, etc... Agradezco su amable atencion a la presente.
Citazione
0 #1 RODOLFO SEALES 2011-12-12 03:19
Hello Marco, how are you?
I congratulate in advance the development of this wonderful tool for Joomla and I would like to know if you can place on your website a complete example well explained, step by step, how you develop an application with the MCM, starting end, explaining the instructions and their effect, as might be extended to other functions, etc,,, hopefully including access to the database, a tutorial style, could be for a video or written form. A good algorithm could be an extension for use in rating a school notes by subject, etc ... etc., that accesses this database, writing to the database, delete data, modicificarlo, etc ... I appreciate your kind attention to this.
Citazione

Aggiungi commento

Please note: URL in text are not linked and user's site address is only for internal use and is not published.

Comments are human checked. All spam will be removed, so don't waste your time and, especially, mine!

Codice di sicurezza
Aggiorna