How to get current selection value from cake php FormHelper - php

I need to modify some code in cakePHP and the last thing that is holding me still is my inability to get current value from input form and send it to my jquery script.
The forms look like this:
<?php echo $this->Form->input('country', array('options' => $countriesOption, 'onchange' => 'getHotels(this.value, $(\'showSingleInput\').checked);', 'style' => 'margin: 10px;', 'div' => false, 'label' => false, 'error' => 'false', 'hiddenField' => false, 'id' => 'countryInput')); ?>
<?php
if($regionOption != null){
echo $this->Form->input('region', array('options' => $regionOption, 'onchange' => 'getHotelsInRegion(this.value,this.value, $(\'showSingleInput\').checked);', 'style' => 'margin: 10px;', 'div' => false, 'label' => false, 'error' => 'false', 'hiddenField' => false, 'id' => 'regionInput'));
}?>
The problems is at second input form 'region'.
I need to pass as first value to JQuery method currently selected value from 'country' form input.
And all the hard stuff I need to do is here:
'getHotelsInRegion(this.value,this.value, $(\'showSingleInput\').checked);'

Something like
$('#ModelNameFieldName').change(function() {
alert($(this).val());
});
should to the trick.

Related

How do i get xeditable's content in js

I have below code of xeditable:
$this->widget('editable_ori.Editable', array(
'url' => $this->createUrl('index'),
'pk' => Yii::app()->user->id,
'name' => 'index',
'type' => 'text',
'text' => $data,
'placement' => 'top',
'showbuttons' => 'bottom',
'send' => 'auto',
));
I would like to get the content of this field in my js.
I have tried to grab content of this in js using below code. But its not working
$(".editable editable-click").text;
You are missing a . before the class name editable-click.
You have an extra space after the class name .editable.
The method is text() in jquery not text property to get the text.
Change
$(".editable editable-click").text;
to
$(".editable.editable-click").text()
EDIT
You should call the script after the editable has loaded you can use the event onInit to trigger your javascript function from another file which is dependent on the editable input. For instance, if you have a function
function myFunction(){
//do something
$(".editable.editable-click").text()
//do something more
}
then you should try calling that function from the onInit event like below
$this->widget('editable_ori.Editable', array(
'url' => $this->createUrl('index'),
'pk' => Yii::app()->user->id,
'name' => 'index',
'type' => 'text',
'text' => $data,
'placement' => 'top',
'showbuttons' => 'bottom',
'send' => 'auto',
'onInit' => 'js:function(){myFunction();}'
));
Try using onShown option of editable which will execute as soon as the editable form is shown. You can refer the code below.
$this->widget('editable_ori.Editable', array(
'url' => $this->createUrl('index'),
'pk' => Yii::app()->user->id,
'name' => 'index',
'type' => 'text',
'text' => $data,
'placement' => 'top',
'showbuttons' => 'bottom',
'send' => 'auto',
'onShown' => 'js: function() {
var txtVal=$(this).data("editable").value; // This will hold your textbox value as soon as it opens.
someFunction(txtVal); //Call your JS function with textbox value as parameter.
}'
));
Then in your JS function, you can do so:
<script>
function someFunction(txtVal)
{
//Do anything with editable text box value
}
</script>
Hope this helps you.

make specific value selected in dropdown fields cakephp 3.0

In select drop down, make one particular value selected from values that are coming in drop down list Cakephp3.0. i am using below code:
$abc = 'india';
echo $this->Form->input('location_id',
['empty' =>'Please select', 'required'=>'required', 'class' => 'form-control', 'label'=>false]);
Name of countries are coming in drop down list, but i want to make specific value selected which is set as variable $abc (i.e india).
Try this code :
use the 'default' key in
$abc = array('1' => 'One','2'=> 'Two');
echo $this->Form->input('location_id',
'options' => $abc,
default' =>$abc[1],
['empty' =>'Please select',
'required'=>'required',
'class' => 'form-control',
'label'=>false]
);
where $abc[0] is the key of the item you want as selected
like this :
$options = array('M' => 'Male', 'F' => 'Female');
echo $this->Form->select('field', $options, array('default' => 'F'));
Use value option of form input helper. Like this :
$selected = 'india';
echo $this->Form->input('location_id', ['empty' =>'Please select', 'required'=>'required', 'class' => 'form-control', 'label'=>false, 'value'=> $selected]);
Suggestion - Read the docs once and then start the development. You will never get stuck at such simple problems. And refer docs first whenever you get stuck at anything.
echo $form->input('field_name', array(
'type' => 'select',
'options' => $arrayOfOptions, // typically set from $this->find('list') in controller
'label'=> 'Label name here',
'value' => $arrProjectLeaderDetails['id'], // specify default value
'escape' => false, // prevent HTML being automatically escaped
'error' => false,
'class' => 'input_select_medium'
));
In cakephp4 :
echo $this->Form->select(
'field_name', [1,2,3,4,5,6],
['empty', => '(Click to select Something']
);
Scroll down a bit from here in the docs: https://book.cakephp.org/4/en/views/helpers/form.html#options-for-select-checkbox-and-radio-controls

How to validate user must choose at least one value radio button in CakePHP?

I have a code in view file about radio button (CakePHP) :
echo $this->Form->input('publish_flg', array(
'type' => 'radio',
'div' => false,
'label' => false,
'fieldset' => false,
'legend' => false,
'hiddenField' => false,
'required' => false,
'separator'=> '</div><div class="radio-inline">',
'before' => '<div class="radio-inline">',
'after' => '</div>',
'options' => array(1 => 'Yes', 0=> 'No'),
'error' => array(
'attributes' => array('wrap' => 'div', 'class' => 'alert alert-danger', 'escape' => 'false')
)
));
Now I want user must check at least one value in radio button, but both validate I write in model also not successfull :
Solution 1 (It always return message error both I check or not check radio button)
public $validate = array(
'publish_flg' => array(
'notEmpty' => array(
'required' => true,
'message' => 'ERROR VALIDATE'
)
)
);
Solution 2 (It not validate, then if I don't check any radio button, it also submit data to database)
public $validate = array(
'publish_flg' => array(
'rule' => 'notEmpty',
'message' => 'ERROR VALIDATE'
)
);
Can anyone give me a solution for this ? , thanks.
In controller, I don't use $this->Model->save() to validate, I used $this->Model->validates(), is it wrong ?
notEmpty validation should work, but you could also try inList, since you specify the values to be either 0 for No and 1 for Yes in the FormHelper.
If you're not sure about your syntax, you could always try to bake the model and create the validation rules that way.

CJuiAutoComplete submit selected value

I try to get the selected value from dropdown, but it only takes the written value.
How can I take the selected value?
$this->widget('zii.widgets.jui.CJuiAutoComplete',array(
'id' => 'autosuggest_name',
'name' => 'model',
'options' => array('minLength'=>'1'),
'source' => $this->createUrl("advertisements/autocompleteBoatModels"),
'htmlOptions'=> array(
'maxlength' => 30
),
'options' => array(
'delay' => 100,
'showAnim' => 'fold',
'select' => 'js:function(event,ui){
$("#hmodel").val(ui.item.model_id);
$(event.target.form).submit();
}',
),
));
I've tried this, but it doesn't work
$("#hmodel").val(ui.item.model_id).submit
Where is the error :)
thank you
Make ajax call instead of submit
'select'=>'js:function(event, ui) {
$.ajax({
type:"POST",
url: "/your/url",
data: {selected:ui.item.id},
success:function(data) {}
});

How to reload a form from a drop down

I would like to reload the form, or preferably just the values, by using 'onChange'. There are several elements in the form that needs to be updated when changing the project id.
$this->addElement('select', 'project_id', array(
'label' => t('Project'),
'multiOptions' => $data,
'filters' => array('Int'),
'value' => 0,
'required' => true,
'disableTranslator' => true,
'validators' => array(),
'onChange' => 'Javascript:window.location.reload(false)'
));
You can change your onChange to call a custom javascript function instead - which will change any fields you need to change, and then submmit the form.

Categories