Can we use 2 or more "textarea" fields using SafeCracker?
{exp:safecracker channel="letters_to_editor" return="site/thank_you" entry_id="{segment_3}" status="Closed"}
<p><label for="Subject">Subject:</label><br />
<input type="text" name="title" id="title" value="{title}" size="50" maxlength="100">
{custom_fields}
{if textinput}
<p><label for="Name">Your Name:</label><br />
<input type="text" name="{field_name}" value="{field_data}" id="{field_name}" size="50" /></p>
{/if}
{if textarea}
<p><label for="letter-content">Your Question:</label><br />
<textarea id="{field_name}" name="{field_name}" 开发者_如何学Pythonrows="10" cols="50">{field_data}</textarea></p>
{/if}
{/custom_fields}
<p><input type="submit" value="Submit"></p>
{/exp:safecracker}
What i want is for the visitors to enter:
- Subject
- Their Name
- Their Question
I want all of these fields to be filled in the backend, so i have the following fields in the backend:
- Subject = {title}
- Name = {questioners_name} ==> Text Input
- Question = {question_bodytext} ==> Text Area
I need another Text Area in the Backend
- Answer = {answer_bodytext} ==> Text Area
But when i create 2 of any fields, they [the fields] appear twice in the Form display on the site.
Where the SafeCracker is only asking for the Subject, Name and Question, it also adds another textarea for the Answer, even though i have not added it in the form. It displays the text area twice.
How to solve this issue ?
When you use the {custom_fields}
tag pair, SafeCracker will always loop through all of the custom fields. What you need to do in this case is just code-up the individual fields using their field names.
{exp:safecracker channel="letters_to_editor" return="site/thank_you" entry_id="{segment_3}" status="Closed"}
<p>
<label for="title">Subject:</label><br />
<input type="text" name="title" id="title" value="{title}" size="50" maxlength="100" />
</p>
<p>
<label for="questioners_name">Your Name:</label><br />
<input type="text" name="questioners_name" value="{questioners_name}" id="questioners_name" size="50" />
</p>
<p>
<label for="question_bodytext">Your Question:</label><br />
<textarea id="question_bodytext" name="question_bodytext" rows="10" cols="50">{question_bodytext}</textarea>
</p>
<p><input type="submit" value="Submit" /></p>
{/exp:safecracker}
D-Rock is correct, when you use the {custom_fields}
tag pair, SafeCracker will loop through and display all of the custom fields for the specified channel.
Depending on how you're using SafeCracker, this can have positive and potentially huge time saving features.
For example, if you're creating a registration or event form with dozens of custom fields, having them all output automatically is pure bliss. However, if your needs are simpler, having a few unwanted custom fields output can be frustrating.
A lazy way of fixing this problem is to allow all of the custom fields to be output dynamically, but selectively hide the unwanted fields with CSS.
Consider the following SafeCracker code:
{exp:safecracker channel="letters_to_editor" return="site/thank_you"}
<p class="title">
<label for="Subject">Subject</label><br />
<input type="text" name="title" id="title" />
</p>
{custom_fields}
{if textinput}
<p class="{field_name}">
<label for="{field_name}">{field_label}</label><br />
<input type="text" id="{field_name}" name="{field_name}" />
</p>
{/if}
{if textarea}
<p class="{field_name}">
<label for="{field_name}">{field_label}</label><br />
<textarea id="{field_name}" name="{field_name}"></textarea>
</p>
{/if}
{/custom_fields}
<p><input type="submit" value="Submit"></p>
{/exp:safecracker}
Which would output the following HTML (simplified, for clarify):
<form method="post" action="#">
<p class="title">
<label for="Subject">Subject</label><br />
<input type="text" name="title" id="title" />
</p>
<!-- Start of Dynamic Custom Fields -->
<p class="questioners_name">
<label for="questioners_name">Name</label><br />
<input name="questioners_name" id="questioners_name" type="text" />
</p>
<p class="question_bodytext">
<label for="question_bodytext">Question</label><br />
<textarea id="question_bodytext" name="question_bodytext"></textarea>
</p>
<p class="answer_bodytext">
<label for="answer_bodytext">Answer</label><br />
<textarea id="answer_bodytext" name="answer_bodytext"></textarea>
</p>
<!-- End of Dynamic Custom Fields -->
<input type="submit" value="Submit" />
</form>
With a single CSS rule you could hide the Answer Textarea from appearing in the form:
<style>
.answer_bodytext { display: none; }
</style>
The powerful key to this approach is to leverage the <p class="custom_field_name">
and use it as a CSS Hook to hide the field(s) you don't want to display.
Understand, this is an extremely lazy way of tackling the problem and won't actually remove the form elements from the page. Instead, it's a low-tech way of selectively hiding specific custom fields without having to manually hard-code the entire form — perfect for time-sensitive changes or lazy programmers!
精彩评论