2

I am attempting to modify code that was created by Component Creator. The modification I am trying to make is to get the select list to save the partno instead of the index value.

There are two important notes: 1. Component Creator assumes all tables are in the same Joomla databse so it created the code accordingly. 2. I have created a custom field type that connects to the other database and selects the data from the table.

It all appears to work fine and the form saves the data to the table on submit. The drop down list displays my part numbers fine and allows me to select the one I want. The problem is that when I view the data in phpMyAdmin the table field partno has an integer which appears to be the index of the array behind the list (not the record ID in the table).

I can see in the ./site/views/[formname]/tmpl/default.php where the field is being rendered. I am not sure how to modify this to get the actual partno instead of the index to be saved.

<?php echo $this->form->renderField('partno'); ?>

<?php foreach((array)$this->item->partno as $value): ?>
    <?php var_dump($value); ?>
    <?php if(!is_array($value)): ?>
        <input type="hidden" class="partno" name="jform[partnohidden][<?php echo $value; ?>]" value="<?php echo $value; ?>" />';
    <?php endif; ?>
<?php endforeach; ?>

Any help would be greatly appreciated!

Additional info:

generated HTML for list

<div class="control-group">
  <div class="control-label">
    <label id="jform_partno-lbl" for="jform_partno">
    Partno</label>
  </div>
  <div class="controls">
    <select id="jform_partno" name="jform[partno]" style="display: none;">
     <option value="0">              for [1/2 FPT SCH</option>
     <option value="1">              for [3/4 FPT SCH</option>
     <option value="2">         -&gt; [GALLOUP3140777 3/</option>
     <option value="3">         -&gt; [GALLOUP3206216 1/</option>
     <option value="4">       (B) DYN-AVI DA1246023-1</option>
     <option value="5">       and (C) DYN-AVI DA12460</option>
     <option value="6">   1. NO REDUCTION IN TUBING D</option>
     <option value="7">   2. NO BURRS ALLOWED ON THE </option>
     <option value="8">   3. HOLES MAY BE DRILLED PRI</option>
     <option value="9">   4. HOLES ARE 90 Degrees +/-</option>
     <option value="10">   5. HELIX PITCH IS 163 +/- 2</option>
    </select>
    <div class="chzn-container chzn-container-single" style="width: 220px;" title="" id="jform_partno_chzn">
      <a class="chzn-single">
        <span>              for [1/2 FPT SCH</span>
        <div><b></b></div>
      </a>
      <div class="chzn-drop">
        <div class="chzn-search">
           <input autocomplete="off" type="text">
        </div>
        <ul class="chzn-results"></ul>
      </div>
    </div>
  </div>
</div>

Here are two additional files that have been requested:

./site/models/forms/taskform.xml

<form>
  <fieldset>
    <field name="id" type="text" readonly="true" class="readonly" default="0" description="JGLOBAL_FIELD_ID_DESC"/>
    <field name="task" required="true" message="Please enter a task." label="COM_TODO_FORM_LBL_TASK_TASK" description="COM_TODO_FORM_DESC_TASK_TASK" hint="COM_TODO_FORM_LBL_TASK_TASK"/>
    <field name="description" cols="80" rows="6" filter="safehtml" type="textarea" label="COM_TODO_FORM_LBL_TASK_DESCRIPTION" description="COM_TODO_FORM_DESC_TASK_DESCRIPTION" hint="COM_TODO_FORM_LBL_TASK_DESCRIPTION"/>
    <field name="due_date" class="inputbox" format="%d-%m-%Y" timeformat="12" filter="server_utc" minyear="-1" showtime="false" singleheader="true" todaybutton="false" weeknumbers="false" filltable="false" type="calendar" label="COM_TODO_FORM_LBL_TASK_DUE_DATE" description="COM_TODO_FORM_DESC_TASK_DUE_DATE" hint="COM_TODO_FORM_LBL_TASK_DUE_DATE"/>
    <field name="task_status" type="foreignkey" label="COM_TODO_FORM_LBL_TASK_TASK_STATUS" description="COM_TODO_FORM_DESC_TASK_TASK_STATUS" class="inputbox" input_type="list" table="#__todo_status" key_field="id" value_field="task_status" required="true" default=""/>
    <field name="partno" key_field="partno" value_field="partno" type="e2part" label="COM_TODO_FORM_LBL_TASK_PARTNO" description="COM_TODO_FORM_DESC_TASK_PARTNO" hint="COM_TODO_FORM_LBL_TASK_PARTNO"/>
    <field name="assigned_to" query="SELECT * FROM #__contact_details WHERE published = 1 ORDER BY name" key_field="id" value_field="name" type="sql" label="COM_TODO_FORM_LBL_TASK_ASSIGNED_TO" description="COM_TODO_FORM_DESC_TASK_ASSIGNED_TO" hint="COM_TODO_FORM_LBL_TASK_ASSIGNED_TO"/>
    <field name="created" type="timecreated" label="COM_TODO_FORM_LBL_TASK_CREATED" description="COM_TODO_FORM_DESC_TASK_CREATED" hidden="true" hint="COM_TODO_FORM_LBL_TASK_CREATED"/>
    <field name="modified" type="timecreated" label="COM_TODO_FORM_LBL_TASK_MODIFIED" description="COM_TODO_FORM_DESC_TASK_MODIFIED" hidden="true" hint="COM_TODO_FORM_LBL_TASK_MODIFIED"/>
    <field name="version_note" type="text" label="JGLOBAL_FIELD_VERSItoON_NOTE_LABEL" description="JGLOBAL_FIELD_VERSION_NOTE_DESC" class="inputbox" size="45" labelclass="control-label"/>
  </fieldset>
  <fieldset name="accesscontrol">
    <field name="asset_id" type="hidden" filter="unset"/>
    <field name="rules" type="rules" label="JFIELD_RULES_LABEL" translate_label="false" filter="rules" validate="rules" class="inputbox" component="com_todo" section="task"/>
  </fieldset>
</form>

./site/models/fields/e2part.php

<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

JFormHelper::loadFieldClass('list');

class JFormFieldE2Part extends JFormFieldList {

    protected $type = 'e2part';

    public function getOptions() {
                $app = JFactory::getApplication();
                $partno = $app->input->get('partno'); // partno is the dynamic value which is being used in the view

                $servername = "localhost";
                $username = "someuser";
                $password="somepassword";
                $dbname = "bmp";
                $table = "estim";

                $db= new mysqli($servername, $username, $password, $dbname);

                if ($db->connect_error){
                    die("E2 Part (Estim) Connection failed: " . $conn_error);
                }

                $query  = "SELECT a.PartNo FROM " . $table . " AS a ";
                $query .= "WHERE a.PartNo > '' ";
                $query .= "ORDER BY PartNo";
                $result = mysqli_query($db, $query);


                if($result){
                    //output parts
                    while($row = mysqli_fetch_assoc($result)) { 
                        $parts[] = $row["PartNo"];
                    }
                }else {
                    die("E2 Part (Estim) Query failed: ");
                }


                // Merge any additional options in the XML definition.
                $options = array_merge(parent::getOptions(), $parts);
                return $options;
    }
}
3

raVan96 is bang on, you should have either a sql field or a list field. But, what you have was an sql field that you have replaced with a custom list field. There are two main issues.

The form xml field attributes

key_field and value_field only apply to sql fields, not to list fields. Hence, they are doing nothing here and you should remove them.

<field name="partno" key_field="partno" value_field="partno" type="e2part" label="COM_TODO_FORM_LBL_TASK_PARTNO" description="COM_TODO_FORM_DESC_TASK_PARTNO" hint="COM_TODO_FORM_LBL_TASK_PARTNO"/>

The custom list field

You've copied the example from the documentation, but that example is not very generic and does things which don't apply to you. I've put comments inline and removed the unneeded bits.

<?php
defined('_JEXEC') or die('Restricted access');

JFormHelper::loadFieldClass('list');

class JFormFieldE2Part extends JFormFieldList {

    protected $type = 'e2part';

    public function getOptions() {
        $servername = "localhost";
        $username = "someuser";
        $password="somepassword";
        $dbname = "bmp";
        $table = "estim";

        $db= new mysqli($servername, $username, $password, $dbname);

        if ($db->connect_error){
            die("E2 Part (Estim) Connection failed: " . $conn_error);
        }

        // Get the two columns for the value and text of the option,
        // but in your case it's just one column!
        $query  = "SELECT PartNo FROM " . $table;
        $query .= " WHERE PartNo > ''";
        $query .= " ORDER BY PartNo";
        $result = mysqli_query($db, $query);

        // Build the options as $options[value] = text
        $parts = [];
        if($result){
            while($row = mysqli_fetch_assoc($result)) { 
                $parts[$row["PartNo"]] = $row["PartNo"];
            }
        }else {
            die("E2 Part (Estim) Query failed: ");
        }
        return $parts;
    }
}
3

You can check the field from where it is coming,

<? php echo $this->form->renderField('partno'); ?>

This line rendering your field 'partno', by looking into generated html it seems it is jform , So you need to go to "site/components/[component_name]/models/forms/form_name.xml file" check the field name as 'partno' it might be sql field or list field

  • If it is sql field check the 'key_field' attribute and 'value_field', So it might have as below key_field="id" so you need to update this line to use value whatever you want to save when form submits.
  • Thanks for your input! I have added the form.xml and the field.php files above to provide more details. As you can see the field type is e2part with the key_field and value_field set to partno not id or some other number. I am still baffled by this. The table's primary key is the partno (not ID) and it looks like the option value is being set to the position of the option in the drop down list. – maleman Apr 12 '18 at 14:23
  • Cool so by looking into the above files and the answer given by @jamesgarrett your problem should solved , Not sure did you tried or not ? Let us know is it worked for you or not , Also try to write queries using the Joomla functions :) – Sudhir Sapkal Apr 13 '18 at 5:49
  • 1
    The solution by @jamesgarrett worked flawlessly! – maleman Apr 13 '18 at 14:49
  • 1
    Great.. :) Up vote for @jamesgarrett – Sudhir Sapkal Apr 16 '18 at 6:35

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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