How do i set an absolute rating count?

I created a custom rating type field called “Rating” and I want the user to be able to only choose an absolute count value when the user is rating.

For Example, a user can only choose stars 1, 2, and 3 and cannot choose partial ratings (decimal values) such as 0.15, 0.35, 0.50, etc.

I cannot find how to do that, is there any property of the rating type field to be set?

Reference screenshot for better understanding:

You can add validation in client-side scripts or server-side scripts in the validate method to restrict selecting partial ratings.

python code

def validate(self):
		if((self.rating * 10)  % 2) != 0:
			frappe.throw("Your Error message")

Javascript code

frappe.ui.form.on("Doctype", {
    validate:function(frm){
        if((frm.doc.rating * 10) % 2){
            frappe.throw("Your Error message")
        }
    }
})
1 Like

Thank you very much, Mr. @ejaaz, for the prompt response. From your reply, I got the message that I have to write code to get/set absolute count as this feature is not available as a property in Rating data type.

however, the suggested formula (((self.rating * 10) % 2) ) to check Modulus value didn’t work for me instead I achieved it from the below server script and the same formula is applicable for the client script as well. Once again thank you for your guidance.

Python code considering rating field set to three star:

def validate(self):
       targets = [0.333, 0.666, 0.999]
        gotAbsValue=(min(targets, key=lambda x: abs(rating - x)))
        frappe.log(gotAbsValue)

UPDATED:
JS code considering rating field set to three star:

frappe.ui.form.on("Doctype", {
    validate:function(frm){
        const targets = [0.333, 0.666, 0.999];
        let ratingValue = frm.rating;
        let gotAbsCount = targets.reduce((a, b) => 
            Math.abs(ratingValue - a) < Math.abs(ratingValue - b) ? a : b
        );
        alert(gotAbsCount);
    }
})

1 Like