Search code examples
node.jsdatehandlebars.jshapi.js

Using inputtype date with handlebars --> hapi - handler


Is there a way to send the selected date with

<input type="date" value="{{moment date=d format='YYYY-MM-DD'}}"/> 

to the handler using hapi/ handlebars? i'm trying to use two datepickers to define when to get records I view.

ex. https://someadress.com/applicants/from/timestamp(from)/to/timestamp(to)


Solution

  • If you're trying to fill in the datepicker values with the parameters in the url, you can set up a route like this, making sure to pass the dates in your context object.

    const joi = require('joi');
    
    const yourRoute = {
        method: 'GET',
        path: '/applicants/from/{datefrom}/to/{dateto}',
        config: {
            validate: {
                params: {
                    datefrom: joi.date(),
                    dateto: joi.date()
                }
            }
        },
        handler: (request, reply) => {
            let context = {
                datefrom: request.params.datefrom,
                dateto: request.params.dateto
            };
    
            reply.view('path/to/view', context);
        }
    };
    

    And then in your template, you only need to set datefrom and dateto:

    <input type="date" value="{{datefrom}}"/>

    And in this example, moment isn't required on on the server. As long as the url parameters are in the correct format, Joi will handle parsing the date correctly for you.