Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Options

How to do I use Addrule() or ValidateRule() to create a custom rule for validation purposes.

peregrineperegrine MVP
edited August 2012 in Vanilla 2.0 - 2.8

how would one use the AddRule() or ValidateRule() - a bit confusing for me.

e.g. to create a custom rule that checked to see if an integer was greater than 50.

there is currently a length rule - with a maximum
in class.validation.php

A bit hard for me to decipher the function.

I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

Best Answer

  • Options
    ToddTodd Chief Product Officer Vanilla Staff
    Answer ✓

    Yeah this isn't super easy. Let me explain the best I can.

    First you have to call Gdn_Validation->AddRule() to make the rule available to the model. You usually do this in your model, but you can also do it in the controller. Just do it before saving.

    // Call this inside your Gdn_Model class.
    $this->Validation->AddRule('MyCustomRule', 'function:ValidateMyCustomRule');
    
    // MyCustomRule: This is a name for the rule. It'll be used to generate error messages so you'll want to translate it too.
    // ValidateMyCustomRule: You need to write this function. It will be passed the value to validate.
    
    function ValidateMyCustomRule($Value) {
      if ($Value == 'peregrine rocks!')
        return TRUE;
      else
        return FALSE;
    }
    

    Now you can use your custom rule on any number of columns by calling Gdn_Validation->ApplyRule().

    $this->Validation->ApplyRule('Description', 'MyCustomRule');
    

    This readies the rule for validation, but doesn't actually validate the data yet. It just makes your model ready for validation. To validate you usually do something like this:

    if ($this->Validate($Post, $Insert)) {
       $this->Save($Post);
    }
    

    So that's that and is really meant for formal model writing. Sometimes you just want to quickly validate some data in a plugin or a controller method. For this you can call the static method Gdn_Validation::ValidateRule().

    $Valid = Gdn_ValidateRule($Post['Description'], 'Description', 'function:ValidateMyCustomRule', 'Incorrect!!!');
    if ($Valid !== TRUE)
      $this->Form->AddError($Valid);
    

    Really though, you just want to call Gdn_Form's ValidateRule.

    $this->Form->ValidateRule('Description', 'function:ValidateRequired');
    $this->Form->ValidateRule('Description', 'function:ValidateMyCustomRule', 'Incorrect!!!');
    // Bunch of other rules.
    
    if ($this->Form->ErrorCount() == 0) {
       // Redirect to a success page maybe.
    }
    // Otherwise just render the form and the errors will be displayed when you call echo $this->Form->Errors();
    

Answers

  • Options
    ToddTodd Chief Product Officer Vanilla Staff
    Answer ✓

    Yeah this isn't super easy. Let me explain the best I can.

    First you have to call Gdn_Validation->AddRule() to make the rule available to the model. You usually do this in your model, but you can also do it in the controller. Just do it before saving.

    // Call this inside your Gdn_Model class.
    $this->Validation->AddRule('MyCustomRule', 'function:ValidateMyCustomRule');
    
    // MyCustomRule: This is a name for the rule. It'll be used to generate error messages so you'll want to translate it too.
    // ValidateMyCustomRule: You need to write this function. It will be passed the value to validate.
    
    function ValidateMyCustomRule($Value) {
      if ($Value == 'peregrine rocks!')
        return TRUE;
      else
        return FALSE;
    }
    

    Now you can use your custom rule on any number of columns by calling Gdn_Validation->ApplyRule().

    $this->Validation->ApplyRule('Description', 'MyCustomRule');
    

    This readies the rule for validation, but doesn't actually validate the data yet. It just makes your model ready for validation. To validate you usually do something like this:

    if ($this->Validate($Post, $Insert)) {
       $this->Save($Post);
    }
    

    So that's that and is really meant for formal model writing. Sometimes you just want to quickly validate some data in a plugin or a controller method. For this you can call the static method Gdn_Validation::ValidateRule().

    $Valid = Gdn_ValidateRule($Post['Description'], 'Description', 'function:ValidateMyCustomRule', 'Incorrect!!!');
    if ($Valid !== TRUE)
      $this->Form->AddError($Valid);
    

    Really though, you just want to call Gdn_Form's ValidateRule.

    $this->Form->ValidateRule('Description', 'function:ValidateRequired');
    $this->Form->ValidateRule('Description', 'function:ValidateMyCustomRule', 'Incorrect!!!');
    // Bunch of other rules.
    
    if ($this->Form->ErrorCount() == 0) {
       // Redirect to a success page maybe.
    }
    // Otherwise just render the form and the errors will be displayed when you call echo $this->Form->Errors();
    
  • Options

    thanks much Todd. I'll try to digest this.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Options
    422422 Developer MVP
    edited August 2012

    if ($this->Form->ErrorCount() == 0) {

    Doesnt this mean if error count does not equal zero ? So if it isnt zero then its got an error, so you wouldnt direct to success page?

    Im pretty crap on php / js operators

    There was an error rendering this rich post.

  • Options
    peregrineperegrine MVP
    edited August 2012
    $this->Form->ErrorCount()   returns an error count
    
    so if the returned error count is equal to 0
    
    0 - one  would assume means no error count - meaning success
    
    therefore 
    
    ($this->Form->ErrorCount() == 0)   would equate to True
    
    if true
      then success
    

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Options
    422422 Developer MVP

    Gottya

    There was an error rendering this rich post.

Sign In or Register to comment.