Created
September 22, 2010 03:12
-
-
Save isaiahdw/591068 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} |
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
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.