I'm trying to write a plugin which will add a certain select-type custom field to a ticket. The difference from a regular custom-field of a select type is that this field will get its values from a database and create a select with optgroups.
I create a custom select field via trac config file and modify it with Transformer
The code goes like this:
db = self.env.get_db_cnx()
cursor = db.cursor()
cursor.execute("SELECT name, a_id FROM a_group")
groups = cursor.fetchall()
cursor.execute("SELECT id, name FROM activities")
activities = cursor.fetchall()
for activity in activities:
stream = stream | Transformer('.//select[@id="field-activity"]').append(tag.optgroup(label=activity[1], id="act-"+str(activity[0])))
for group in groups:
if int(group[1]) == activity[0]:
stream = stream | Transformer('.//optgroup[@id="act-' + str(activity[0]) + '"]').append(tag.option(group[0]))
The problem is: when I'm trying to save a newticket, I get an error:
Warning: <field_name_goes_here> is not a valid value for the activity field.
Which is due to the fact, that while I'm using custom field via trac custom-fields functionality - I do not provide any options via trac config file.
The question is - what the best (if any) way to implement this kind of feature?
The error comes from '_validate_ticket' in trac.ticket.web_ui. As stated in the source comment header it will "Always validate for known values" of option fields.
Since you already mangle the custom field appearance, it might be worth trying to alter its input field type as well. The idea is to pretend it is a simple (text) input field, that will accept any value without validation, you see?
Other approaches would involve messing with Trac core code, what is not a bright idea, especially if you plan to follow upstream code in general for the years to come.