Jump to content

Insert a paragraph inside a form built using API


enricob
 Share

Recommended Posts

Hi all,

I am building a page that contains a quite complex request form.

To do this, I created a page based on a "contact-form" template, and inside this template I put all the code needed to display the form and manage the results.

The form is built using PW API, the code is inspired from some examples I found here on the forum.

My problem is that I don't know how to insert a text paragraph between the fields, anyone has idea on how to do this?

Here's a very simplified version of my code (only two fields displayed), in this case I would like to append a custom paragraph (simple html) between Company and Name fields.

The only solution I found is to use an additional field of Fieldset type, without content, and on the label I set the text I need to output. However in the label field I am not able to insert html tags...

Any help is very appreciated... Thank you!

// create a text input
$field = $modules->get("InputfieldText");
$field->label = "Company";
$field->attr('id+name','company');
$field->required = 1;
$form_fields[] = $field;

// create a text input
$field = $modules->get("InputfieldText");
$field->label = "Name";
$field->attr('id+name','name');
$field->required = 1;
$form_fields[] = $field;

// submit button
$submit = $modules->get("InputfieldSubmit");
$submit->attr("value","Send");
$submit->attr("id+name","submit2");
$submit->skipLabel = '4';

// create the form field (also field wrapper)
$form = $modules->get("InputfieldForm");
$form->action = "./";
$form->method = "post";
$form->attr("id+name",'contact-form');
foreach ($form_fields as $field) {
  $form->append($field);
}
$form->append($submit);
// append the form in $out variable
$out .= $form->render();
  • Like 1
Link to comment
Share on other sites

Sorry to highjack the thread, but I had build a few forms yesterday and had the same problem. What I noticed is, that each field gets inserted in the same wrapper-elements, even the InputfieldMarkup. Is there a way to exclude some fields from getting these wrapper-elements?

  • Like 1
Link to comment
Share on other sites

@adrian

It is nice we can ask such questions here and get the perfect answers. But how do you know the answers? Is it just experience or you quickly look up somewhere (docs, source code, etc.)? Could you explain that in detail?

Teach me how to fish (but share some fish also :)).

  • Like 6
Link to comment
Share on other sites

That is experience-based. Of course, I have seen that Soma's topic about making contact form from the PW API, so I know what this one is about. But how do you answer question like your's last? The only method working for me is reading and finding something smart guys wrote (hope it helps with your question). But how do they know? I guess? Ryan did not tell'em personally.

So I want recomendations how to find answers myself. I think it will end up with something like "learn PHP and read the code", but maybe something more specific could be said here like "in PW you should find xxx files in wire/xxx and look for certain methods" or something.

Link to comment
Share on other sites

Ivan,

Things like this are a bit more advanced, so there might not be too much documentation (outside the forum topic you linked to above). At least not yet.

In the meantime, I find I learn a lot by looking at the modules in the /wire/ folder. It gives you some idea of the things you can tap into.

I also look at the source for just about every non-core module to see what other devs are doing — you can learn a lot (quickly) that way.

  • Like 4
Link to comment
Share on other sites

@adrian

It is nice we can ask such questions here and get the perfect answers. But how do you know the answers? Is it just experience or you quickly look up somewhere (docs, source code, etc.)? Could you explain that in detail?

Teach me how to fish (but share some fish also :)).

Ivan,

I have learned things in lots of different ways. Some from searching/browsing the forum, some from studying the core code, some from looking at other's modules, sometimes looking back at my own modules. I sometimes have a hard time remembering how to do something, but I usually remember where I did it or where someone else did it, or at least the right term to search for.

In this example, I have used Markup a couple of times in my modules now, but the first time I went to do something like that, I remembered that Hanna Code has a huge chunk of text under "PHP and Javascript usage notes" on the Code tab, so I went and looked how it was done!

  • Like 7
Link to comment
Share on other sites

Thank you adrian!!!

I did take a look at /wire/modules but I was expecting something like InputFieldHtml or InputFieldParagraph or something like that :)

Anyway I am too interested in what LostKobrakai asked...How can I exclude a field from being included in the wrapper element? Or can I at least customize the class of the wrapper element for a specific field?

Link to comment
Share on other sites

I got all inspired yesterday and got to the source, which is quite well documented and is probably an easy reading for those capable of that (not me yet). Could not find any options which could be configured to exclude certain field from being rendered as others. I guess you have to hook to InputfieldWrapper ___render() method and do some conditional formatting there. But it is only this far I could get :(.

Link to comment
Share on other sites

Anyway I am too interested in what LostKobrakai asked...How can I exclude a field from being included in the wrapper element? Or can I at least customize the class of the wrapper element for a specific field?

You can customize the wrapper code for the whole form, which is described here: https://processwire.com/talk/topic/2089-create-simple-forms-using-api/?p=39436

To add a class to one field use this: https://processwire.com/talk/topic/2089-create-simple-forms-using-api/?p=39375

To learn a lot more about the form api, read the whole thread :)

What I want to know, if there's a way to only change the wrapper for a single field instead of the whole form.

  • Like 1
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

×
×
  • Create New...