aurelia / validation

A validation plugin for Aurelia.
MIT License
132 stars 128 forks source link

How to define and use Date() #260

Closed huw489 closed 8 years ago

huw489 commented 8 years ago

Hi, apologies if this is in the wrong place, but could somebody please explain how to set up Date(). I'm hitting the "Both the parse and format functions needs to be set to use the datetime/date validator", but I'm fairly new to all of this and don't know where to set these. Thanks in advance.

y2k4life commented 8 years ago

There needs to be some clarification as to where and what. This should be part of aurelia-validatejs https://github.com/aurelia/validatejs. There are three components to validation, there is validation, aurelia-validatejs and validate.js. What is doing the actual validation? It is validate.js, therefore you will need to look at their documentation. http://validatejs.org/. I had to use the equality, I reviewed the documentation and pieced this together.

            .ensure('confirmPassword').required().equality({
                attribute: 'password',
                comparator: function (v1, v2) {
                    return v1 === v2;
                }
            })

Hope that helps.

huw489 commented 8 years ago

Hi, thanks. I did look at the documentation at http://validatejs.org/ and I found some code: `// Before using it we must add the parse and format functions // Here is a sample implementation using moment.js

validate.extend(validate.validators.datetime, { // The value is guaranteed not to be null or undefined but otherwise it // could be anything. parse: function(value, options) { return +moment.utc(value); }, // Input is a unix timestamp format: function(value, options) { var format = options.dateOnly ? "YYYY-MM-DD" : "YYYY-MM-DD hh:mm:ss"; return moment.utc(value).format(format); } });`

What i'm struggling with is where do I add this type of code in Aurelia?

y2k4life commented 8 years ago

Yes and I was testing date and that one unlike the equality does not take the function for format and parse in the configuration. The only thing I can think of is that aurelia-validatejs would need a method to set this up with a call back to a function you provide.

If I was to take a stab at it something like this in the aurelia-validatejs

    dataParse(parse) {
        validate.extend(validate.validators.datetime, {
            parse: function(value, options) {
                parse(value, options);
            }
        });
    });

On a side note because of not know what is what and where. This is actually a aurelia-validatejs issue and not a aurelia-validation issue.

huw489 commented 8 years ago

Thanks @y2k4life I've asked the same question over at the aurelia-validatejs and so closing the topic here.