cancel
Showing results for 
Search instead for 
Did you mean: 

getting the valid date in datetime datatype

abhashree
Champ in-the-making
Champ in-the-making
Hi All,

I have a custom content model.
I have written a constraint having datatype "datetime".
But the problem is in this datatype someone can give future date .
My requirement is to validate the date so nobody can give any future date.
Is it possible to get the system date and time while setting the time in that field.
Please guide me how can I achieve this.


Thanks in advance,
Abhashree
2 REPLIES 2

jpotts
World-Class Innovator
World-Class Innovator
Have you considered writing your own custom constraint? Constraints are just Java classes. So your class could look at the date provided and compare it to today and then return false if the date is greater than today's date.

This will take care of it for the back-end. If you want it validated in real-time in the Share UI you'll have to write your own validator and then use form config to tell Share to use it. You could start with the existing date validator and extend it to compare it to today's date.

Jeff

hemantkpn
Champ in-the-making
Champ in-the-making
Hi,

You can implement your own Date validator constraint class by extending it with org.alfresco.repo.dictionary.constraint.AbstractConstraint.
You just need to override a method protected void evaluateSingleValue(Object value).

Just type cast value parameter to java.util.Date and you can compare as you want.
Post validation, you can return an appropriate message by throwing ConstraintException which takes message and value as parameters in constraint.

Following is sample code for you:

   protected void evaluateSingleValue(Object value)
    {
        final String ERR_INVALID_DATE = "d_dictionary.constraint.my_date.invalid_date";
        // ensure that the value can be converted to a String
        Date checkValue = (Date) value;
        if(checkValue.before(new Date())) {
           throw new ConstraintException(ERR_INVALID_DATE, value);
        }
    }