Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using the new validation business map
#1
In an ongoing attempt to get a hacked installation more and more in an 'original' state, I'll be updating again soon. One of the customizations I made is some custom validations that will be overridden by the dividable validations on inventory modules (which ironically, I wrote). So I'm diving into the new validation business maps, that seem promising. I'd like to get some more info on using the custom functions here.

I see in the docs that a sample validation function is provided (all the way at the bottom) that returns 'true'. I know in the past we had a special '%%%OK%%%' value to return (or something like that). Does anyone have pointers on the return values possible when using custom functions?

Also, I see in the example custom function validation map:

Quote:restrictions: file name, validation test name, function name and label to show on error (will be translated)

And then:

PHP Code:
<map>
  <
originmodule>
    <
originname>Accounts</originname>
  </
originmodule>
  <
fields>
    <
field>
      <
fieldname>email1</fieldname>
      <
validations>
        <
validation>
          <
rule>custom</rule>
          <
restrictions>
          <
restriction>modules/cbMap/Validation.php</restriction>
          <
restriction>testemail</restriction>
          <
restriction>validate_testacccemail</restriction>
          </
restrictions>
        </
validation>
      </
validations>
    </
field>
  </
fields>
</
map

I see three restrictions, the second is the 'validation test name' (testemail), but what is that? Also, the fourth optional (not used in the example) will be the label on error and that it will be translated, but from where, from the target module's language files?
Reply
#2
The possible return values of custom validation function is only true or false.

The validation system uses the Valitron library: https://github.com/vlucas/valitron

This library takes a set of values, then a set of rules, and it applies all the rules to the values in one shot returning an array of all the rules that did not pass. Actually an error message for each rule that did not pass.

This looks like this:

PHP Code:
$v = new cbValidator($screen_values);
$v->rule('required', ['name''email']);
$v->rule('email''email');
$v->validate() 

after that last line, $v contains the result of all the rules it has applied.

A custom validation must have a rule name and a function, so the example in the documentation would be called like this:

PHP Code:
$v->rule('testemail''email'); 

note how I have given my validate_testacccemail() function a rule name. Valitron knows that when it has to apply the "testemail" rule, it must call the validate_testacccemail() function and it expects a true or false value to know if the field passes or not the rule.

Finally, in the result of the validations it will return a string which is the label of the field (Email in this case) followed by the rule that did not pass. For example,

Code:
Email is required


In coreBOS validations you can give an additional parameter to the custom validation rule which will be the text that appears after the field label. If none is given it simply outputs "Invalid"

Keep asking and let me know how it goes.
Joe
TSolucio
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)