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 Form Validation

edited May 2012 in Framework

As it is right now, validation checks for unique if the field is allowed to be empty or not. Is it easily possible to allow for an empty result to bypass the unique validation?

Comments

  • Yep, you can add skip_if_empty = 1 to fields you want to make optional but still validate when filled out.

  • edited May 2012

    Took me a bit to get that to work. It should be noted the skip_if_empty = 1 only appears to work if it is the first setting in the field options.

  • Just updated the docs to mention that.

  • Here's another question. Upon editing a form that has a unique field requirement from the database, should we implement (or is there already) a way to bypass checking on itself. As it is, editing while requiring a unique value invalidates a model against itself.

  • You probably need to write a custom callback validator for that. Something like this might do it:

    function myapp_validate_unique_for_edit ($value) {
        $id = DB::shift ('select id from my_table where fieldname = ?', $value);
        if ($id !== false && $id !== $_GET['id']) {
            return false;
        }
        return true;
    }
    

    You can also constrain the field in the database to be unique, e.g., adding unique(fieldname) to the schema, that way it's ensured to be unique or the update will fail.

Sign In or Register to comment.