Welcome Guest, Not a member yet? Register   Sign In
Like CI Form Validation is there any easy way to have multiple (Groups) validation in DataMapper Model Extension?
#3

[eluser]OverZealous[/eluser]
Sorry this is so much later, but I just read your post and wanted to make a suggestion:

When saving an existing object, you should load that object in, first. This allows DM to determine which fields have changed, and only update those fields.
Code:
$u = new User();
$u->get_by_id($input->form->post['user_id']);
// set data
$u->save();
// etc.

If it is a new object, every required field should be filled out (I mean, it's required, right?).

If the field is sometimes required, and sometimes not, I have a simple trick that works really well. I add a _required method to the model, which I can then always return TRUE on. This prevents errors for empty fields. Then I have a custom validation routine that determines if that field is necessary or not:
Code:
$validation = array(
    array(
        'field' => 'first',
        'rules' => array('required', 'check_names')
    ), // etc
);

// down in the validation routines

function _required($field) {
    if(in_array($field, array('first', 'last')) {
        return TRUE;
    } else {
        return !empty($this->{$field});
    }
}

function check_names($field) {
    // require either first OR last
    return empty($this->first) && empty($this->last);
}

You will need to have a language key called "check_names" with a message like:
"You must include either a first name or a last name."


Messages In This Thread
Like CI Form Validation is there any easy way to have multiple (Groups) validation in DataMapper Model Extension? - by El Forum - 03-22-2009, 08:52 PM



Theme © iAndrew 2016 - Forum software by © MyBB