Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Custom Validation in Modules
  • Hi, 

    I've been learning and liking the framework so far, and I was playing about with modules and themes and managed to get some working examples, but I was wondering what the best way of including custom validation rules were? 

    I have custom validation rules set up as per this section of the docs: https://fuelphp.com/docs/classes/validation/validation.html#/extending_validation, but these rules don't seem to work in my modules. Is it better then to extend the core validation class? 

    Thanks in advance and look forward to progressing with the framework. 
  • There are several ways of using custom validation rules, depending on your need.

    Extending the core validation is easy, all your own rules are available immediately and everywhere, but it isn't really clean, it creates a dependency if you're not careful, and it requires a class file in the app, making it not modular at all. 

    We usually use a layered approach:

    - Basic rule: We don't touch core classes unless ABSOLUTELY necessary
    - For global, app-wide rules, we use a dedicated class in app/classes
    - Every module can have a class with the same name in modules/module/classes
    - Every model can have model specific validation rules

    And we use a method in the model to construct the validation object, which uses add_callable() to add the appropriate validation rule classes (from the list above).
  • Hi and thanks for your reply.

    So in my module if my file structure was like:

    • mymodule
    • classes
    • controller
    • myrules.php

    I should then be able to access the custom validation rules in the myrules.php file? Because if so then It doesn't seem to be working for me. 

    Thanks a lot.
  • You mean: if I create a Validation object in my myrules controller which is in my mymodule module?

    the answer is yes, all the above options should work fine. I don't know how and where you have defined your own rules, and you try to use them, so I can't comment without more info.
  • Sorry about that the formatting of the list items didn't seem to work.

    I meant to say that in my modules classes/controller folder say I had a controller called Home.php.

    mymodule/classes/controller/Home.php

    And some custom validation rules in the classes folder in a file called myrules.php.

    mymodule/classes/myrules.php

    Then I use add_callable method in the Home controller to use those validation rules in myrules.php.

    Thanks for the speedy reply.
  • If you pass a string to add_callable(), make sure it's fully qualified, so "\Mymodule\Myrules".
  • That fixed it.

    I was just wondering though. Wouldn't this mean I had to define the same validation rules a few times? Say I had a rule to check for the existence of an email on both a login form and registration form if there were a login module and a registration module? 

    Thanks a lot. 
  • We use a global rule class for those rules. You can add multiple classes.
  • Sorry to keep on bothering you.

    So the global rule class will be situated in, app/classes/myrules.php? 


  • Yes. 

    Or in a package if you want the ability to reuse it in other apps (without having to copy files).
  • Hmm it seems to be working now. I couldn't get it to work globally before for some reason. Must have been user error.

    Thanks a lot for your help.
  • You're welcome. Happy coding. ;)

Howdy, Stranger!

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

In this Discussion