Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
fieldset + checkbox + validation + repopulating ?
  • can anybody write a simple example for this please ? i need working with repopulated validated datas (+ optional default value) need something like "set_checkbox" / "set_value" in CI THANKS
  • Just thought I'd mention that repopulating checkboxes and radio buttons is working but only in relatively new builds, so you should download the latest "develop" core code.
  • Mitchell Steinberg wrote on Tuesday 24th of May 2011:
    Just thought I'd mention that repopulating checkboxes and radio buttons is working but only in relatively new builds, so you should download the latest "develop" core code.

    yes, i know but i need example for this all
  • Here's a stripped down version of what I have working. Hope this helps. On a side note, there are some config template variables for formatting the html output (field_template, multi_field_template, required_mark). I've been able to tweak these to generate meaningful html output for all form fields except checkboxes and radio buttons. For checkboxes and radio buttons, I haven't been able to use the templates to print both the field label and the text that goes along side the checkboxes. For example, in the example below for the field "group_id", I'm unable to get both the label "User Type" as well as the text next to the checkboxes, "User" and "Administrator" to print out. If anyone can explain how to change the multi_field_template variable to do that, I'd love to know. Controller:
    Controller_Admin extends Controller
    {
    public function action_add_edit_admin($user_id = null)
    {
    ...
    $view = View::factor('admin/view_admin_add_edit');
    $form = Fieldset::factory('form_admin')->add_model('Model_admin', array($user_id))->repopulate();
    ...
    if ($form->validation()->run() == TRUE)
    {
    $success = Model_admin::register_user($form, $user_id);
    
    if ($success)
    {
    ..
    }
    else
    {
    ...
    }
    ...
    
    $view->set('user_id', $user_id);
    $this->template->content = $view;
    }
    

    Model code:
    class Model_Admin extends Model_User
    {
     public static function set_form_fields(Fieldset $form, $user_id = null
     {
     if ($user_id != null)
      {
       $admin = self::get_info($user_id);
       if (empty($admin))
       {
        return false;
       }
      }
    
      $form->add('first_name',
       'First Name',
       array(
        'type' => 'text',
        'value' => ! empty($admin['first_name']) ? $admin['first_name'] : '',
        'id' => 'first-name',
        'size' => 50,
       ),
       array(
        array('required'),
        array('trim'),
        array('max_length', 50),
       )
      );
    
      $auth_groups = self::get_auth_groups();
    // Generates something like: $auth_groups = array('1' => 'User', '10' => Administrator');
    
      $form->add('group_id',
       'User Type',
       array(
        'type' => 'radio',
        'options' => $auth_groups,
        'id' => 'group-id',
        'value' => ! empty($admin['group_id']) ? $admin['group_id'] : '',
       ),
       array(
       )
      );
    
    
     $form->add('form_cancel', null,
       array(
        'type' => 'submit',
        'value' => 'Cancel'
       )
      );
    
      $form->add('submit', null,
       array(
        'type' => 'submit',
        'value' => 'Submit'
       )
      );
        }
    }
    

    View:
    ...
    echo $form->show_errors();
    
     if (empty($user_id))
      echo '<h1>Create Administrator</h1>';
     else
      echo '<h1>Edit Administrator</h1>';
    
     echo $form->form()->open();
    
     foreach ($form->field() as $index => $row)
    &#123;
      echo $row;
     }
    
     echo $form->form()->close();
    
  • "I haven't been able to use the templates to print both the field label and the text that goes along side the checkboxes." A bit offtopic. I ran into the same problem yesterday and I fixed it locally. I will submit a pull request for the fix.
  • @leo-nard: That would be great if the form printing for checkboxes and radio buttons could be fixed. Thanks.
  • Mitchell Steinberg wrote on Wednesday 25th of May 2011:
    @leo-nard: That would be great if the form printing for checkboxes and radio buttons could be fixed. Thanks.

    https://github.com/fuel/core/pull/151
  • leo-nard: I just downloaded and checked out your code, and it works much better. A couple things I noticed: 1. The "required_mark" is missing from the fields
    2. If I change:
    'multi_field_template'  => "\t\t\t&#123;label}\n &#123;fields}\t\t\t&#123;label}&#123;required} &#123;field}&#123;fields}",
    

    To:
    'multi_field_template'  => "\t\t\t&#123;label}\n &#123;fields}\t\t\t&#123;field}&#123;label}&#123;required} &#123;fields}",
    

    It reverses the checkbox and the checkbox text so that the checkbox is before the text. I usually prefer that order, but maybe it's just me.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion