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?
#1

[eluser]dmyers[/eluser]
I have really been digging into datamapper and was wondering if like CodeIgniter's Form Validation which has validation "groups" is there an easy way to have "groups" on a datamapper model?

http://ellislab.com/codeigniter/user-gui...ation.html

For example on 1 form I have every field BUT email (or whatever) if I try to validate the form (via the datamodel) it fails because I haven't set the "email" field.

Perhaps a "not check" value of some kind???

What I ended up doing is this in which I change the validation via a function call. Any body else have any ideas?


Code:
$user = new User();

$user->firstname_validation();
        
$user->first = $_POST['edffirst'];
$user->last = $_POST['edflast'];

$user->validate();


Code:
class User extends DataMapper {

        var $table = 'users';

        function _username_check($field,$param=null) {
            /**
            Custom Error msg in datamapper_lang.php file
            $lang['username_check']    = '%s user name check failed';
            */
            
            return false;
        }

        function default_validation() {
            $this->validation = array(
            array(
                'field' => 'first',
                'label' => 'First Name',
                'rules' => array('required','alpha_numeric')
            ),
            array(
                'field' => 'last',
                'label' => 'Last Name',
                'rules' => array('required','alpha_numeric')
            ),
            array(
                'field' => 'email',
                'label' => 'Email',
                'rules' => array('required', 'username_check', 'trim', 'valid_email')
            )
        );
        }        

        function firstname_validation() {
            $this->validation = array(
            array(
                'field' => 'first',
                'label' => 'First Name',
                'rules' => array('required','alpha_numeric')
            ),
            array(
                'field' => 'last',
                'label' => 'Last Name',
                'rules' => array('required','alpha_numeric')
            ),
        );
        }        


        function __construct() {
        parent::DataMapper();
        
        $this->default_validation();
    }
}
#2

[eluser]dmyers[/eluser]
Ok, this is what I came up with I think it's the easiest.

Adding this

Code:
function only_validate($inp=null) {
        $remove_set = array_diff(array_keys($this->validation),explode(',',$inp));
    
        foreach ($remove_set as $field)
            $this->validation[$field] = array('field'=>'','label'=>'','rules'=>array());        
    }

to your class and/or datamapper you can use it like this.

Code:
$u->only_validate('first');
$u->only_validate('email,first');

Unit Test against each

Code:
$u = new user();

$u->only_validate('first');
$u->only_validate('email,first');

$u->validate();
        
echo $u->error->string;

Then you can leave you validation in place and turn off what you need depending on the form requirements.

What do you think?
#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."




Theme © iAndrew 2016 - Forum software by © MyBB