How to create a custom form field type in a module?

10,421

Solution 1

You forgot to specify models in the addfieldpath

<fields name="params">
   <fieldset name="basic" addfieldpath="/modules/mod_royalslider/models/fields">

       <field name="title" type="City" label="anythging" description=""   />

   </fieldset>
</fields>

Just put the fields folder inside the models folder and put the path like above.

Solution 2

Use addfieldpath within fields as like ::

<fields name="params" addfieldpath="/modules/mod_royalslider/models/fields" >

       <fieldset name="basic">

         <field name="title" type="City" label="anything" description="" />

       </fieldset>
 </fields>
Share:
10,421
Mansour Alnasser
Author by

Mansour Alnasser

App and Web Developer: self-learning #PHP/#Javascript/#Angular Travelling and gaming

Updated on June 28, 2022

Comments

  • Mansour Alnasser
    Mansour Alnasser almost 2 years

    Im useing joomla 2.5, and I want to create a custom form field type that stored in the same module.

    In the XML:

     <fieldset  name="basic" addfieldpath="/modules/mod_royalslider/fields"></fields>
          <fieldset name="basic">
               <field name="title" type="City" label="anythging" description=""   />
          </fieldset>
     </fields>
    

    In the file /modules/mod_royalslider/fields/city.php

    <?php
    // Check to ensure this file is included in Joomla!
    defined('_JEXEC') or die('Restricted access');
    
    jimport('joomla.form.formfield');
    
    class JFormFieldCity extends JFormField {
    
        protected $type = 'City';
    
        // getLabel() left out
    
        public function getInput() {
                return '<select id="'.$this->id.'" name="'.$this->name.'">'.
                       '<option value="1" >New York</option>'.
                       '<option value="2" >Chicago</option>'.
                       '<option value="3" >San Francisco</option>'.
                       '</select>';
        }
    }
    

    for now it show me error, when ever i remove <fieldset name="basic" addfieldpath="/modules/mod_royalslider/fields"></fields>

    the error gone and the field disply as a text field.

  • Mansour Alnasser
    Mansour Alnasser over 11 years
    Not worked. However, it's a path at the end I moved the php file to the same path. Do any body has an open source files!