简体   繁体   中英

how to create select box in registration form in joomla

how to create select box in registration form in joomla.

I have try to add birthyear in my registration form using dynamic.

option value from 1950 to current year.how to add the year using loops or dynamic code.

Here is my code.

<field name="joinyear" type="list"
        description="COM_USERS_REGISTER_JOINYEAR_LABEL"
        filter="string"
        label="COM_USERS_REGISTER_JOINYEAR_DESC"
        message="COM_USERS_REGISTER_JOINYEAR_DESC"
        required="true">
    <option value="2013">2013</option>
    <option value="2012">2012</option>
    <option value="2011">2011</option>
    .
    .
    .
    .
    .
    <option value="1950">1950</option>
</field>

Actually I'd recommend you to create your own list type: create a directory /models/fields/year.php which will extend JFormFieldList class defined in /libraries/joomla/form/fields/list.php

And add there the following code:

<?php
defined('JPATH_PLATFORM') or die;

JFormHelper::loadFieldClass('list');

class JFormFieldYear extends JFormFieldList
{
    protected $type = 'Year';

    protected function getOptions()
    {
        $options = array();         
        $yearNow = date('Y');

        for ($i = 1950; $i <= $yearNow; $i++)
        {
            $tmp = JHtml::_('select.option', $i, $i, 'value', 'text', false);
            $options[] = $tmp;
        }

        reset($options);

        return $options;
    }
}

And then in your xml type define type="year" instead of type="list" for a field like this:

<field name="joinyear" type="year"
        description="COM_USERS_REGISTER_JOINYEAR_LABEL"
        filter="string"
        label="COM_USERS_REGISTER_JOINYEAR_DESC"
        message="COM_USERS_REGISTER_JOINYEAR_DESC"
        required="true" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM