Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
A few simple questions about forms and validation (with fieldset)
  • I'm trying out some simple fuel scenarios and was hoping to avoid digging through the source code to find out how things work. This proves to be a bit difficult for some of the form/validation stuff: The fieldset class tells that "form markup is generated when passing the Fieldset instance to a View or by calling build().". I would conclude that this means creating the fieldset in the controller, populating it and then passing it to a view. Something like this in the controller:
    $article_form = Fieldset::factory('article');
    $title_field = $article_form->add('article_title');
    $data['form'] = $article_form;
    $this-response->body = View::factory('welcome/index', $data);
    
    And in the view:
    echo $form;
    

    This echos the form as a literal string though, with characters like < and > changed to lt; and gt;, why is that? Another question is how to add other fields to the fieldset; the $article_form->add functions doesn't seem to have any 'type' parameter and is only meant for input boxes? Or does this need to be done separately through an extra $article_form->form() step? How would that work? Since the documentation of the fieldset class is a bit sparse, i was looking at this external article on forms and validation by Trevor Morris. He describes a similar situation, and explains why he chose to use the form and validation classes directly instead:
    <div class="text email required">
        <label for="contact-email">Email <abbr title="Required">*</abbr></label>
        <?php echo Form::input('email', $validation->input('email'), array(
            'type'     => 'email',
            'id'       => 'contact-email',
            'required' => 'required'
        )); ?>
        <?php echo $validation->errors('email'); ?>
    </div>
    

    This seems straightword enough, but moving some 'controller logic' to the view seems unideal too?
  • The literal string issue seems because of View::$auto_encode. (i'm posting this as a reply instead of editing the first post because the block there keeps stripping the < and > on edits)[code] block there keeps stripping the < and > on edits)
  • For the first question ... You have to disabile auto_encode to views, check config.php file. And you have to use :
    $data['form'] = $article_form->build();
    
    Second question :
    You have to specify a field type into attributes array, like :
    $form->add('fieldname', 'label', array('type' => 'textarea'));
    $form->add('fieldname1', 'label1', array('type' => 'password'));
    ...
    

    I usually mantain the logic into controllers and not in views ( using fieldsets ) but Trevor choice with Form class is not wrong ... That are personal choiches
  • Thanks Davide, placing the logic in the controller seems the cleanest approach, i'll experiment some more with this. :-)

Howdy, Stranger!

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

In this Discussion