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

How do I handle a shared form in a custom module?

Development and Programming

Darin's avatar
Darin
7 posts
6 years ago
Darin's avatar Darin

Hello all,

I am attempting to create my very first custom module, but I’m in over my head and need some help. I don’t know much about coding, so the developer docs are only partially helpful to me. Even something as basic as using the Shared Form View is confusing me, as the docs leave gaps where it is assumed the reader fully knows what they’re doing. I, on the other hand, only partially know what I’m doing.

In the CP, I want my module to present the user with a single page containing a form with two fields. At this stage, I don’t need any user configurable settings. The user simply fills out the form and submits it. The module should validate the form and either present the user with any form errors (a general error message on top and specific error messages beside the applicable fields), or process the data and present the user with a success or failure message above a reset form.

Here’s what I’ve got so far. By the way, I couldn’t find any instructions in the docs about how to add buttons (other than the default save settings button) to a shared form, so I hope I did it correctly.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * A simple module to collect and process some data in the EE control panel.
 */

class My_first_module_mcp {
 
 public $name = 'My_first_module'; // Is this variable necessary or used anywhere?
 
 public function index()
 {
  // The data we'll want to populate our form fields with
  // next 3 lines not currently needed (I don't think)
//  $site = ee('Model')->get('Site')
//      ->filter('site_id', ee()->config->item('site_id'))
//      ->first();
  
  // Form definition array
  $vars['sections'] = array(
    array(
      // Name field
      array(
        'title' => 'data_name',
        'desc' => 'data_name_desc',
        'fields' => array(
          'data_name' => array(
            'type' => 'text',
//            'value' => 'Example',
            'required' => TRUE
          )
        )
      ),
      // Details field
      array(
        'title' => 'data_details',
        'desc' => 'data_details_desc',
        'fields' => array(
          'data_details' => array(
            'type' => 'textarea',
//            'value' => 'List the details here',
            'required' => TRUE,
          )
        )
      )
    )
  );
  
  // Submit button
  $vars['buttons'] = array(
   array(
    'name' => 'submit',
    'type' => 'submit',
    'value' => 'Submit', // What is value actually used for here? Is it necessary, or can I remove it?
    'text' => 'Submit',
    'working' => 'Processing...'
   )
  );
  
  // Final view variables we need to render the form
  $vars += array(
    'base_url' => ee('CP/URL', 'addons/settings/my_first_module'),
    'cp_page_title' => lang('my_first_module_name'),
    // There are currently no config settings, so don't use the default save button.
//    'save_btn_text' => 'btn_save_settings',
//    'save_btn_text_working' => 'btn_saving',
    'hide_top_buttons' => TRUE
  );
  
   // This wrecks things, because $result doesn't exist yet. How do I properly validate and shows errors or success?
//  $vars['errors'] = $result;
    
  // The style to enlarge textarea needs a better implementation, this is only temporary. How do I attach this style properly? Using attrs doesn't work, because it attaches to the parent rather than the field itself.
  $styles = '<style>textarea {height:150px;}</style>'; 
  
  // Temporary reminder to update language file.
  $reminder = '<b>Don\'t forget to update the language file!</b>';
  
  return $styles . ee('View')->make('ee:_shared/form')->render($vars) . $reminder;
 }
 
 // Is this what I should be doing? I'm so lost. Help!
 public function process_data()
 {
  $rules = array(
    'data_name' => 'required',
    'data_details' => 'required'
  );
  
  $result = ee('Validation')->make($rules)->validate($_POST);
  
  if ($result->isValid())
  {
    // yay
    // 1. process data - should this be done right here or in another place or file?
    // 2. reset form, showing success or failure message at top
  }
  
  // no :(
  // return to form, showing general error message at top and specific error message(s) beside applicable field(s)
 }
}
// END CLASS

// EOF

Your help is appreciated.

Darin

       

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.