Hello all,
Another Fieldtype development query…
What is the correct way of building a fieldtype with multiple form fields so that post data doesn’t end up in my Api_channel_entries INSERT SQL?
What I mean by this is if I have a fieldtype with HTML that looks something like:
<label>Username</label>
<?=form_input($username_data);?> // let's say this one is field_id_18_username
<label>Password</label>
<?=form_input($password_data);?> // let's say this one is field_id_18_password
<label>Account Active?</label>
<?=form_dropdown($field_id.'_active', array(1 => 'Yes', 0 => 'No'), $active);?> // let's say this one is field_id_18_active
<?=form_hidden($field_name, $saved_data)?> // the actual custom field dataWhen this an entry is submitted with the above fields, I get a MySQL error:
Unknown column 'field_id_18_username' in 'field list'
INSERT INTO exp_channel_data (entry_id, channel_id, site_id, field_id_1, field_id_2, field_id_3, field_id_4, field_id_5, field_id_6, field_id_7, field_id_8, field_id_9, field_id_10, field_id_11, field_id_12, field_id_18_username, field_id_18_active, field_id_18_password, field_id_18, field_ft_1, field_ft_2, field_ft_3, field_ft_4, field_ft_5, field_ft_6, field_ft_7, field_ft_8, field_ft_9, field_ft_10, field_ft_11, field_ft_12, field_ft_18) VALUES (4, '1', '1', '', '', '', '', '', '', '', '', '', '', '', '', '123', '1', '456', 15, 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'xhtml')
Filename: libraries/api/Api_channel_entries.php
Line Number: 1808In essence, the SQL that gets generated by Api_channel_entries is based on what comes through in the HTTP POST array, not what field name has been assigned to that fieldtype for that channel.
How do I stop field_id_18_username, field_id_18_password and field_id_18_active getting included in the INSERT SQL? Or what is the best practice for creating fieldtypes with multiple form fields which don’t need to be included in the SQL INSERT/UPDATE routine?
I notice other developers (i.e. Pt_list etc.) create additional fields via Javascript. I could do this, but my natural instinct is to avoid javascript if I don’t need it. Surely I can do this all server side?
Thanks in advance.
Ah, I’ve just figured it out. Api_channel_entries does a strncmp on POST data key values for the presence of the words “field” and “field_id_”, so if you have any form fields whose names include those words, it’ll pull them into the SQL. Solution is to call the fields something else. i.e. field_id_18_username can be called crazy_chickens_18_username or whatever else instead.
Voila.
Ah yes that’s what I tried actually but it didn’t work. I guess the Codeigniter core must pull in the POST data to EE->input from which point onwards, $_POST is ignored? (for EE stuff like Api_channel_entries etc. anyway)
Well anyway, I’ll just avoid using “field” or “field_id_” for any of my extra fieldtype names.
Cheers.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.