3

I need a custom field which has to select the database with some conditions. When customer A logged in this field has to select records belong to customer A only.

The custom field is as below

JFormHelper::loadFieldClass('list');

class JFormFieldGroupList extends JFormFieldList{
  protected $type='grouplist'

  public function getOptions($id=''){
    $options = array();
    //connect to database and get the records
    //Form the value and text
    return $options
  }
}

In the XML

<field name="grouplist"
  multiple="true"
  type="grouplist"          
  label="Group"/>

In the default.php

echo $this->form->renderField('grouplist',null,$default_group);

The question is I want to pass a value from default.php to the custom field so that I can put a conditional select query while fetching data from the database.

  • 1
    Instead of getting the values on the field, why don't call a method on your model to execute the query? – Pep Lainez Nov 23 '15 at 17:37
  • 1
    Pep Lainez has it correct. To do what you are trying to do you are going to have to move that to your model. In your model create a function that will query the database using JFactory::getUser()->id Loop through the results and format them as an option list, then return that option list and print it out in your default.php file between the select element tags – Terry Carter Feb 4 '16 at 18:28
1

So build it into the field logic.

JFormHelper::loadFieldClass('list');

class JFormFieldGroupList extends JFormFieldList{
    protected $type='grouplist'

    public function getOptions($id=''){
        $options = array();
        $user = JFactory::getUser();
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        // set up your query
        $query->where('user_id = '.$user->id);
        //connect to database and get the records
        //Form the value and text
        return $options
    }
}
0

Use jQuery onChange event and execute a controller task that will provide you a JSON result with data you need. If this is not a component you will need to create one. Alternatively you can try a CLI application. This is the only secure and reliable way.

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.