wiki:IS_ONE_OF_EMPTY

Version 5 (modified by Fran Boon, 14 years ago) ( diff )

Update hyperlinks

IS_ONE_OF_EMPTY

validator

This one has all properties of IS_ONE_OF but one: it does not send back to the browser a potentially huge dropdown with all values.

Instead, it creates the INPUT box which can carry the corresponding lookup id.

And when the form gets sent back to the server, the content of this INPUT box gets validated on the server against the corresponding lookup table.

How to use the IS_ONE_OF_EMPTY?

1. Controller

Keep in mind that the corresponding model assumes default validator against the organisation_id.

The working example is controllers/or.py:

here the organisation_id is not mandatory, but: if present, must be validated against the (large) organisation table.

def office():
...

    # the update forms are not ready. when they will - uncomment this and comment the next one
    #if request.args(0) in ("create", "update"):
    if request.args(0) == "create":
        db[table].organisation_id.requires = IS_NULL_OR(IS_ONE_OF_EMPTY(db, "or_organisation.id"))

...

Note: the method update has a separate view file, and as of now it is not having the auto-complete functionality yet.

Once it is ready, the line

if request.args(0) in ("create", "update"):

will replace the existing one, taking care of just create:

if request.args(0) == "create":

2. View - Auto-complete

And the complimentary view or/office_create.html

<script type="text/javascript">//<![CDATA[
$(function() {
    // Hide the real Input Field
    $("#or_office_organisation_id").hide();
    // Add a dummy field
    $("#or_office_organisation_id").after("<input id='dummy_organisation' class='ac_input' size=50 />");
    {{include 'or/organisation_autocomplete.js'}}
    // Populate the real Input when the Dummy is selected
    $("#dummy_organisation").result(function(event, data, formatted) {
        var newvalue = data.id;
        $("#or_office_organisation_id").val(newvalue);
    }); 
});
//]]></script>

............(back to DeveloperGuidelinesTips)..............

Note: See TracWiki for help on using the wiki.