class XMLForm extends CFormModel
{
public $rulesArray;
/*!
* \brief Produce a php array to use to construct a html form from an xml config file
*
* \param[in] $xmlFile location of the xml config file
* \param[in] $name name of the xml node to be used
*
* \retval Array Success
* \retval null Failure
*
*
*/
public function setFormArray($xmlFile, $name)
{
# Check that the xml file can be loaded
try
{
$xml = simplexml_load_file($xmlFile);
}
catch (Exception $e)
{
return null;
}
$form['attributes']['name'] = (string)($xml->$name->name);
$form['attributes']['id'] = (string)($xml->$name->id);
# Form the input values
if(count($xml->$name->input) > 0)
{
$i=0;
foreach($xml->$name->input as $input)
{
# Every form input should have a name, id, label and type attribute
$form['inputs'][$i]['name'] = (string)$input['name'];
$form['inputs'][$i]['id'] = (string)$input['id'];
$form['inputs'][$i]['label'] = (string)$input['label'];
$form['inputs'][$i]['type'] = (string)$input['type'];
# Every input should also have a help description and a value
$form['inputs'][$i]['help'] = (string)$input->help;
$form['inputs'][$i]['value'] = (string)$input->value;
$i++;
}
}
# Form rules
if(count($xml->$name->rule) > 0)
{
$j=0;
foreach($xml->$name->rule as $rule)
{
$form['rules'][$j][0] = (string)$rule->attributeList;
$form['rules'][$j][1] = (string)$rule->validatorName;
}
}
$this->rulesArray = $form['rules'];
return $form;
}
public function rules()
{
return $this->rulesArray;
}
}
When I try and use the form with the CActiveForm widget in the view I get the following error:
‘Property “XMLForm.imccURL” is not defined.’
Which means that I should have put something like this in the XMLForm class:
class XMLForm extends CFormModel
{
public $imccURL;
BUT I want this to be generic - is there anyway I can declare a property for the XMLForm class from the setFormArray() fucntion?
Or is there any other way people can think of to do it?
I’m still getting the same error though - have I done something daft here? My XMLForm class now reads:
class XMLForm extends CFormModel
{
public $rulesArray;
protected $properties=array();
public function __get($name)
{
if(isset($this->properties[$name]))
{
return $this->properties[$name];
}
return parent::__get($name);
}
public function __set($name,$value)
{
if(isset($this->properties[$name]))
{
return $this->properties[$name]=$value;
}
return parent::__set($name,$value);
}
/*!
* \brief Produce a php array to use to construct a html form from an xml config file
*
* \param[in] $xmlFile location of the xml config file
* \param[in] $name name of the xml node to be used
*
* \retval Array Success
* \retval null Failure
*
*
*/
public function setFormArray($xmlFile, $name)
{
# Check that the xml file can be loaded
try
{
$xml = simplexml_load_file($xmlFile);
}
catch (Exception $e)
{
return null;
}
$form['attributes']['name'] = (string)($xml->$name->name);
$form['attributes']['id'] = (string)($xml->$name->id);
# Form the input values
if(count($xml->$name->input) > 0)
{
$i=0;
foreach($xml->$name->input as $input)
{
# Every form input should have a name, id, label and type attribute
$form['inputs'][$i]['name'] = (string)$input['name'];
$form['inputs'][$i]['id'] = (string)$input['id'];
$form['inputs'][$i]['label'] = (string)$input['label'];
$form['inputs'][$i]['type'] = (string)$input['type'];
# Every input should also have a help description and a value
$form['inputs'][$i]['help'] = (string)$input->help;
$form['inputs'][$i]['value'] = (string)$input->value;
$i++;
}
}
# Form rules
if(count($xml->$name->rule) > 0)
{
$j=0;
foreach($xml->$name->rule as $rule)
{
$form['rules'][$j][0] = (string)$rule->attributeList;
$form['rules'][$j][1] = (string)$rule->validatorName;
}
}
$this->rulesArray = $form['rules'];
return $form;
}
public function rules()
{
return $this->rulesArray;
}
}
And the services action is just the same:
public function actionServices()
{
$model = new XMLForm;
# create the php array for the form
$this->formArray = $model->setFormArray(Yii::app()->params['xmlConfig'], 'services');
// renders the view file 'protected/admin/views/settings/services.php'
// using the default layout 'protected/admin/views/layouts/main.php'
$this->render('services',array('model'=>$model));
}
Hi there, I am newbie and I am trying to do some project for school having the same purpose as yours and I cannot understand how does all these classes relate to each other. For example what are you configuring in parameter ‘xmlConfig’ ? Can you share your solution ?