Dynamic Form Widget

Hello All,

I am currently working on a project where I am in need of a widget that can be populated using a model derived from an array contained in an XML RPC response.

The purpose of the project is to make a web application that can communicate with the POS system. The only API available to me is via Remote Procedure Calls to the POS system. All of the XML RPC communication has been established and tested.

The issue I am having is that I am not sure how to put the returned data into a CFormModel so that it can populate a dropdown menu or even if CFormModel is the correct structure to be using.

The response from the API that I am using looks like this:





print_r($this->api->response);




Array ( [0] => Array ( [YearID] => 36 [Year] => 2011 ) [1] => Array ( [YearID] => 35 [Year] => 2010 ) [2] => Array ( [YearID] => 34 [Year] => 2009 ) [3] => Array ( [YearID] => 33 [Year] => 2008 ) [4] => Array ( [YearID] => 32 [Year] => 2007 ) [5] => Array ( [YearID] => 31 [Year] => 2006 ) [6] => Array ( [YearID] => 30 [Year] => 2005 ) [7] => Array ( [YearID] => 29 [Year] => 2004 ) [8] => Array ( [YearID] => 23 [Year] => 2003 ) [9] => Array ( [YearID] => 22 [Year] => 2002 ) [10] => Array ( [YearID] => 21 [Year] => 2001 ) [11] => Array ( [YearID] => 20 [Year] => 2000 ) [12] => Array ( [YearID] => 19 [Year] => 1999 ) [13] => Array ( [YearID] => 18 [Year] => 1998 ) [14] => Array ( [YearID] => 17 [Year] => 1997 ) [15] => Array ( [YearID] => 16 [Year] => 1996 ) [16] => Array ( [YearID] => 15 [Year] => 1995 ) [17] => Array ( [YearID] => 14 [Year] => 1994 ) [18] => Array ( [YearID] => 13 [Year] => 1993 ) [19] => Array ( [YearID] => 12 [Year] => 1992 ) [20] => Array ( [YearID] => 11 [Year] => 1991 ) [21] => Array ( [YearID] => 10 [Year] => 1990 ) [22] => Array ( [YearID] => 9 [Year] => 1989 ) [23] => Array ( [YearID] => 8 [Year] => 1988 ) [24] => Array ( [YearID] => 7 [Year] => 1987 ) [25] => Array ( [YearID] => 6 [Year] => 1986 ) [26] => Array ( [YearID] => 5 [Year] => 1985 ) [27] => Array ( [YearID] => 4 [Year] => 1984 ) [28] => Array ( [YearID] => 3 [Year] => 1983 ) [29] => Array ( [YearID] => 2 [Year] => 1982 ) [30] => Array ( [YearID] => 1 [Year] => 1981 ) [31] => Array ( [YearID] => 28 [Year] => 1980 ) [32] => Array ( [YearID] => 27 [Year] => 1979 ) [33] => Array ( [YearID] => 26 [Year] => 1978 ) [34] => Array ( [YearID] => 25 [Year] => 1977 ) [35] => Array ( [YearID] => 24 [Year] => 1976 ) )



Can someone help me fill in the gap?

I noticed that information concerning the use of xmlrpc with Yii is quite scarce. I am willing to share my code and write up a tutorial once I get all of this figured out.

bump

So the API is there, the response works, you only want to use the response in a CFormModel? Let’s say the CFormModel has a dropdown that displays the year options (using the id as key and the year itself as value) you could use something like this:




class ApiFormModel extends CFormModel {

           

        public $yearOption;

            

        private $_years; //used to store the api response for later reference

    

        public function rules()

        {

            return array(

                array('yearOption','safe')

            );

        }

        

        protected function getYears()

        {

            if(isset($this->_years))

                return $this->_years;

            else

                return $this->_years=$this->api->response;

        }

        

        public static function getYearOptions()

        {

            $options=array();

            foreach($this->years as $yearEntry)

                foreach($yearEntry as $id=>$year)

                    $options[$id]=$year;

            

            return $options;

        }

}


//AND IN YOUR VIEW

echo CHtml::activeDropDownList($model, 'yearOption', ApiFormModel::getYearOptions());




$yearOptions is a public property used to be set by the user (via dropdown) and will hold the id of the year as returned by the API. That’s why you would have to declare it as safe via the rules method. The dropdown is populated with the array returned by the API.

Hope it helps,

Hannes