dependent form fields

edited September 2015 in Modules
I've built a cascading form with dependent form fields, e.g.

a selected list of manufactures
$fields['make_id'] = array('label' => 'Make', 'type' => 'select', 'options' => $options, 'first_option' => 'Select one...');

the list of products depends on the manufacturer
['model_id'] = array('type' => 'dependent', 'depends_on' => 'make_id', 'url' => fuel_url('models/ajax/options'), 'multiple' => FALSE);

Then the attributes of the product are loaded

$fields['body_type'] = array('type' => 'select',
'js' => '$(function(){
$(document).on("change", "#model_id", function(e){
var url = "'.fuel_url('body_types/ajax/options').'";
$.get(url, {model_id: $(this).val()}, function(html){
$("#body_type").html(html);
});
});
$("#model_id").trigger("change");
});
');
.....

I've specified the js function for the attributes as the dependent field type has been causing a js problem whereby the image field buttons weren't loading (no console error).

The form works find when creating products, but doesn't load the attributes when trying to edit a product.

I can't figure out where the issue is, any pointers would be gratefully received.

P.S. everything works fine when there is only one manufacturer

Comments

  • edited 10:26PM
    There were some changes made to the dependent field in the develop branch. I'm not sure if any of the updates would fix your error but if you can, can you update to the develop branch (You can use GIT to pull in and merge any changes ... may need to fix some conflicts depending on changes you've made)?
    https://github.com/daylightstudio/FUEL-CMS/tree/develop
  • edited 10:26PM
    I've updated my code base, but it hasn't fixed the issue.

    It seems that it can't handle the cascading dependency. manufacture determines products which intern determines product attributes.

    It all works fine when there is one manufacturer.
  • edited 10:26PM
    i've traced one of the issues, when creating a product I was getting a val.length issue in fuel/assets/js/fuel/custom_fields,js line 1393, I've wrapped it in if(val !== null){}

    the create function now works, edit is still and issue
  • edited 10:26PM
    Hmmm... I'm having trouble replicating this with the code provided. However, I've posted an update in the develop branch to check that there is a val first based on your fix.
  • edited 10:26PM
    cheers.

    I've got the fields working as I want them.

    In the form_fields function I pick up on the $values to determine if its edit or create. I guess there's a eloquent fuel way to do this that I've not found yet. So:

    load manufacturer list

    $fields['product_id'] = array('type' => 'dependent', 'depends_on' => 'make_id', 'url' => fuel_url('products/ajax/options'), 'multiple' => FALSE);

    if(!$values){
    $fields['body_type'] = array('type' => 'dependent', 'depends_on' => 'model_id', 'url' => fuel_url('body_types/ajax/options'), 'multiple' => FALSE);
    $fields['body_type']['order'] = "12";

    } else {

    $this->db->select('body_types.id, body_types.option');
    $this->db->join('model_body_types', 'model_body_types.option_id = body_types.id','LEFT');
    $ST2K_options = $this->db->get_where('body_types', array('model_id' => $values['model_id'], 'active' => 'yes'))->result();
    $options = array();
    foreach ($ST2K_options as $ST2K_option){
    $options[$ST2K_option->id]=$ST2K_option->option;
    }
    $fields['body_type'] = array('label' => 'Body Type', 'type' => 'select', 'options' => $options);

    }

    Thanks
Sign In or Register to comment.