Custom module settings validation

Is there a way to validate the values of settings (form fields) of a custom module when you hit save button? I am using the ConfigurationModule.

Thanks

Comments

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator
       public function settingsController_myPlugin_create($sender) {
           $sender->permission('Garden.Settings.Manage');
           $sender->title(Gdn::translate('MyPlugin Settings'));
           $sender->setHighlightRoute('dashboard/settings/plugins');
           $sender->description(Gdn::translate('MyPlugin Description'));
    
           $sender->Form = new Gdn_Form();
           $validation = new Gdn_Validation();
    
           if ($sender->Form->authenticatedPostBack()) {
               $formPostValues = $sender->Form->formValues();
               // Some examples using validations routiness.
               $validation->applyRule('Email', ['Required', 'Email']);
               $validation->applyRule('Password', 'Required');
    
               // Do custom validation here
               if ($formPostValues['Password'] === 'password') {
                   // Add a custom message.
                   $validation->addValidationResult('Password', 'Please don\'t use "password" as your password, Stupid!');
               }
               // Insert all validation results to the form.
               $sender->Form->setValidationResults($validation->results());
           }
    
           $configurationModule = new ConfigurationModule($sender);
           $configurationModule->initialize([
               // Your config here
           ]);
           $configurationModule->renderAll();
       }
    


    rloyola
  • Thank you so much

Sign In or Register to comment.