Course Content
Controller Validation for Membership
COMPLETED
Faris Ansari
May 19, 2021

Now, let's write code that will make sure whenever a Library Membership is created, there is no active membership for the Member.

library_membership.py

from __future__ import unicode_literals

import frappe
from frappe.model.document import Document

class LibraryMembership(Document):
    # check before submitting this document
    def before_submit(self):
        exists = frappe.db.exists(
            "Library Membership",
            {
                "library_member": self.library_member,
                # check for submitted documents
                "docstatus": 1,
                # check if the membership's end date is later than this membership's start date
                "to_date": (">", self.from_date),
            },
        )
        if exists:
            frappe.throw("There is an active membership for this member")

We wrote our logic in the before_submit method which will run before we submit the document. We used the frappe.db.exists method to check if a Library Membership record exists with our provided filters. If it exists, we used frappe.throw to stop the execution of program with a message that will show up letting the user know the reason.

Now, try creating a Library Membership with an overlapping period and you should see an error when you submit the document.

Library Membership Validation

Questions
Validation for Membership
rehan haider
6 months ago
Post
Dismiss

I've followed the steps exactly the name but unable to see the desired result i'm able to make multiple Library membership of a single member which is not supposed to execute like this please help.

Frappe Framework: v12.25.0 (version-12)

rehan haider
6 months ago
Post
Dismiss

not name its SAME

GG Gouri Gaviamth
2 months ago
Post
Dismiss

i have followed same steps but unable to see desired results .i can create multiple membership of single member.And i m not geting full name of memeber

Want to discuss?
Post it here, our mentors will help you out.
Link to a DocType
Steven Choo
9 months ago
Post
Dismiss

Frappe Framework creates a linking to a doctype is good, but sometimes, a developer may not want to link the whole table, insteads, we want a sub-set of a table. For example, we want to link library membership which is active. If frappe can create a view or a sub-set of the table based on SQL (for example, library members who are active and the link can be linked to a view instead of a doctype).

May have to resort to coding to revolve this? Please advise, thanks!

BB belajar bahasa arab
9 months ago
Post
Dismiss

you can edit in form script

Steven Choo
9 months ago
Post
Dismiss

So, your solution is to go to form scripts to do coding. Odoo and Zoho have a better solution without coding...

Want to discuss?
Post it here, our mentors will help you out.