Date_format indicative validation

#1

Hi

The indicative validating date_format for “YYYY-MM-DD” is working well
But now when i want to validating “YYYY-MM-DD HH:mm:ss” , it’s always returns error

get rules() {
	return {
		effective_datetime: "required|date_format:YYYY-MM-DD HH:mm:ss"
    };
}

I tried to input 2019-01-02 05:00:00, but failed. Any suggestion?

1 Like

#2
  1. Please share the error message you got (you said , it’s always returns error)
  2. Did you use date or datetime to define effective_datetime in the corresponding migration file?
  3. Please check & share with us an example of an actual value of effective_datetime in the database.

I asked you the above things because, as you may have read on the documentatin, Validation will also fail, when format expects a timezone but missing in original date.

0 Likes

#3
  1. The message error is my custom error message :

     get messages() {
         return {
     	    "effective_datetime.date_format": "Effective datetime must be valid datetime format (YYYY-MM-DD HH:mm:ss)"
         };
     }
    

    So it returns that message

  2. I use datetime format in migration file

  3. The data is created without date_format validation (I turned it off temporary)
    23

0 Likes

#4

I just created a hook for the solution :

const Moment = use("moment");	

const checkDatetimeFormat = async (data, field, message, args, get) => {
	const value = get(data, field);

	if (!value) {
		return;
	}

	const valid = Moment(value, "YYYY-MM-DD HH:mm:ss", true);

	if (valid.isValid() === false) {
		throw message;
	}
};

Validator.extend("checkDatetimeFormat", checkDatetimeFormat); 

Just call it in rules like this :

 effective_datetime: "required|check_datetime_format"

Thanks, hope this can help other people facing same problem :slightly_smiling_face:

1 Like

#5

I am curious to solve your problem without using a hook. I will try to solve it this weekend and let you know if I come up with a simpler way

0 Likes

#6

I gave it the first quick try, indeed that is strange it does not work… but I will try again :slight_smile:

0 Likes

#7

Yeah, it should be working according to the documentation

‘YYYY-MM-DD HH’ is working though, but not until minutes and seconds

1 Like

#8

This smells like a bug …

0 Likes

#9

I reported this as a bug because after few more attempts and investigation, I do not see a reason why it should not work.

0 Likes

#10

Thanks, waiting for the fix

0 Likes