Programmatically update CCK List allowed values on an ajax callback attached to a dependent field

877 Views Asked by At

Hi I'm really quite stumped here. I've been trying to get this working for a while, and have just about pulled most of my hair out!..

I have a CCK list(text) select field where the options are populated using ajax based on the value of another field. The problem is, I have not set any 'allowed values' for the select field as they are populated programmatically during form completion. This causes the following problems:

  1. Can only select 1 value, even though list element is set to 'unlimited'
  2. Saving form gives illegal choice error
  3. Editing form will not set default values

I understand why most of this is happening, but not how to work around it in a clean way. I have tried updating the fields allowed values during the ajax call, but I then get "A list field ___ with existing data cannot have its keys changed."

Thanks

CCK List populate programmatically

1

There are 1 best solutions below

0
On

Ok I managed to get this working.. What I have achieved is to dynamically update the select list values of an existing CCK field based on the value of another field which contains an ajax callback to re-render the CCK field with the updated values.

Now this would have been a piece of cake if using the form API to create the form/elements from scratch, but sometimes that is not an option when working with entities and existing fields.

In short I did the following:

  1. Update field settings to use allowed_values_function
  2. Implement the custom function for populating the CCK field list
  3. Use get/set_variable to store allowed values between ajax calls
  4. Use hook_field_attach_validate to remove any 'illegal choice.' errors produced by core list.module