Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID Sign In with Twitter

In this Discussion

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

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.


factoid: Most questions have been previously answered, try the search box first, please provide your Vanilla version Number!

Peregrine's Addons - donations gladly accepted for "successful solutions" and addons - kind of like tipping a waiter at a restaurant

Best Answer

  • ToddTodd Chief Product Officer vanilla
    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();
    
    peregrine422

Answers

Sign In or Register to comment.