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

∥☆過路亽.° 提交于 2019-12-10 12:50:19

问题


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.


回答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.




回答2:


You just do it using the code you gave, easy:

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



回答3:


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>



回答4:


you can try using this code. this work in mine

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

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

</fieldset>


来源:https://stackoverflow.com/questions/14197236/how-to-create-a-custom-form-field-type-in-a-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!