Skip Navigation

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

Something like this:

[cred_generic_field field='wpcf-due-date' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"1523521254",
"persist":1
}
[/cred_generic_field]
 
<input type="date" readonly value="12 April 2018">

100% of people find this useful.

This support ticket is created 6 years, 7 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 4 replies, has 2 voices.

Last updated by christianW-4 6 years, 7 months ago.

Assisted by: Nigel.

Author
Posts
#651852

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

https://toolset.com/forums/topic/make-select-field-as-readonly-in-cred/
or
https://toolset.com/forums/topic/cred-readonly-field/
do not apply because I not only want to show the field values but also submit them.

I also tried it with simple input fields. But toolset uses two fields per date (one

[display-only]

using a timestamp and one

[datepicker]

using a formatted date string).
Therefore I am not user what is expected by the toolset form processing function.

hidden link

#651936

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Christian

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.

So it should look something like this:

[cred_generic_field field='wpcf-due-date' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"1523521254",
"persist":1
}
[/cred_generic_field]
#651970

Hi Nigel

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.

[cred_generic_field field='wpcf-MY-FIELD' type='date' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"[types field='FIELDNAME' id='POSTID' format=''][/types]",
"persist":1,
"readonly":1
}
[/cred_generic_field]
#652077

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

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.

[cred_generic_field field='wpcf-due-date' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"1523521254",
"persist":1
}
[/cred_generic_field]

<input type="text" readonly value="12 April 2018">
#652084

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.