Mandatory Checkbox on Website Form

Hello,

I did a form on webiste module.

Everything works great, but as I receive data from the users, I added a Privacity checkbox, and I am trying to make it mandatory so users can’t send the data unless they accept it, but I am not having much luck.

If I create the field Marc (Checkbox) as mandatory, that doesn’t work.

I added a Custom Script, but I am unable also to make it work.

I have the checkbox value as undefined

This is the code I am using:

frappe.web_form.validate = () => {
value = frappe.web_form.get_value(“Privacidad”);
if (value == 1)
return true;
else
frappe.msgprint("Por favor, acepta la condición de Privacidad);
return false;
};

I am not sure what I am doing wrong.

Many thanks and kind regards,

Alb

Kindly validate the script in any javascript validator.
Look like the string quote is missing atleast here.

1 Like

Try this:
frappe.web_form.validate = () => {
let value = frappe.web_form.get_value(“Privacidad”);
if (value == 1) {
return true;
} else {
frappe.msgprint(“Por favor, acepta la condición de Privacidad”);
return false;
}
};

1 Like

Many thanks @sternbj !!!

So many changes and tests because I was unsure how to get the value from the field of the form, and at the end I was failing on the quotes.

Thanks and kind regards from Spain,

Alb

Hi @jalajc,

Many thanks for your reply.

So many tests and trying to find examples on the forum and documentation, and at the end I had it wrong because of the quotes.

Many thanks and kind regards from Spain,

Alb

this happens with every developer, thats ok. happened with me too :slight_smile:
(Analogy…) phone was not charging so the entire mobile phone was opened and dissembled because . no-clue why.
Later it was discovered after spending much time and effort that the charging-cable was faulty… and in worst case someone also forgot to switch on the socket altogether. :smile:

so always good to start diagnose/debug with basic checks (such as syntax check)

1 Like

Hi @jalajc,

I wish I was a developer…

:sweat_smile: You are right with your analogy , that also happens to me sometimes on my normal job. I repair something and when checking I have to disassemble again to find after some time that I had forgotten to connect a battery, or switch on a fuse… :pensive:

But I will follow your advice and next time I start with the basic checks!

Thanks and kind regards,

Alberto

Hello, this is not working for me. Regardless of whether someone checks the box or not, the person gets a validation error that the box is not checked.

Hello @mrPauwHaan ,

It is working on my side.

Do you have the code as @sternbj wrote?

I have it like this:

frappe.web_form.validate = () => {
let value = frappe.web_form.get_value(“privacidad”);
let value1 = frappe.web_form.get_value(“permiso”);
if (value == value1 ==1){
return true;
}
else{
frappe.msgprint(“Por favor, tienes que aceptar la condición de Privacidad y darnos permiso para publicar tus comentarios”);
return false;
}
};

1 Like

The code you suggested works, I do not know what went wrong with the other code

1 Like

Very glad it worked.