[Resolved] Set a prefilled CRED date field readonly
This thread is resolved. Here is a description of the problem and solution.
Problem:
How to add a date field to a CRED form which is read only?
Solution:
The Types date field is tightly integrated with the jQuery UI datepicker extension, and cannot be made read only.
But you can add a CRED generic field that is hidden which submits the date in UNIX timestamp format, and then include a readonly input in the form to display the date in human form which is discarded when the form is submitted.
I want the user to create a new custom post type with the start and end date preset and unchangeable. Therefore I use date cred fields and preset them over the value attribute. But I am not able to set the fields readonly.
I want to solve it in a CRED form and not in PHP to prevent the need of deployments
Types date fields are stored as UNIX timestamps, and if you want to be able to, for example, filter a View of posts with these dates then that's what you need to store.
So instead of the cred_field shortcodes for the dates, replace these with hidden generic fields, where you specify the field name (including the 'wpcf-' prefix used to store Types fields in wp_postmeta) and provide the value as a timestamp.
You can insert generic fields with the Add generic fields button on the form.
Note that you will need to manually add the "persist": 1 option, which is required if you want the value of the generic field to be stored in the database.
Thanks for the answer.
It works as described.
Nice would be if I could use the field as readonly. So use type='date' and something like 'readonly'. See the following example.
You want to display the date (in human-readable format) but not allow it to be changed?
The date fields involve special handling, because the visible part with the datepicker is purely for user interaction, and the part which is actually saved in the database is in a hidden field (with timestamp format).
So it is not trivial to change this to allow for readonly behaviour.
However, you can include the timestamp in a hidden generic field as I described above, and alongside it include an input tag directly in the form to display a read only date. It would get submitted along with the form, but never processed, it would be purely a visual aid for the user.
Thanks Nigel for the support.
I'll implement it as you described above. With the little difference that i use a readonly date field instead of a text field.