Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to external validation rules
  • Hello everyone,
    I watched a little validation system in FuelPHP, I have come to understand a little about how to factor this is my code for error messages etc. ...
    I also looked at the operation of the class Fieldset () and Validation () but there is one thing that escapes me.

    In Symfony2 or ZendFramework or even in kohana (I had trouble anyway) and Laravel see how I could outsource my validations so that I can avoid having to rewrite my code.

    Take the example of a large application where we would have to treat many forms,

    the examples I could find on the net are good for small treatment but nothing suitable for complex situations.

    In case we do not want to tie our validations with a Model class, how to outsource its validation in relation to the forms?

    I tried to create a class in a file as "Form / article.php" as class name Form_Article (). I tried to send an object in the Validate method :: forge (new Form_Article ()), but it does not work.

    Even with the doc and searching in the code it is not very intuitive and I did not understand I think, validation forge.

    example of my code:
    My code

    Thank your for your answers.
  • HarroHarro
    Accepted Answer
    This is documented here: http://docs.fuelphp.com/classes/validation/validation.html#extending_validation

    So there are two options:
    - extend the Validation class in your app/classes, and add your global validation rules to it
    - create a separate class, and add it to the validation instance using add_callable()

    And of course you can combine the both.

    Note that in both cases, the method name should be _validation_<rulename>.
  • Thank's for your answer,
    I
    actually saw this part there, but it seems that it is to extend or create your own validation rules, am I wrong?

    in fact this is my class validation fields so that I can then reuse it elsewhere.
    I actually thought you can create a validation object grouping related fields as shown below in my class.


    My validation class :
    http://pastebin.com/3JWdi4sC

    In fact I do not want to create my validation rule but add validation rules in relation to the names of input fields

    If you give me an exemple.
  • HarroHarro
    Accepted Answer
    Yes, then I misunderstood the question.

    You want to store your rules somewhere? Or you want a generic ruleset?

    You can do that in any ordinary class:

    class Validationrules
    {
        public static function define($fields = array(), $instance = null)
        {
            // if no validation instance was passed, create a new one
            $instance or $instance = \Validation::forge(\Str::random());

            // make sure we've got an array
            is_array($fields) or $fields = array($fields);

            // loop over the fields
            foreach ($fields as $field)
            {
                switch ($field)
                {
                    case 'username':
                        $instance->add_field('username', 'Username', 'required|trim|valid_string[alpha,lowercase,numeric]');
                    break;

                    case 'email':
                        $instance->add_field('email', 'Email', 'required|trim|valid_email');
                    break;

                    case 'age':
                        $instance->add_field('age', 'Age', 'valid_string[numeric]');
                    break;

                }
            }
            // return the validation instance
            return $instance;
        }
    }
  • ok I understand better, I think I was looking too FuelPHP a method that allowed me to Factorizer my validations fields, I thought it was possible just like in Symfony2 by creating an object and send the validation class: : factory ().

    I think in these cases create an abstract class in order to facilitate this.
    I understand well how I instantiate in my controller? Excuse me, but I confess I do not fully control the validation FuelPHP.

    Edit:
    Ok i see, thank you Harro verton for your help ;)
  • An abstract class doesn't really help you, since you can't do anything with it. You need a validation instance, and you need to define rules on that instance.

    My example can be used like so:

    $val = \Validationrules::define(array('username', 'email', 'age'));
    if ($val->run())
    {
        // input validated
    }

    and will return a validation object with those three fields defined.
  • verton thank you, initially I had not seen the instance of validation () in your class, suddenly view that there was no implementation for outsourcing validation rules in connection with the names of fields, I created a class doing the job.

    Class external rules validateRules

    The class will take care of instantiating the class Validation :: forge (). by calling the class as below: ValidateRules :: getRules ('the name of the file containing the validation rules');

    The static class can take a second parameter:
    ValidateRules :: getRules ('file name', 'name of instance validation');
    The name of the instance Validation :: forge ({name})

    How it works:
    We need to create a directory at the root of the class called "rules" folder inside that folder I create a file that is prefixed by the name of directory classes_{name your validation}.php
    image
    Inside the file there is a validation table, this table that file back in class ValidateRules :: getRules () simply.

    My example file validation
    classes_article.php


    The ValidateRules class () can take a first parameter table with the filenames of validation rules.
    The class will automatically merge the array of rules, against I forgot to implement research in the modules I think.

    Finally here if it can serve as an idea, the best would probably rather to extend a class where you can extend the rules of another validation to be able to call other places if you want to reuse the validation rules in other forms.
    Finally there with array that returns the file validations, it works as well.

    My class is not perfect, far from it I guess lol, well if it can give ideas to others. I think it would be nice to be able to do this natively in FuelPHP it would factorize the rules of use in order to reuse.

    What do you think?
  • HarroHarro
    Accepted Answer
    You could make it a lot simpler if you move those files out of /classes (they are not classes), and into /config/rules (for example).

    That would allow you to use the Config class to load and access them, instead of having custom code to load the files.

    Also, try to avoid "self", but use "static" instead. Using "self" leads to issues with extending, so it's better to teach yourself to use "static" by default.
  • ok I will take and I would change it all :) thank you

Howdy, Stranger!

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

In this Discussion