Skip Navigation

[Resolved] How to make a field read only on post form?

This thread is resolved. Here is a description of the problem and solution.

Problem:
The user was setting the value of the relationship field automatically, he would like to make it readonly.

Solution:
This is not available by default, we need custom Javascript code to make it readonly.

(function( $ ) {
  $('select[name="@project-prompt-to-project-submission.parent"]').attr('disabled',true);
})(jQuery);

Relevant Documentation:
https://toolset.com/course-lesson/selecting-parent-posts-when-using-forms-to-create-child-items/#creating-forms-when-a-parent-post-is-preselected

This support ticket is created 3 years, 12 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.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

Tagged: 

This topic contains 7 replies, has 2 voices.

Last updated by himanshuS 3 years, 11 months ago.

Assisted by: Jamal.

Author
Posts
#1897857

I have post form where I am passing value to a field through url param.

The field is relationship field and is populated with id of the parent post. I want user to see that field but not update it.

How can I do that on CRED forms?

#1898699

Hello and thank you for contacting the Toolset support.

You can use the attribute "readonly" of the Toolset field shortcode. For example:

[cred_field field='post_title' readonly='true' class='form-control' output='bootstrap']

Read more about the available attributes here
https://toolset.com/documentation/programmer-reference/forms/cred-shortcodes/#cred_field

#1898879

Thanks Jamal. Read only status will not impact insertion of data by a url param. Right?

Also, this is a relationship field. Are are any special considerations because of that?

#1899265

You are right, "Readonly" status will not impact the insertion of data by a URL param. It will only prevent the user from changing the value of the field.

I am not sure to understand your last question, but I think it is worth mentioning this documentation article that explains how to automatically preselect a parent post in the case of a relationship.
https://toolset.com/course-lesson/selecting-parent-posts-when-using-forms-to-create-child-items/#creating-forms-when-a-parent-post-is-preselected

#1899509

Jamal,

I made the change you suggested and field can still be modified on the front end. The field is a relationship select field.

I found this note in the documentation: "Readonly. Optional. Boolean. When set to true, the field will be read only. Read-only parameter can be applied only to text controls and not to other elements, like checkboxes or buttons. Defaults to false."

Will it not work for select dropdowns?

My code:
<div class="form-group">
<label>[cred_i18n name='@project-prompt-to-project-submission.parent-label']Project Prompt[/cred_i18n]</label>
[cred_field field='@project-prompt-to-project-submission.parent' readyonly='true' class='form-control' output='bootstrap' select_text='--- not set ---' required='true' urlparam='parent-project-prompt1-id']
</div>

#1899535

Exactly, the "readonly" attribute will not work for select fields. The remaining solution is to set it as read-only using Javascript code. Check this reply on a similar ticket https://toolset.com/forums/topic/parent-field-in-child-form-on-parent-template/#post-1136580

If this does not help, please allow me temporary access to your website. Let me know what form is it? and where it is used(URL)? And I'll see what I can do. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

#1900057

You had a slight error in the custom Javascript code. The code was using the attribute "readonly", when it should use the attribute "disabled" because the field is an <select> element.
hidden link

So, the following code is working now:

(function( $ ) {
  $('select[name="@project-prompt-to-project-submission.parent"]').attr('disabled',true);
})(jQuery);

Check this screenshot hidden link

#1900187

Thanks, Jamal. My issue is resolved now.