bwakad Posted June 16, 2014 Share Posted June 16, 2014 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 More sharing options...
adrian Posted June 16, 2014 Share Posted June 16, 2014 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 More sharing options...
bwakad Posted June 16, 2014 Author Share Posted June 16, 2014 Well, the code from your answer is working perfectly. Many many thanks! It's amazing how you guys come up with these great snippets ;-) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now