Hey guys, our company is looking to start using FuelPHP as it's main framework for development.
Personally I come from a CakePHP background, and really like the way that Cake handles a lot of things, such as validation - i.e. set the rules once in the model, and everything works together to honour the rules.
Moving into Fuel, however, it looks like they have a different approach to this, where the validation rules need to be set in the controller. The down side of this is lots of repeated code - i.e., even if you only have add and edit actions, that's still two places that you need to define your rules.
Because of this, I was hoping that someone might be able to help me out and let me know if it's possible for the model to handle the validation?
Thank you :)
As I've been too busy lately, regretably I haven't gotten round to documenting a lot. There are a couple of options:
Create a set_form_fields() to use with validation that you pass an instance of the Fieldset class and should setup all fields (including validation). An example can be found here (though based on an outdated version of Fuel). Once you've got such a method in your model you can use
Validation::factory()->add_model('Model_Example')
(Rules can also be created within the model, prefix them with_validation_
and you can use them when adding the model like above)Set the validation rules in the
$_properties
property of a Orm\Model class and add the model like with 1. (the Orm\Model has the set_form_fields() method build in)Add the Orm\Observer_Validation to your Orm\Model
$_observers
property and validation will be done upon saving your model, throwing aValidationFailed
exception when failing. The errors can be fetched by the classname from the Validation class after this, for example: when a Model_Example instance failed saving because of validation you can fetch the full validation instance usingValidation::instance('Model_Example')
or fetch the errors usingValidation::instance('Model_Example')->show_errors()
精彩评论