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
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
thanks much Todd. I'll try to digest this.
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
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Awesome LOL •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
422 Real Estate Australia , now open Check it out
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Awesome LOL •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
- Spam
- Abuse
- Troll
1 • Off Topic Insightful 1Awesome LOL •Gottya
422 Real Estate Australia , now open Check it out
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Awesome LOL •