This support ticket is created 2 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
If a user simply removes an image URL from an image field in a CRED edit form by clicking on the red cross, when the form is submitted I had expected the field would be updated to empty but the actual field is being removed from the post in the database.
Yes this is the expected behaviour as the record being removed is equivalent to an empty field. Meaning if you check the post on the backend you will see that the field is empty, this means there isn't any record in the database for that field for that specific post.
I can understand this being the behaviour for a repeating image field but for single image fields, this doesn't make sense.
If a user creates a post without uploading an image, the field is added to the database as an empty field. It is illogical to delete the entire meta entry if having uploaded an image to the field at some point, the user then later removes that image without replacing it with another.
Single Line fields, for example, remain in the database even if they've been updated to empty; why should image fields be any different??
I need to execute code in CRED hooks according to the field content on submission of the edit form. So if the image has been removed & not replaced, A happens, if a new image has been uploaded, B happens. It doesn't matter though how I check for the existence of or content of the image field (using empty, ! or ''), the conditions are ignored and both bits of code are executed triggering errors for the code that shouldn't run.
I can't even use
$variable = $_POST['wpcf-custom-field-name']
in the before save hook to capture the "old" field content. I'm able to do this is in scenarios that use other field types but not images. It's nonsense and inconsistent.
It seems the only way round this is to present the user with one form for removing without replacing and another for replacing or adding but that's just messy.
Thanks for the additional information. Are you uploading the images using the media library ?
Perhaps you can set a default value.
Single Line fields, for example, remain in the database even if they've been updated to empty; why should image fields be any different??
Can you let me know the exact use case scenario so that I can try to replicate? Perhaps you can setup an example for me on our sandbox site so that I can go through to see exactly what happens.
I don't understand what it is you want to replicate. You've already confirmed that deletion of the field from the database is the expected behaviour when an image field is updated to empty. I am saying this behaviour is illogical and I've given good reasons for my opinion.
I think this should be referred to the developers with a view to changing the current behaviour so that the field is not removed from the database when updated to empty.
Capturing the old state of a field before the changes are saved is NOT a new feature; this is something that can be done with any other type of CRED field, just not image fields, using the before save hook (as I stated I'm using in my opening thread) and it shouldn't require the use of Javascript.
The whole problem is due to the image field being removed from the database. No other empty fields are removed from the database when a form is submitted and actually an empty image field is added to the database when a create CRED form is submitted so it's not unreasonable to expect empty fields to stay there. It's the behaviour of the edit form that's wrong.
All code is based on logic and consistency. There is NO logic or consistency here and it needs referring to the developers for change.
Also looking at your code again have you tried using the isset() function ?
For example
if(isset($_POST['wpcf-custom-field-name']))
This function checks if the field actually exist. This should work in the case where the field is filled out.
For the field not being fill out or the value removed you can do this.