Thread

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

September 10, 2017 6:12pm

Subscribe [1]
  • #1 / Sep 10, 2017 6:12pm

    webslinger

    14 posts

    (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);
    }
    
    ...
  • #2 / Sep 11, 2017 5:22pm

    webslinger

    14 posts

    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 smile

ExpressionEngine News

#eecms, #events, #releases