We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

RESOLVED - EE3: Custom validation rules in Model -- validation method called but never returns error

Development and Programming

webslinger's avatar
webslinger
14 posts
7 years ago
webslinger's avatar webslinger

(EE 3.5.11) I’m trying to implement a custom validation rule in our module’s Model (for a settings table). The standard validation rules (required, integer, etc.) all work fine and return errors when a field does not comply, but I cannot get even the simplest custom validation rule to return an error.

I finally just copied the example custom validation rule shown in the docs for Building Your Own Models (validateMultipleOf[x]) and applied this to one of our fields (both in combination with other rules and by itself). No matter what the field content, the rule never throws an error. (Again, the built-in validation methods do return errors when not valid.)

Below is a stripped down version of the code (including the example rule from the docs). Am I missing a step for these custom rules? Is this a bug?

I have verified that the custom validation rule method is called. Even removing the ‘return TRUE’ and forcing the rule to return an error (either by returning an error message or FALSE) still does not actually show an error…

Model:

namespace MyModule\Model;

use EllisLab\ExpressionEngine\Service\Model\Model;

class Settings extends Model {

 protected static $_primary_key = 'id';
 protected static $_table_name = 'my_settings';

 protected static $_validation_rules = array(
  'even_number'       => 'validateMultipleOf[2]',    // field value is odd
 );
 
 protected $id;
 protected $even_number;
 
 public function validateMultipleOf($name, $value, $params, $object)
 {
   if ($value % $params[0] != 0)
   {
   return 'This field must be a multiple of '.$params[0];
   }
  return TRUE;
 }
 
}

Controller:

...

$settings = ee('Model')->get('my_module:Settings', $_POST['id'])->first();
  
$settings->set($_POST);

$result = $settings->validate();
  
if ($result->isValid())
{
 $settings = $settings->save();

 ee('CP/Alert')->makeInline('shared-form')
 ->asSuccess()
 ->withTitle('Settings Updated')
 ->addToBody('Settings were updated successfully!')
 ->defer();
   
 ee()->functions->redirect(ee('CP/URL')->make('addons/settings/my_module/settings'));
}
else
{
 $errors = $result->getAllErrors();
   
 $alert = ee('CP/Alert')->makeInline('shared-form')
 ->asIssue()
 ->withTitle('Error - Could Not Update Settings')
 ->addToBody('The following field(s) had errors: ');
   
 foreach($errors as $field => $es)
 {
  $alert->addToBody('<strong>'.lang($field).'</strong>');
  $alert->addToBody($es);  // show array of field errors as bulleted list
 }
    
 $alert->now();
    
 return $this->settings($result);
}

...
       
webslinger's avatar
webslinger
14 posts
7 years ago
webslinger's avatar webslinger

I moved the custom validation rule to another field and it worked. After experimentation, I realized that fields are only validated when the value changes. Perhaps that makes sense, but it was really throwing me! Seems to be working now 😊

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.