We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Post data for multiple fields in custom Fieldtype

Development and Programming

Oskar Smith's avatar
Oskar Smith
33 posts
about 15 years ago
Oskar Smith's avatar Oskar Smith

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 data

When 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: 1808

In 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.

       
Oskar Smith's avatar
Oskar Smith
33 posts
about 15 years ago
Oskar Smith's avatar Oskar Smith

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.

       
Rob Sanchez's avatar
Rob Sanchez
335 posts
about 15 years ago
Rob Sanchez's avatar Rob Sanchez

You can also unset them from POST at the end of your save() method.

       
Oskar Smith's avatar
Oskar Smith
33 posts
about 15 years ago
Oskar Smith's avatar Oskar Smith

Thanks Rob. Actually before I figured the strncmp thing out I did try doing an unset() in the save() method, but it didn’t work. I must have been doing that wrong too. Out of interest, how do you unset data from $this-EE->input-post() like you describe?

       
Rob Sanchez's avatar
Rob Sanchez
335 posts
about 15 years ago
Rob Sanchez's avatar Rob Sanchez

You should be able to do:

unset($_POST['field_id_18_username']);
       
Oskar Smith's avatar
Oskar Smith
33 posts
about 15 years ago
Oskar Smith's avatar Oskar Smith

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.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.