开发者

CakePHP-like validation in FuelPHP

开发者_开发问答

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:

  1. 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)

  2. 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)

  3. Add the Orm\Observer_Validation to your Orm\Model $_observers property and validation will be done upon saving your model, throwing a ValidationFailed 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 using Validation::instance('Model_Example') or fetch the errors using Validation::instance('Model_Example')->show_errors()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜