Do not allow date 50 years back in the database

Asked

Viewed 104 times

2

I have a JSF system and Primefaces 4.0 and I have a field <p: calendar> in which I did the treatment to not load dates in the current day Calendar up to -50 years.

But still the user can type it through the keyboard and insert it into the database.

I need a method of not letting a date like that be included, what would be the best way to do it?

Follow an example of the class:

public class DocumentoClienteModel  {

    private Date dateInicio;

    public Date getDtInicio() {
        return dtInicio;
    }

    public void setDtInicio(final Date dtInicio) {
        this.dtInicio = dtInicio;
    }
}
  • Oi Bruno, you can implement javascript on your screen?

  • I think I just need to make sure the security, have any idea?

  • If yes, I can help you!

  • I can, how could I?

  • Just one minute....

  • Beforehand, if my answer is useful, could I vote in favour of it and mark it as a useful response? It would help a lot :)

  • 1

    Absolutely ! I will vote no problem

  • I posted an answer, tell me if it helped you, right? Hugs!

  • 1

    I saw answers giving solution to solve your problem on the front end. But you know that the user can circumvent everything that is in the browser, right? I don’t know anything about Java, but probably, the logic could be developed in the backend, for validation if the date is the one you don’t want to let enter. I say this because you quoted "database". If you do not want to let it enter in the database, validate it in the backend. If you do as the answers say, even to use readonly, is very easy to cover your app.

  • On validation at backend vs frontend: https://answall.com/q/13298/112052

Show 5 more comments

1 answer

1

I would make a method to take the minimum date and set the component, staying that way:

public Date getMinDate(){
    Date minDate = new Date();
    Calendar calendarData = Calendar.getInstance();
    calendarData.setTime(minDate);
    calendarData.add(Calendar.YEAR,-50);

    return calendarData.getTime();
}

in xhtml:

    <h:form>
        <p:calendar value="#{mBDates.minhaData}"  mindate="#{mBDates.minDate}" id="dtLast"></p:calendar>
        <p:message for="dtLast" display="icon" />
    </h:form>

And in case the smart user enters the date in his hand, he would put a validation before saving:

public boolean dataValida(Date data){
    int compare = data.compareTo(getMinDate());
    //se a data for igual ou menor a data minima retorna falso
    //e não salva meu formulario, lançando uma advertencia para o usuário
    if(compare == 0 || compare == -1)
        return false;
    //senão retorna que a data é valida e salva meu formuladrio
    return true;
}



    public void salvarMeuForm(){
        if(!dataValida(minhaEntidade.getMinhaData()))
           FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro!", "Data não permitida."));
   }
  • You can do the method dataValida in a row: return data.after(getMinDate()); - the date is valid only if it is later (after) of getMinDate()

  • The code to get the minimum date I had managed to do , now I’m only having difficulty in the part of the user type in hand, create public boolea dataValida(date Date ) in my class as said and on the front I need to put something else ?

  • @Brunos if I were making this form, I would put an error message when the user tries to save when typed an invalid date, I will edit my reply and put the example with the message.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.