Jump to content

simple forms question


bwakad
 Share

Recommended Posts

No matter what I searched about forms, I ended up to an old topic of Soma (with a lot of participation from forum members)...

But I liked the approach of building a form like this since the code is easy to read.

I did try to make a select field as this, and this shows me an empty select field:

// create a select input
$field = $modules->get("InputfieldSelect");
$field->label = "Contract";
$field->attr('id+name','name');
$field->required = 1;
$form->append($field); // append the field to the form

I added this part in between, based on code found on one of the seven pages in mentioned topic:

$contract = $pages->get("/contract/")->children();
    foreach ($contract as $p) {
    $field->addOption($p->id,$p->title);
    }

But now, I am stuck on this part, that I also found on one of these pages. Is should be a default value for a custom select. But I can't really find out how to change this to use on my not-so-custom select:

$field->attr("value", $input->post->cms ? $input->post->cms : 'pw');

But wait, there's more... I use this code so I can get to my field values (if posted):

$member_page = $pages->get("template=member,name={$user->name}");
// now I can get a value like: $member_page->get('contract');

And the $field->attr should either be a value from the member_page contract field, or if none, display a simple text: "please select".

Right now, without this attr. it defaults to the first select value as selected...

Link to comment
Share on other sites

Before your foreach, add this:

$field->addOption('Please Select');

Then to force it to automatically select the member_page contract field do:

if($member_page->get('contract')->id) $field->attr("value", $member_page->get('contract')->id);
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...