Jump to content
  • 0

Custom Validation For Universal Module Help File


will

Question

http://docs.blesta.com/display/dev/Error+Checking has some erroneous information in it. Under the heading "Custom Rules" the documentation instructs the developer to create a function "getRules()"

 

getRules() may have been called to retrieve the rules for a custom field in previous versions, but it is no longer called. (I have not tested this in the present beta release.) Either getRules() needs to be called once again to retrieve those custom rules, or documentation should specify that the function validateService is the function that needs modification.

 

This is the way I modified validateService to include my rules. I added these lines immediately before the line $this->Input->setRules($rules);

 

The first parameter of rules, "hostname", identifies the variable the rule is for. The second parameter, e.g. "format" is an arbitrary name you are assigning to the rule. It must be unique within the scope of the variable. (That is, two variables can have a rule with the same name, but a single variable must have different names for all of its rules.)

$rules["hostname"]["format"] = array(
                   'pre_format' => array(array($this, "wrap_strtolower")), /* wrap_strtolower is a function in the same class as this function. It simply returns strtolower($inputstring) because strtolower() is not accessible as a callback function. The function returns a string which will be passed to the rule. */
                   'rule' => array("matches", "/^[a-z][a-z0-9]{2,31}$/"),
                   'message' => 'Hostname is incorrectly formatted. It must be between 3 and 32 characters. It must begin with a letter and contain only letters, numbers, and dashes.',);
$rules["hostname"]["absent"] = array(
                   'pre_format' => array(array($this, "wrap_strtolower")),
                   'rule' => array(array($this, "validateAbsent"),), /* validateAbsent is a function in the same class as this function. It returns true if it finds that its input is valid, and false if not. */
                   'message' => "This hostname already exists.");

Directly setting $rules to an array as recommended by the documentation should be considered harmful because it will clobber any rules already set, such as checking that the field is required (if that was specified in the module configuration).

 

I've made this bug report verbose to assist anyone who was confused by the documentation. :)

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

The use of "getRules" on the Error Checking page is simply to aid the example of how you might define a custom callback method in a validation rule. How you decide to use the rules to validate input is not covered in the example since it would be superfluous.

 

As flangefrog mentioned, you would fetch the defined rules when setting them to the Input component.

 

 

Directly setting $rules to an array as recommended by the documentation should be considered harmful because it will clobber any rules already set, such as checking that the field is required (if that was specified in the module configuration).

Setting rules to an array wouldn't be considered harmful. As a developer, it is up to you how best to manage your rules for validation, such as by merging your custom rules with rules that already exist if you choose not to directly update the array of rules shown in the example.

 

As for the Universal Module, it supports a JSON-encoded object of input rules, but cannot specify custom callbacks unless you update the module itself to support such an action.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...