Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Sets of checkboxes
  • Hello!

    Based on this http://net.tutsplus.com/tutorials/php/easy-form-generation-using-fuelphp article I've created a form. In this form there are many checkboxes.I want to group them. For example:

    Now the form looks like
    checkbox1
    checkbox2
    checkbox3
    checkbox4
    checkbox5
    checkbox6
    checkbox7
    checkbox8
    checkbox9

    What can I do, to make form:

    Set of checkboxes1
    checkbox1
    checkbox2
    checkbox3


    Set of checkboxes2
    checkbox4
    checkbox5


    Set of checkboxes
    checkbox6
    checkbox7
    checkbox8
    checkbox9

    If it is need to know, I use ORM

    Thank you very much.

    Sorry for my English.
  • HarroHarro
    Accepted Answer
    I've quickly glanced at that tutorial.

    It does the fieldset generation in the controller (using the build() method), and passing the generated HTML as one single block to the view. To generate custom form layouts, you can not use the build() method anymore.

    Instead, pass the fieldset object to the view, and in the view, do something like

    <?php echo Form::open(); ?>
    <fieldset>
        <legend>Set of checkboxes1</legend>
        <?php echo $fieldset->field('box1')->build(); ?>
        <?php echo $fieldset->field('box2')->build(); ?>
        <?php echo $fieldset->field('box3')->build(); ?>
    </fieldset>
    <?php echo Form::close(); ?>


  • Thank you for answer, everything is fine!
    I have one more simple question:
    $this->template->set('fieldset', $fieldset, false);

    Is it only one way to pass the fieldset object to the view? (I'm afraid to use the third parameter "false")
  • I'm not sure if it's required to disable encoding on a Fieldset object. Change the false to true and you'll find out. ;-)
  • Thanks for your answers!

    http://fuelphp.com/docs/general/views.html

    Here press ctrl+f and enter the next string (it will be on a yellow background):
    Note on objects

    Here you can see my example.
    So, if I change the false to true I recieve
    ErrorException [ Error ]: Call to a member function field() on a non-object

    That's why I asked this question.
  • That would suggest another issue. Encoding doesn't destroy objects.

    Are you sure you're passing the fieldset object, and not the result of a build() call to your view?
  • In controller:

     function action_add()
        {
            $fieldset = Fieldset::forge()->add_model('Model_Advert')->repopulate();
    ...
            $this->template->set('fieldset', $fieldset, false);
        }

    and in view

     <?php echo Form::open(); ?>
     <?php echo $fieldset->field('field_id')->build(); ?>
    <?php echo Form::close(); ?>

    Are you sure you're passing the fieldset object, and not the result of a build() call to your view?
    So, I gues, not... but I'm not sure
  • That looks ok, and should work without problems. I do this in every app.

    Your model contains a property called 'field_id'?
  • No, it was just for example.
    If I set the third param. false - that is OK - the form is shown as needed, if true -
    ErrorException [ Error ]: Call to a member function field() on a non-object
    as I wrote above
  • if 3-d param = true.

    <?php echo Form::open(); ?>
        <?php echo $fieldset; ?>
    <?php echo Form::close(); ?>


    This code in view shows in browser code of the form, and in source we can see for example
    &lt;td class=&quot;&quot;&gt;&lt;label id=&quot;label_skype&quot; for=&quot;form_skype&quot;&gt;skype&lt;/label&gt;&lt;/td&gt;

  • Just checked the code.

    Objects can't be encoded unless they have a __toString() magic method, or implement Iterator or ArrayAccess, or are an instance of stdClass.

    Since a fieldset object has the magic method, encoding it triggers that method, which causes build() to run on the fieldset, and a string containing HTML to be passed to the View.

    So yes, either use false do disable encoding on set(), or whitelist the Fieldset class in the config.

    From a security point of view it should not be a problem, the Fieldset should not contain unvalidated data, other then user input which will be re-displayed in a form field.
  • Thank you very much! I'm very pleasure to learn FuelPHP with you! You are great!
  • Thanks for the kudos. That keeps us going!

Howdy, Stranger!

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

In this Discussion