0

I'm developing my custom component for storing some records in the DB. In my #__records DB table there is the column attribs where I want to store some data in json-format like:

{"field1":"value1","field2":"value2"}

I try to add the input for the field1 in my admin/models/forms/record.xml like:

<fields name="attribs">
  <fieldset name="basic">
     <field name="field1" type="text" />
  </fieldset>
</fields>

and I can render it in my admin/views/record/tmpl/default.php via foreach:

<?php foreach($this->form->getFieldset() as $field): ?>
    <?php echo $field->input; ?>
<?php endforeach; ?>

Although at that the field1 hasn't its value that is already stored in the DB.

There are two questions:

1. How can I render the input in views/record/tmpl/default.php not via foreach but like:

echo $this->form->getInput('field1'); // it doesn't display the input at all

and with its stored value

2. How can I save new value of the field1 i.e. what code should I add to the save($data) function in my admin/models/record.php (or to JTable class)? I need something like:

public function save($data)
{
   if(isset($data['attribs']) && isset($data['attribs']['field1']))
   {
      // what code should be here ???
   }
   return parent::save($data);
}
1

Please see following answers to your question:

You can render individual field as follows:

echo $this->form->getField('field1')->label;
echo $this->form->getField('field1')->input;

At this time you would not have a value to above input field. If you want to give default value to your field1 then you can do following in your model:

Make sure that you are extending your model with JModelForm or JModelAdmin

override getForm method

public function getForm($data = array(), $loadData = true)
{
        $form = $this->loadForm('com_yourcomponent.formname', 'formname', array('control' => 'jform', 'load_data' => $loadData));

  if (empty($form))
  {
      return false;
  }            

  $form->setFieldAttribute('field1', 'default', 'Your Value');

  return $form;
}

If you want to get value in runtime them do following:

In your save($data) method of Model

public function save($data)
{
   // Get the value of field1 with it's name. Make sure to check if value exists.
   $field1 = $data['field1'];
   $field2 = $data['field2'];

   // Merge both field's value to one Array
   $attribs = array('field1' => $field1, 'field2' => $field2);

   // Convert it to JSON object
   $data['attribs'] = json_encode($attribs);
   return parent::save($data);
}
  • If I use echo $this->form->getField('field1')->input; I get the error: Trying to get property of non-object in ... and any value is not being saved in the DB with your code for the model – stckvrw May 20 '15 at 17:14
  • api.joomla.org/cms-3/classes/JForm.html Look at this API it says getField method is correct. Looks like your form is not loading that field. Try to debug your code by doing print_r($this->form) and see if you can get your field there. – Sahil Purav May 20 '15 at 17:16
  • Oh, you missed $group string — there should be: echo $this->form->getField('field1','attribs')->input; Now I get the field. However it still hasn't its value that is already stored in the DB and has default value from XML-form – stckvrw May 20 '15 at 17:34
1

Ok, yet I resolved the first issue with the code:

$json = $this->item->attribs;
$obj = json_decode($json);
$value1 = $obj->{"field1"};
echo $this->form->getField('field1','attribs')->label;
echo '<input type="text" name="jform[attribs][field1]" id="jform_attribs_field1" value="'.$value1.'" class="inputbox" size="40" />';

and to the save($data) function in the model I've added only the code:

$attribs = array();
$attribs['field1'] = $data['attribs']['field1'];
$data['attribs'] = json_encode($attribs);

There is no need to add something to getForm method

Now everything works perfectly but any better solution would be appreciated

  • getForm method is optional to override. Glad you solved this issue! Accept your answer whenever you are allowed to :-) – Sahil Purav May 20 '15 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.