4

I have an XML file like

<?xml version="1.0" encoding="UTF-8"?>
<form>
     <field name="age" type="list"
        default="1"
        description="age"
        label="Age"
        size="1"
    >
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>

    </field>
</form>

I need to repeat the option for a fixed number of times. How can I write a loop to repeat the option? Is it possible to write a loop?

3 Answers 3

7

Currently you can perfectly use Integer form field.

The integer form field type provides a select box with a range of integer values.

<field 
  name="age" 
  type="integer" 
  default="1" 
  label="Age" 
  description="age" 
  first="1" 
  last="100" 
  step="1" 
/>

first would be minimum age and last would be maximum age.

ALTERNATIVE

Another option would be to create custom form field. This gives you more flexibility if you want to extend your field.

administrator/components/[name of your component]/models/fields/Age.php

<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

jimport('joomla.form.formfield');

class JFormFieldAge extends JFormField {

  protected $type = 'Age';

  // getLabel() left out, no need to overwrite it (at least for this example)

  public function getInput() {

    // Define min and max ages, if not set by field, defaults to 1 and 100.
    $minAge = isset($this->element['min']) ? $this->element['min'] : 1;
    $maxAge = isset($this->element['max']) ? $this->element['max'] : 100;

    $html = '<select id="'.$this->id.'" name="'.$this->name.'">';

    // Loop
    for ($i = $minAge; $i <= $maxAge; $i++) { 
      $html .= '<option value="'.$i.'">'.$i.'</option>';
    }

    $html .= '</select>';

    return $html;
  }
}

Load your fields in your extension main file.

JForm::addFieldPath(JPATH_COMPONENT . '/models/fields'); // Change the path for your needs

XML

In addition, you may need to add the field path to the parent :

<fieldset addfieldpath="/administrator/components/<component name>/models/fields">

Again, change the path for your needs.

And finally, use your custom field type

<field 
  name="age"
  type="Age"
  description="age"
  label="Age"
  min="18"
  max="90"
  size="1" />

Read more about creating a custom form field type.

3
  • I am getting an error JForm::getInstance could not load file. what may be the reason?
    – aish
    May 8, 2015 at 5:44
  • Check your XML file for errors. Is it valid XML? Maybe you have typo.
    – Rene Korss
    May 8, 2015 at 5:47
  • thank you for your quick response. there was an error in my xml file.
    – aish
    May 8, 2015 at 5:56
1

Although Rene's solution is much more elegant, I'd like to add another alternative. Instead of creating a loop inside your XML file (which I doubt is possible), you can generate the XML text using any other programming language, like PHP, and then simply copy/paste the content into your XML file.

Here's an example for your Age field:

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>XML Loop</title>
</head>
<body>
<textarea rows="50" cols="100">
<field name="age" type="list"
        default="1"
        description="age"
        label="Age"
        size="1"
    >
<?php 
    for ($x = 1; $x <= 100; $x++) { // Change values according to your desired range
        echo "        <option value=\"$x\">$x</option> \n";
    }
?>
</field>
</textarea>
</body>
</html>

This will output the <option> tag with values from 1 to 100, inside a text-area. The code can then be copied into your XML file.

Maybe not that elegant, but it definitely works...

-2

It will repeat the option in fixed number of times

 <?xml version="1.0" encoding="UTF-8"?>
    <form>
         <field name="age" type="list"
            default="1"
            description="age"
            label="Age"
            size="1"
        >
    <?php
    for($i=1; $i<=100;$i++)
    {
    echo " <option value="$i">$i</option> ";
    } 
    ?>
4
  • Did you test your code before posting it? PHP code won't work in Joomla manifest files. In addition, PHP variables has to begin with $.
    – johanpw
    Jun 25, 2015 at 14:19
  • It's my mistake.. i replace the coding...
    – Jextn
    Jun 26, 2015 at 4:23
  • It still looks strange with PHP code in the XML file, I'd be interested to know if this really works (I only get an empty select box). Can you confirm if it works?
    – johanpw
    Jun 30, 2015 at 17:24
  • This requires editing server configuration to treat XML file as PHP file.
    – Rene Korss
    Apr 1, 2016 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.