Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

Unique input in a combination of two entry fields

edited March 2012 in Apps

I have been reading http://www.elefantcms.com/wiki/Forms-and-input-validation about form validation. I see a validation rule like this:

unique = "table.column"

However I need the combination of two entry fields to be unique, author.first_name and author.last_name. How would I go about this?

Comments

  • What I would do is write a custom callback function validator and set it via callback = author_unique_name then in PHP:

    <?php
    
    function author_unique_name ($value) {
        $res = Author::query ()
            ->where ('first_name', $_POST['first_name'])
            ->where ('last_name', $_POST['last_name'])
            ->count ();
        if ($res > 0) {
            return false;
        }
        return true;
    }
    
    ?>
    

    This assumes an Author model object, but could just as easily be written without too:

    <?php
    
    function author_unique_name ($value) {
        $res = db_shift (
            'select count(*) from author where first_name = ? and last_name = ?',
            $_POST['first_name'],
            $_POST['last_name']
        );
        if ($res > 0) {
            return false;
        }
        return true;
    }
    
    ?>
    

    The key here is that the callback is using the $_POST values instead of the value passed to it, otherwise it would only have access to either of the two values, but not both at the same time. Let me know how that works out :)

  • Into which file do I put the function?

  • edited March 2012

    I put it in /apps/myapp/models/Author.php like so:

    <?php class Author extends Model { function author_unique_name ($value) { $res = Author::query () ->where ('first_name', $_POST['first-name']) ->where ('last_name', $_POST['last-name']) ->count (); if ($res > 0) { return false; } return true; } } ?> However it always returns false even though the database is empty. I tried inserting:

    return true;

    as the first line of the function and got the same result.

  • Sorry about the ugly formatting. Here it is again:

    I put it in /apps/myapp/models/Author.php like so:

    <?php
    
    class Author extends Model {
    
        function author_unique_name ($value) {
            $res = Author::query ()
                ->where ('first_name', $_POST['first-name'])
                ->where ('last_name', $_POST['last-name'])
                ->count ();
            if ($res > 0) {
                return false;
            }
            return true;
        }
        
    }
    
    ?>

    However it always returns false even though the database is empty. I tried inserting:

    return true;

    as the first line of the function and got the same result.

  • edited March 2012

    Allow me to clarify my statement: Even though the author table is empty, not the database.

  • One further clarification just so you don't think this is a bug: I use underscores for database column names and dashes for form input names.

  • I think the only thing you need to change is the callback to this:

    callback = "Author::author_unique_name"
    

    Then the form handling works just like normal. If you run into trouble, here's a gist that should work as an example:

    https://gist.github.com/2244952

    The file names would go into app like this:

    forms_author.php -> forms/author.php handlers_author.php -> handlers/author.php Author.php -> models/Author.php views_author.html -> views/author.html

    The naming doesn't allow for folders... :)

  • Your change solved the problem, thanks.

Sign In or Register to comment.