Forum for discussing general topics related to Couch.
6 posts Page 1 of 1
I am using the cms editable date type in my website to show the date. The idea is as follows:

1) The user enters a title
2) The user enters a date - <cms:editable name='blog_date' type='datetime' format='mdy'/> . The date is shown <p class="sub"><cms:show blog_date /></p>
3) The user enters content

The issue I am facing is:
- How to restrict the user from omitting date entry. At the moment Couch allows the user to save the page without entering the date.
- How to make only Month and Year as mandatory Fields. At the moment if I enter a month and a year and proceed to save, it throws an error to enter the Day.

Kindly help!
Apologies for the delay in my reply, Sarah.

At the moment Couch allows the user to save the page without entering the date.
Every editable region supports the 'required' attribute. Setting it to '1' will ensure the field will never remain empty -
Code: Select all
<cms:editable 
    name='blog_date'
    type='datetime'
    format='mdy'
    required='1'
/>

How to make only Month and Year as mandatory Fields. At the moment if I enter a month and a year and proceed to save, it throws an error to enter the Day.
Making only selected portions of the date mandatory, I am afraid, will require writing custom validation rules. Can be done, of course, but I think your intention in this case is just to save the user a few keystrokes. If that is so, an easier way would be to fill the field by default with some values e.g. the following will show the current date by default -
Code: Select all
<cms:editable 
    name='blog_date'
    type='datetime'
    format='mdy'
    required='1'
    default_time='@current'
/>

If the day is not important you can simply ignore its value while displaying the date.

Hope this helps.
Thank you for replying back.

If I want to ensure that the day in DateTime is not mandatory, is there any way to fetch the day part of the date through blog_date.DAY. Something like it?

Also I have specified <cms:editable name='blog_date' type='datetime' format='mdy' required='1'/>, the time labels should not appear. I am not given an option to enter the time but when i save it I see the date appear as '2010-12-05 00:00:00' although I specified the format to be 'mdy'. Please help!
Sure.
Internally, all dates in Couch are stored in yyyy-mm-dd h:m:s format. So, if you were to simply output the date entered by the user as
Code: Select all
<cms:show blog_date />
it will show something like this
Code: Select all
2014-10-02 00:00:00

That is the date in raw internal format. The date can be shown in any desired fromat by using the cms:date tag e.g.
Code: Select all
<cms:date blog_date format='jS M, Y' />
would show
Code: Select all
2nd Oct, 2014

Answering your specific question - the same cms:date can be used to fetch specific parts of the date as well. e.g.
Code: Select all
<cms:date blog_date format='j' />
would simply show '2' (i.e. the day)

Please see http://www.couchcms.com/docs/tags-reference/date.html for details on cms:date tag.

Hope this helps.
Yes this does help, thanks. The only issue now is to make the Day part of the Date as not mandatory. What do you mean by custom validations? Would that be part of the php script? How can we assist the user from omitting the Day part of the date from the CMS admin page. I believe that is not possible in couch right?
Sarah, I think a better solution in your case would be to simply *hide* the 'day' field (of course, provided we also give it a default value as we did in the previous post).

We can do that by injecting our own CSS rules into the admin-panel. This can be done using editable region of type 'message'. Following is an example that hides the 'name' field. I am sure you'll be able to adapt it to hide the 'day' field of your particular datetime instance
Code: Select all
<cms:editable name='my_css' type='message'>
    <style type="text/css">
        #k_page_name{
            display:none;
        }
    </style>
</cms:editable>

Please use firebug/chrome-tools etc. to see the classes/IDs assigned to the field - this will help you in formulatiing the CSS rules.

Hope this helps.
6 posts Page 1 of 1
cron