0
  • My Channel has radios and checkboxes. How to I loop through the options so that I can display each item as a radio/checkbox option in the mark up? The example I found doesn't make much sense to me:
{if radio}
     {options}
          <label class="checkbox">{option_value}
          <input type="radio" id="{field_name}" name="{field_name}" value="{option_value}"{checked}>
          </label>
     {/options}
{/if}
  • SafeCracker outputs a bunch of hidden fields in a div.hiddenFields. It should be in a <fieldset> if it's going to pass validation. Is there a way to make them appear in a fieldset?
  • I'd like to set a class in the <form> tag produced by SafeCracker. Is there a way to do that?
  • How do I display the field value for text, textarea, radio, checkbox, etc? I'd like to display them when the form loads if the values previously exist. And when user submits a form with bad values, I'd like to display the submitted values when the error page is displayed.

EDIT 1: My first question is solved, I think:

{options:gender}
    <label for="{option_value}">
        <input type="radio" name="gender" value="{option_value}" id="{option_value}">
        {option_value}
    </label>
{/options:gender}
4
  • You may want to use the {checked} variable make sure the radio buttons are checked properly on edit and when validation fails. Dec 15, 2012 at 15:18
  • Just create a new question for each separate question. It allows the best answer for each question to be selected, rather than people having to post answers which only answer part of your question. Dec 15, 2012 at 18:27
  • @JustinKimbrell - thanks for the {checked} tip. Dec 18, 2012 at 12:42
  • @AdrianMacneil - will do! Dec 18, 2012 at 12:44

2 Answers 2

2

Q1: what's not clear about the example?

Q2: Form elements can be children of divs, that's fine. They just can't be directly children of the <form> element (although I have a feeling that HTML5 might be a bit more relaxed about that).

Q3: The SafeCracker form has a class parameter.

Q4: Examples for custom fields are here.

4
  • Q1: what is this {if radio}? Also, the syntax that worked does not look like the example I initially gave. Q2: field elements should be in a <fieldset>. Dec 15, 2012 at 13:37
  • @ExpressionEngineNewbie I disagree your comment on the fieldset. Given that they are hidden fields used for programmatic purposes and should not need validating (with JS or W3C). You can have hidden input elements in div in a form. Dec 15, 2012 at 15:26
  • I would've thought {if radio} was pretty clear: if it's a radio input, then do whatever you put inside the conditional. Q2: that's not correct; it's not mandatory. Form inputs just have to be contained within another block level element. I've validated 100s of forms that don't have fieldsets. There's even an example in the spec which shows a form with everything wrapped in paragraph tags.
    – Tyssen
    Dec 15, 2012 at 21:59
  • @Tyssen - if what is a radio input? Is this syntax for something else? Plus, that's snippet of code did nothing for me. Dec 15, 2012 at 22:25
1

My suggestion with SafeCracker is to not rely on the {exp:safecracker} tag to output your final form. Instead use it to get your initial HTML output, paste that into your template and customize as needed. This way you can change the hidden fields to have the as you want and will have 100% control over your form fields and HTML layout. This option offers the most flexibility in my opinion.

You'll need to use this Global Variable to print out the XID hash in your template. This will be one of the fields in the hidden group.

7
  • How can you use safecracker without it outputting a form tag? Dec 16, 2012 at 5:45
  • 1
    Yes, this is how I use SafeCracker. I use the {exp:safecracker} tag to output the initial html/form code, then I put that code into my template and adjust and fine-tune as needed.
    – Anna_MediaGirl
    Dec 16, 2012 at 6:03
  • Gotcha. What don't you have control over when using the tag itself though? Dec 16, 2012 at 13:19
  • Can't remember why I started doing it this way actually. Most likely issues inside the custom_fields loop.
    – Anna_MediaGirl
    Dec 16, 2012 at 15:55
  • 1
    That's what I do too.
    – Tyssen
    Dec 19, 2012 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.