How to do I use Addrule() or ValidateRule() to create a custom rule for validation purposes.
peregrine
MVP
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
-
Todd
Vanilla Staff
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();3
Answers
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().
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();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.
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.
I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.
Gottya
There was an error rendering this rich post.