Skip to content

Instantly share code, notes, and snippets.

@isaiahdw
Created September 22, 2010 03:12
Show Gist options
  • Save isaiahdw/591068 to your computer and use it in GitHub Desktop.
Save isaiahdw/591068 to your computer and use it in GitHub Desktop.
<?php
// Pass the data to the object
$user = ORM::factory('user')
->values($_POST, array('display_name', 'username', 'password', 'company_id', 'email', 'group_id'));
// Additional validation. Remember we need to pass in $_POST again so we can validate the extra fields!
$user->validate($_POST)
->rule('password_confirm', 'matches', array('password'));
try
{
// Save the object
$user->create();
}
catch (ORM_Validate_Exception $e)
{
$errors = $e->validate->errors('validate/user');
}
@zeelot
Copy link

zeelot commented Sep 22, 2010

You do not access the validate method from outside, I wish it would just be protected! The extra validation object goes in the create() method call, or save() when it gets reintroduced. You also do not ask the exception for the validation object, you simply ask it for the errors.

@isaiahdw
Copy link
Author

Yeah, I'm just trying to come up with a simple way to make it better ;) I don't really like passing a validate object into create()/update()/save() I think that gets messy. Wouldn't it be better to have a "validate()" method that you can pass the validation object into?

The thing I like about my above example is it does what we need. It allows you to use external data for validation, and set extra rules/callbacks outside the model. Very little code modification is required to make it work this way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment