liferay dynamic datalist getfieldvalue for radio and select fields contains cruft - liferay-6

How do I remove the extra [" "] when retrieving DynamicDataList data from LifeRay 6.1.30 using DDLRecordLocalService getFieldValue in a velocity template for radio or select fields.
Here is my VM:
#set ($ddlRecordsUtil = $serviceLocator.findService("com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalService"))
#set ($records = ${ddlRecordsUtil.getRecords($getterUtil.getLong($reserved_record_set_id.data))})
<ul>
#foreach ($record in $records)
<li>
$record.getFieldValue("radio1477"),
$record.getFieldValue("select2047")
</li>
#end
</ul>
Where radio1477 is:
<input name="radio1477" value="arf">arf
<input name="radio1477" value="bark">bark
<input name="radio1477" value="woof">woof
and select2047 is
<select name="select2047">
<option value="arf"> arf </option>
<option value="bark"> bark </option>
<option value="woof"> woof </option>
</select>
Returns the following list:
["arf"], ["arf"]
["bark"], ["bark"]
["woof"], ["woof"]
What is the best way to remove the [" "] cruft?

The best way to receive the field-value is to get the field first and then use getRenderedValue($locale).
...
#set ($fields = $record.getFields())
#set ($myField = $fields.get("myselect"))
#set ($myValue = $myField.getRenderedValue($locale)
...

Don't know if this is the best way but it did remove the [" "] cruft.
#set($arf = $record.getFieldValue("radio1477"))
#set($arflen = $arf.length() - 2)
#if ($arflen > 2)
$arf.substring(2, $arflen)
#end

John,
Retrieve it from serviceContext; something like this should work.
${serviceContext.getAttribute("$field")}
Iterate over the fields of the individual records

Related

Angular2 reactive forms select how to set invalid?

I use reactive forms within my app. In a certain form I want to display a required (Validators.required) select like this:
<select class="form-control"
[id]="dformControl.key"
[formControlName]="dformControl.key"
[multiple]="dformControl.multiple">
<option *ngIf="!dformControl.value"
value="undefined">
Choose ...
</option>
<option *ngFor="let opt of dformControl.options"
[value]="opt.value"
[selected]="dformControl.value == opt.value">
{{opt.label}}
</option>
</select>
The problem is whether I use value="undefined" or value="" the form control still is set to valid because it got a value. Do not present the value attribute results in value="Choose ...".
Am I using select with reactive forms in a false way or how would I be able to make the option "Choose ..." being not valid??
Assigning initial value of select control to null will do the trick. Try below,
model_property = null
....
this.fb.group({
....
'control_key' : [this.model_property, Validators.required]
...
})
Check this Plunker!!, Look into app/reactive/hero-form-reactive.component.ts file.
I updated the Plunker to include below and it seems to be working,
<select id="power" class="form-control"
formControlName="power" required >
// see the value is set to empty,
<option value="">Choose...</option>
<option *ngFor="let p of powers" [value]="p">{{p}}</option>
</select>
Hope this helps!!
What I do is add a blank option and when that is selected since there is no value it is not valid.
<select class="form-control"
[id]="dformControl.key"
[formControlName]="dformControl.key"
[multiple]="dformControl.multiple">
<option></option>
<option *ngFor="let opt of dformControl.options"
[value]="opt.value"
[selected]="dformControl.value == opt.value">
{{opt.label}}
</option>
</select>

Change form action according to select option

I have a simple form:
<form name="simple" id="simple" method="post" action="X.php">
<select name="select">
<option value="none"> Select </option>
<option value="1">1st</option>
<option value="2">2nd</option>
<option value="3">3rd</option>
</select>
</form>
I want X (X.php) to change with option values.
For example, when user selects 1st, it should change to 1.php.
Is it possible?
Yes you can, example with jquery is as follows:
$("#selectID").change(function() {
var action = $(this).val();
$("#simple").attr("action", action + ".php"); // Can also use .prop("action", action + ".php");
});

How use form select in cake php with foreach

I want to fill a form select with informations of a table :"foreach"
i don't find a solution , someone can help me !i use cakephp 2.5.5 .
i want solution like that , but with cake php .
<select id="Select" name="section_id" class="form-control">
#foreach($sections as $section)
<option value="{{$section->id}}">{{$section->section_name}}</option>
#endforeach
</select>
Try this
First fetch data from database like this :
$result = $this->ModelName->findById('id');
$this->set('result',$result);
Then check in view file
<select id="Select" name="section_id" class="form-control">
#foreach($sections as $section)
<option <?php echo ($result['ModelName']['section_id'] == $section->id) ? 'selected="selected"' : '' ?> value="{{$section->id}}">{{$section->section_name}}</option>
#endforeach
</select>
Use Cakephp form helper to create select box so cake automatic display selected data like this
$this->From->select('ModelName.select_box_name',$sections);

Zend Form - setting defaults for multi checkbox

The HTML for my checkbox list is like this:
<dt id="list-label">
<label for="list" class="optional">
Choose which feeds to include in the mix
</label>
</dt>
<dd id="list-element">
<label for="list-1">
<input type="checkbox" name="list[]" id="list-1" value="1">Marko Polo
</label>
<br />
<label for="list-2">
<input type="checkbox" name="list[]" id="list-2" value="2">Jeano Polo
</label>
</dd>
I'm trying to pre-populate them with selected=selected for those with values of 1 from the database. I thought the following would work:
$form->setDefaults(array('list-1'=>1,'list-2'=>1));
But it doesn't. Is there a way to do this?
EDIT
Here is my form class code:
$model = new Admin_Model_Db();
$model->setTableName('graduates');
$gradA = $model->getAllGraduates();
foreach ($gradA as $grad){
if (!empty($grad['twitter'])){
$twitter[$grad['id']] = $grad['firstname'] . ' ' . $grad['lastname'];
}
}
$list = $this->CreateElement('multicheckbox', 'list')
->setLabel('Choose which feeds to include in the mix')
->setRequired(false)
->setMultiOptions($twitter);
Try this:
$all_record = array('k1'=>'v1', 'k2'=>'v2');
$checked_values = aray('k1');
$checkbox = new Zend_Form_Element_MultiCheckbox('id',
array('multiOptions' => $all_records)
);
$checkbox->setValue($checked_values);
// ...
$this->addElement($checkbox);
$this is Zend_Form of course :)
Your form element name is list[], this means that when your boxes are checked and you get their values this way :
$list = $form->getElement('list')->getValue();
$list's value will be array(1,2)
So logically this should work :
$form->setDefaults(array('list'=>array(1,2));
//if not, try with strings instead of integers array('1','2')

How to get value of selected text by tag name, not id

I only have names of tags, no ids. Need to figure out a way to get text of selected dropdowns. This is the HTML:
<SELECT name="selectRightName">
<OPTION value="76" >1</OPTION>
<OPTION value="200" >2</OPTION>
<OPTION value="201" >3</OPTION>
<OPTION value="202" >4</OPTION>
<OPTION value="203" >5</OPTION>
</SELECT>
By some reason this returns empty value:
$(document).ready(function(){
alert(productName);
alert(selectRightName);
$('select[name=selectRightName]').change(onSelectChange);
$('select[name=selectLeftName]').change(onSelectChange);
});
function onSelectChange(){
var fselected = $('select[name=selectRightName] option:selected');
var sselected = $('select[name=selectLeftName] option:selected');
alert(fselected + " " + sselected);
The attribute in your HTML starts with a capital S, but your selector isn't reflecting it, and attribute selectors are case-sensitive everywhere.
var fselected = $('select[name=SelectRightName] option:selected');
Also there's a stray ) in your HTML, not sure what that's doing there but you should remove it.