How to hide mandatory form fields in edit mode?

By: Ralph Nohe | Asked: 08/24/2024
ForumsCategory: General questionsHow to hide mandatory form fields in edit mode?
Ralph Nohe asked 3 months ago
I have a form which is used to create, but on the other hand also to edit entries. The form contains fields which are mandatory, i.e. payment fields with credit card data, but which I would like to hide entirely when the form is being used to edit an existing entry. I have also an credit card payment action defined (in settings) which pulls data from the payment data fields, and I would also like to skip this connected action, of course. What is the best practice to accomplish this?  Thanks! Ralph
Question Tags:
3 Answers
Rob LeVineRob LeVine answered 3 months ago
For hiding fields when editing, here's my suggestion: Make sure you have a userid field in your form and set the conditional logic on all the fields you want to hide so that it's hidden when userid doesn't equal [blank]. You can do the same sort of thing with the payment action by going into the action and clicking the "Add Conditional Logic" link and setting up the same logic as previously mentioned. Alternatively, you can do it using jQuery and key off the form's mode.
Ralph Nohe answered 3 months ago

Hi Rob, the user filling the form, be it when creating the post or later when editing the post, is always logged in. So that doesn’t work. What I thought should work is using the url parameter „entry“ which doesn’t exist if the form is used to create a post. When editing, it contains the id of the formidable forms entry, eg „entry=54“.I tried to use this parameter being pulled as default value into a hidden field using get param. But when using it in a visible text field for testing purposes, it shows a blank although the parameter exists. I find this strange!

Separately, I found a hook function to make certain fields non-editable when in edit mode. But that doesn’t help either for the credit card data which isn’t stored on my site. Isn’t there any hook to HIDE fields when in editing mode? This might be the easiest solution. Otherwise, perhaps there is an alternative approach for my url parameter idea outlined above? Or something completely different? Thank you! Best regards, Ralph  

Ralph Nohe answered 3 months ago

Hi Rob,  a quick update: I made it work!  I added the following javascript (jQuery) in the HTML editor ("after the fields" section). In case the URL parameter "entry" exists, it hides the fields, in the example field id 255. It also changes the header, but thats just the cherry on top of it. An interesting detail with regards to the URl parameters: when in editing mode, it seems that the shortcode [get param=XYZ] in the default field value settings doesn't work. This only works when using the form in "entry mode". Best regards, Ralph  

<script type="text/javascript">
jQuery(document).ready(function($){
// Function to get URL parameters
function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
}
// Check if "entry" parameter exists
if (getUrlParameter('entry')) {
// Hide fields if "entry" parameter exists
$("#frm_field_255_container").css('display','none');
$('h1').text('EDIT Listing');
document.title = 'Anzeige bearbeiten';
$('meta[property="og:title"]').attr('content', 'EDIT Listing');
}
});
</script>

 

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right