Set maximum length for field in joomla - joomla

I want to set max length from default.php.
Here is my XML format,
<fieldset name="sender_details">
<field name="sendername"
type="text"
label="SENDERV_SENDER"
description="SENDERV_SENDER_DESC"
default=""
required="true" />
</fieldset>
Here is the coding for default.php.
foreach ($this->form->getFieldset('sender_details') as $field) :
echo $field->label;
echo $field->input;
}
I had a variable $length=5.The text box should allow only 5 characters.The length is coming from the configuration. I need to do in default.php
kindly Help Me.

To specify max length in Joomla Text Field Like This
<fieldset name="sender_details">
<field name="sendername"
type="text"
label="SENDERV_SENDER"
description="SENDERV_SENDER_DESC"
default=""
maxlength="5"
required="true" />
</fieldset>
if you want your custom attribute then you can create joomla new field type or you can modify joomla form fields
libraries/joomla/form/fields/text.php
libraries/joomla/form/fields/textarea.php
libraries/joomla/form/fields/.......
any problem implementing please reply

The problem with editing core-files (libraries/joomla/form/fields/text.php, libraries/joomla/form/fields/textarea.php, libraries/joomla/form/fields/...) is, that with the next joomla-update these changes could be overwriten again.
Until joomla does noch correct this bug, is there any possibility to set a custom html-attribute to a jformfield before it renders with "print $field->input;" ?
edit: There already exists a bug-entry for that: http://issues.joomla.org/tracker/joomla-cms/3510?lang=de-DE

You can set in edit layout like this :
<?php echo $this->form->getInput('fieldname','maxLength',4); ?>
And then your html code is :
<input type="text" name="jform[fieldname]" id="jform_fieldname" maxlength="4">

Related

How to set default value in thymeleaf th:field

I have a form and I want to set default value in the field below but it's not working.
<span>ID User:</span>
<input type="text" th:value="${session.name}" th:field="*{userid}" th:errorclass="field-error" />
</div>
<div>
<span class="name-in">ID Room:</span>
<input type="text" th:value="${id}" th:field="*{room}" th:errorclass="field-error" />
</div>
I read some topic about this problem and I try to change as given below
th:attr="value = ${session.name}"
But It's still not working. Field ID User is empty. I don't know how to solve this problem.
Although your question contain less information, but i think you want to put default value for all field. If you like to do so change
`<input type="text" th:value="${session.name}" th:field="*{userid}" th:errorclass="field-error" />`
to
<input type="text" name="userid" value="as your wish" th:errorclass="field-error" />
Instead of changing the html, you should instead set the value of *{userid} in your controller. That way you can keep your html the same:
// Controller
modelObject.setUserId(session.name);
// HTML
<input type="text" th:field="*{userid}" th:errorclass="field-error" />

Form validation on radio buttons Codeigniter

I am trying to add validation for radio buttons on a html form in Codeigniter. The form is prepopulated with data from a database, including a list of uploaded documents (this field may be empty if the item has no associated docs). The user can upload a new document, adding it to the existing docs or adding a new doc deleting the current ones.
So I have a text field containing the org doc names and a file upload field for new:
<label for="orgdocs">Documents</label>
<input type="text" id="orgdocs" readonly="readonly" value="<?php echo $fetched_row['pdocs']; ?>" />
<input type="file" id="newdocs" name="newdocs[]" multiple="multiple" />
and radio buttons: (ignore the bad attempt at Spanish names)
<label for="mas"><b>Añada mas</b></label>
<input type="radio" name="docsacion" style="margin-right: 0" <?php if (isset($docsacion) && $docsacion=="mas") echo "checked";?> value="mas" title="Add another document to existing docs"><br />
<label for="otra"><b>Borrar y añada otra</b></label>
<input type="radio" name="docsacion" style="margin-right: 0" <?php if (isset($docsacion) && $docsacion=="otra") echo "checked";?> value="otra" title="Remove all current docs and add new">
I just want to add validation. IF a new document has been selected(newdocs is not empty), dosacion is required.
I have tried:
if(isset($_FILES['newdocs']['name']) && (!empty($_FILES['newdocs']['name'])))
{$this->form_validation->set_rules('docascion','Documentation upload', 'required');}
but this gives the error even if the newdocs field is empty and I´ve no idea why!?
Try this
if(!empty($_FILES['newdocs']['name'])){
$this->form_validation->set_rules('docascion','Documentation upload', 'required');
}

How can I use a calendar field in a repeatable field in a Joomla form?

I would like to allow users to be able to enter multiple dates, and was hoping I could use the repeatable form field type (new as of Joomla 3.3).
I do get the modal popup with rows to add/remove items, however the calendar icon does not popup a calendar picker. I tried with just a text input and that seems to work ok.
The relevant part of my form definition:
<field
name="event_dates"
type="repeatable"
id="event_dates"
icon="calendar"
label="Event calendar dates"
select="Select dates"
>
<fields name="jmfields_event_dates">
<fieldset
name="event_dates_modal"
repeat="true"
hidden="true">
<field
name="event_date"
type="calendar"
format="%d-%m-%Y"
label="Date"
/>
</fieldset>
</fields>
</field>
I haven't tried to have a calendar field inside the Repeatable field, but Repeatable field is too buggy yet with a bunch of javascript issues/conflicts. Check in your browser error console for any such issues. I would suggest to wait till Repeatable Field becomes more stable.
I also wanted to do the same, and instead of the default calendar field I end-up using the jQuery date picker on a normal text field instead. You add the jQuery script on the page to target the input field and you have a little work around until the actual fix for the default calendar is out.
You need these files in your header:
JHtml::_('jquery.framework');
JHtml::_('jquery.ui');
$doc =& JFactory::getDocument();
// loaded from the code.jquery.com site
$doc->addStylesheet('http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css');
// I downloaded the datepicker only and placed it in this folder
$doc->addScript(JURI::root().'/media/jui/js/datepicker/jquery-ui.min.js');
The xml for the field:
<!-- Course_date Field. Type: Repeatable. (joomla) -->
<field
type="repeatable"
name="course_date"
label="Course Dates"
description="COM_LEARNINGMANAGER_EVENT_COURSE_DATE_DESCRIPTION"
id="course_date"
class="course_dates"
select="COM_LEARNINGMANAGER_EVENT_COURSE_DATE_SELECT"
icon="list"
maximum="50">
<fields name="course_date_fields" label="">
<fieldset hidden="true" name="course_date_modal" repeat="true">
<!-- Course Field. Type: Courses. (custom) -->
<field
type="courses"
name="course"
label="COM_LEARNINGMANAGER_EVENT_COURSE_LABEL"
class="list_class"
button="false"
/>
<!-- Date Field. Type: Text. (joomla) -->
<field
type="text"
name="date"
label="COM_LEARNINGMANAGER_EVENT_DATE_LABEL"
size="20"
maxlength="50"
description="COM_LEARNINGMANAGER_EVENT_DATE_DESCRIPTION"
class="text_area datepicker"
readonly="false"
disabled="false"
filter="STRING"
message="Error! Please add date here."
hint="COM_LEARNINGMANAGER_EVENT_DATE_HINT"
/>
</fieldset>
</fields>
</field>
Here is the script you need to add to the default.php or edit.php file:
<script type="text/javascript">
// means your repteable field can only take 50 rows
<?php $fieldNrs = range(1,50,1); ?>
jQuery('input.form-field-repeatable').on('row-add', function (e) {
<?php foreach($fieldNrs as $nr): ?>
jQuery('#jform_course_date_fields_date-<?php echo $nr ?>').datepicker(
{
minDate: -1,
prevText: '',
nextText: '',
maxDate: '+3M',
firstDay: 1,
dateFormat: 'yy-mm-dd',
onSelect: function(dateText, inst) {
jQuery('#jform_course_date_fields_date-<?php echo $nr ?>').val(dateText);
}
});
<? endforeach; ?>
});
</script>
Enjoy!
You can try with this ("Multiplier module"). I used different approach to get same result like with Joomla repeatable form field type, and also with that avoid "pop-ups inside popup". Still in development phase, but maybe you can try with that concept. Regards.
What I did to solve similar problem?
I had a similar problem with default Joomla repeatable form field type, especially with "pop-up" form fields type (eg. Joomla "date"). Because of that I created some kind of module pattern (mod_multiplier) where I used different approach -> I wouldn't repeat form of fields (section), I want to use same section for multiple insertion.
Module mod_multiplier
Inside module xml file are 3 reserved fields "wrapper", "repeater", and "content".
Inside "wrapper" is the place for "repeatable" fields:
<fieldset name="basic" addfieldpath="/modules/mod_multiplier/models/fields">
<!-- Wrapper is container for repeatable fields-->
<fields name="wrapper">
<!--This is the place where you insert your fields-->
</fields>
<field name="repeater" label="" type="repeater" />
<field name="content" hidden="true" label="Content" type="hidden" />
</fieldset>
All content would be stored inside "content" field in JSON format. Every time when we press form "Add" button we will add one row of data to "content" field. After all, inside "content" we will have rows of data.
How that look inside tmpl/default.php file and how to get fields values?
Hierarchical data structure has 3 levels: rows, row and field.
all data => $rows
one row of data => $row
one field => $row->field_name
Practical example from mod_multiplier:
Inside "wrapper" are fields "country", and "city" and we call them by name (inside tmpl/default.php) like this:
<ul>
<?php foreach ($rows as $row):?>
<li>
<?php echo $row->country;?>:<?php echo $row->city;?>
</li>
<?php endforeach; ?>
</ul>

String concatenation with ASP.NET MVC3 Razor

i'm trying to concatenate a string in asp.net mvc 3 razor and i'm getting a little sintax problem with my cshtml.
i what to generate an id for my checkboxes on a foreach statement, and my checkboxes should start with "chk" and what to cancatenate a fieldon the ID, something like that:
<input type="checkbox" id="chk+#obj.field" />
but or exampple the result for id attribute is: id="chk+8"
how can i just get a result for something like "chk8"?
Just put your variable next to prefix:
<input type="checkbox" id="chk#(obj.field)" />
Try
<input type="checkbox" id="#("chk" + obj.field)" />
or
<input type="checkbox" id="chk#obj.field" />
<input type="checkbox" id="chk#(obj.field)" /> should work.
The most direct and clean way to add a prefix a suffix.
#("PREFIX " + obj.field + " SUFFIX")
<input type="checkbox" id="chk#(obj.field)" /> should work.
Best way to concate any C# variable in rozer view by using string.Format
id="#string.Format("{0}_Title", _Id)" // Apend after
id="#string.Format("Title_{0}", _Id)" // Apend before
id="#string.Format("Title_{0}_Title", _Id)" // Apend Middle

MVC3 Validation on edit

I am having a problem with my client side validating. I am using DataAnnotations in my model. An this works when i am in the create form very nicely, but when i go to edit the information only some of the validation works.
i.e the Name box does have clientside val but the amount box does.
i have check the source name doesnot include the data-val="true". I don't understand why this would render this way because on the 'create' form it does and works fine?
<input class="text-box single-line" id="Name" name="Name" type="text" value="name" />
<span class="field-validation-valid" data-valmsg-for="Name" data-valmsg-replace="true"></span>
<input class="text-box single-line" data-val="true" data-val-number="The field Amount must be a number." data-val-required="The Amount field is required." id="Amount" name="Amount" type="text" value="120.00" />
<span class="field-validation-valid" data-valmsg-for="Amount" data-valmsg-replace="true"></span>
can somebody give me an idea how to make my 'edit' page validation work?
Thanks in advance
thank you for the help. but is have solved it!!
The problem was that because i was generated the edit view from the wrong class which meant that the DataAnnotations was not being read. what is have done now is to change the name of the class to that of my database then mark the class as partial and connected the DataAnnotaions by using this code below
[MetadataType(typeof(Budget_Validation))]
public partial class Budget
I put all the dataAnnotaions in the Budget_Validation class.
All now working fine!
Thanks

Resources