Helloeveryone, I watched alittlevalidation systeminFuelPHP, Ihave come to understanda little about howto factorthis ismy codefor errormessagesetc.... I also looked atthe operation of theclassFieldset() andValidation ()butthere is one thingthat escapes me.
InSymfony2orZendFrameworkoreven inkohana(Ihad troubleanyway)andLaravelsee howI couldoutsourcemyvalidationsso that I canavoid having torewrite mycode.
Take the example ofa large applicationwhere we wouldhave totreat manyforms, the examplesI could findon the net aregoodfor smalltreatment butnothingsuitablefor complexsituations.
In casewe do notwant to tieourvalidationswithaModelclass,how tooutsource itsvalidationin relationto the forms?
I triedto create aclassin a fileas"Form/article.php"asclass nameForm_Article().I tried tosendan object in theValidate method::forge (newForm_Article()), butit does not work.
Even with thedocand searchingin the codeitis notvery intuitive andIdid not understandI think, validationforge.
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, Iactuallysawthis partthere, butit seems that itisto extendorcreate your ownvalidation rules,am I wrong?
in factthis is myclassvalidationfieldsso that I canthenreuse it elsewhere. Iactually thoughtyou can create avalidation objectgroupingrelatedfieldsas shownbelowin myclass.
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;
okI understand better, I thinkI was lookingtooFuelPHPa methodthat allowed me toFactorizermyvalidationsfields,I thought itwas possiblejust likeinSymfony2by creating an objectand sendthevalidationclass::factory ().
I thinkin these casescreate an abstractclassin order tofacilitate this. I understandwellhowIinstantiatein mycontroller?Excuseme, butI confess I donot fullycontrol thevalidationFuelPHP.
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.
Theclasswill take careof instantiatingtheclassValidation::forge ().by callingtheclassasbelow:ValidateRules::getRules('the name of thefile containing thevalidation rules');
The staticclasscan takea second parameter: ValidateRules::getRules('filename', 'nameofinstancevalidation'); The name of theinstanceValidation::forge ({name})
How it works: We need to createa directoryat the root oftheclasscalled"rules" folderinsidethat folderI createa filethat isprefixedbythe name ofdirectory classes_{nameyourvalidation}.php
Inside thefilethere is avalidationtable,this tablethat filebackinclassValidateRules::getRules()simply.
TheValidateRulesclass() cantakeafirst parametertable with thefilenames ofvalidation rules. Theclasswillautomaticallymerge the array ofrules,againstI forgot toimplementresearch in themodulesI think.
Finally hereif itcan serve as anidea,the best wouldprobablyratherto extend aclasswhere you canextend the rulesof anothervalidation tobe able tocallother placesif you want toreusethevalidation rulesin otherforms. Finallythere with array thatreturns thefilevalidations, it worksas well.
Myclassis not perfect,far from itI guesslol,well ifit cangive ideas toothers.I think itwould be niceto be able todo thisnatively inFuelPHPitwouldfactorizethe rulesof usein order toreuse.
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.