Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Using Orm\Validation_Observer, how do I find out what field failed validation?
  • Hi guys, So I decided to validate user input in the model using the Orm's Validation_Observer, but how do I get it to tell me which field failed validation? My model...
    class Model_User extends Orm\Model {
        $_observers = array('\\Orm\Observer_Validation');
        $_properties = array(
            'name' => array('validation' => array('required')),
            'email' => array('validation' => array('required'))
        );
    }
    
    Following the example in the midde of this docs page...
    class Controller_User extends Controller 
    {
            public function action_create () {
                $user = new Model_User();
                $user->name = NULL;
                $user->email = 'helloworld@gmail.com';
                try {
                    $user->save();
                } catch (ValidationFailed $e) {
                    echo $e -> getMessage();
                }
            
                return;
            }
        }
    }
    
    If I visit "user/create", all I see is a blank screen. I looked under the hood, and not that I'm a php super-programmer or anything, but it looks like the Orm package is "throwing away" all the goodness of the Fieldset's error messages on line 87 of Fuel/packages/orm/classes/observer/validation.php. The ValidationFailed exception it throws just extends Fuel's base exception which in turn doesn't do anything but extend PHP's native exception. Am I doing something wrong or missing a key configuration option? Should I validate in my controller instead? Thanks for the help!
  • Hi, you are not setting up your models properly, you can give labels and all in the $_properties array look at the docs on creating orm models for more info
  • Ignore kapv89, the $_properties array look right. You aren't catching the Exception, you are trying to catch ValidationFailed which doesn't exist - it's Orm\ValidationFailed. In v1.1 you'll be able to fetch the Fieldset instance from the exception using $e->get_fieldset() from which you can get the validation instance: $e->get_fieldset()->validation().
  • @kapv89,
    Thanks for the input, but even when I included other elements in my $_properties array (like label, data_type, etc), it didn't give me any more info in the exception's message. @jelmer,
    Awesome. Thanks for the heads up about 1.1. I was on 1.0-rc3. I updated to 1.1-rc1, and the get_fieldset() method fixed my problem. (WRT the exception's namespace, I copied the code into my question incorrectly. I did use the namespace in my code. I just forgot to write it in my post. Doh!). Am I the first person to have this issue (or the only one too dumb to figure out a work-around haha)? I'm just curious (I'm new to MVC) but do you/most people validate in the controller? I feel like some validation (e.g., making sure the confirm_password fields is the same as the password field) belongs in the controller, but most validation (e.g., making sure a user's password is valid or a user's name is valid) belongs in the model. Thanks for a great framework (as a senior developer, it's awesome that you take the time to answer questions)!

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion