Jose LHS
Jose LHS

Reputation: 177

Passing parameter from view to controller on Play Framework

I want to pass a parameter from a view to a controller, I'm not sure how to do it.

This is my view:

#{extends 'crud.html' /}

#{form @DesglosesHorarios.updateValues(), class:"form-horizontal"}
    <div id="add-date">
        <input type="hidden" name="array-fechas" value="" />
        <fieldset>
            Fechas  <input type="date" id="fecha-update" placeholder="Introduce fechas separadas por coma"/>
            <button type="button" id="add-date-button" class="btn btn-primary">Añadir</button>

        </fieldset>
    </div>
    </br>
    <div id="value-dates">
        <ul></ul>
    </div>
    <div id="fecha-error"><ul></ul></div>
    <div class="form-actions">
        <button type="submit" class="btn btn-primary"><i class="icon-ok icon-white"></i> &{'save'}</button>
        <a class="btn" href="@{list()}"><i class="icon-remove"></i> &{'cancel'}</a>
    </div>
#{/form}

This is the function I want to use in the controller:

public static void updatevalues(String[] fechas) {
        for(String fecha : fechas){
            updateXML(CalendarUtil.formatDate(CalendarUtil.ParseFecha(fecha)));
        }
        render(request.controller + "/updatevalues.html", fechas);

    }

And this is a JavaScript code that I'm using to catch data and put it into an array:

fechas=[]; 

$("#add-date-button").on('click', function(){
    exprfecha = /^([0-9]{2}\/[0-9]{2}\/[0-9]{4})$/;
    values = $("#fecha-update").val().replace( /\s/g,"");
    values = values.split(',');

    errores=[];

    for(i=0; i<values.length; i++){
        if(values[i].match(exprfecha)){
            if(fechas.indexOf(values[i]) == -1){
                fechas.push(values[i]);
                $("#value-dates ul").css("color","green");
                $("#value-dates ul").append('<li>'+values[i]+'</li>');
            }
        }else{
            errores.push(values[i]);
        }
    }

    $("#fecha-error").empty();
    if(errores.length != 0){
        $("#fecha-error").css("color","red");
        for(i=0;i<errores.length;i++){
            $("#fecha-error").append('<li>'+errores[i]+" no tiene el formato adecuado (DD/MM/AAAA).</li>");
        }
    }
});

Upvotes: 0

Views: 1037

Answers (2)

Jose LHS
Jose LHS

Reputation: 177

Sure. I think that there will be better ways to do this, and this is kinda a personalised solution for my case, but this worked for me. I saw that params.body gave me a string like this one: authenticityToken=8a10c96e3b2f16d68eee075e268a6b3886b9bc64&fecha%22=15%2F07%2F2015

So I simply used this params.body on my controller and took what I needed from the string and process it to obtain the date.

String [] body = params.get("body").split("=");
String paramfechas = body[2];
paramfechas = paramfechas.replace("%2F", "/").replace("%2C", ",");
List<String> fechas = new ArrayList<String>();
String [] fechas = paramfechas.split(",");

On this way I obtained the date(or dates, depending on how many dates were on the params.body) on the format I desired (DD/MM/YYYY)

Upvotes: 0

Kris
Kris

Reputation: 4823

Since your view is running on the client-side (browser) you can use a HTTP POST (AJAX or normal) to let your server-side (e.g. your controller) know about your parameter. I see you use jQuery, so you could use jquery.post to post your data back to your controller. In your controller you'd need a second action method that handles this post from your form. Have a look at the Play tutorial about forms and here a couple of older Play examples that include form handling.

Upvotes: 1

Related Questions