Yii how to send through ajaxButton the selected value of a combo? - ajax

echo CHtml::ajaxButton (Yii::t('Listing.Listing','Associa'),
$this->createUrl('listing/addAssociation', array("id" => $model->id, "itemId"=> '5' ) ),
array('update' => '#dataToUpdate')
);
i'm using this example code (and it's working), to send an id and an itemId ('5' fixed in the example), to addAssociation.
All is working, but I've the need to pass for itemId the result of a jQuery expression. something like
"itemId" = "js: $('#myDropDown option:selected').val();"
But in this case, the js expression is not evaluated and in actionAddAssociation i see that itemId is the literal string:
"js: $('#myDropDown option:selected').val();"
how to allow an user to select a combo, and the pass the selected value to an action based on this action ?

You have to pass that in the ajax option's data property:
echo CHtml::ajaxButton (Yii::t('Listing.Listing','Associa'),
$this->createUrl('listing/addAssociation', array("id" => $model->id)),
array(// these are ajax options
'data' => array('itemId'=> 'js: $("#myDropDown option:selected").val()'),
// and don't use semi-colon after val(), you can also pass id here itself
'update' => '#dataToUpdate'
)
);

You can try to use jQuery's ajaxPrefilter to add a function that will alter your data being passed through ajax.
First, you have to register the following script on your page's initialization:
jQuery.ajaxPrefilter(function(options, originalOptions, jqXHR) {
if (originalOptions['data'] && originalOptions['data']['requestId'] === "myDropDown") {
delete originalOptions['data']['requestId'];
originalOptions['data']['itemId'] = jQuery("#myDropDown option:selected").val();
options['data'] = jQuery.param(originalOptions['data']);
}
});
Here options is a complete settings object determined for jQuery.ajax(). I pass an additional requestId parameter to identify ajax request and, to make things simplier, a view could look like this:
<?php
print CHtml::dropDownList("myDropDown", 1, array(
1 => "First",
2 => "Second",
3 => "Third"
));
print CHtml::ajaxButton(Yii::t("Listing.Listing", "Associa"), Yii::app()->createUrl("listing/addAssociation"), array(
'data' => array(
'requestId' => "myDropDown",
'id' => $model->id
),
'update' => "#dataToUpdate"
));

Related

Validate Select2 in Yii2 via AJAX

I have Yii2 application which uses the Kartik plugin to initialize Select2 dropdowns in forms.
I have one particular Select2 which uses AJAX call to get the data for the drop down options.
<?=
$form->field($model, 'court_house_id', ['enableAjaxValidation' => true, 'selectors' => ['input' => '#' . $id . "-court-house"],'template' => FormHelper::GenerateFieldTemplate([6])])
->widget(Select2::classname(), [
'options' => ['id' => $id . "-court-house", 'placeholder' => Yii::t('app', 'Search court house...')],
'hashVarLoadPosition' => \yii\web\View::POS_READY,
'pluginOptions' => [
'dropdownParent' => new JsExpression("$('#$modalWindowId')"),
'allowClear' => true,
'minimumInputLength' => 2,
'language' => [
'errorLoading' => new JsExpression("function () { return '" . Yii::t('app', 'Search...') . "'; }"),
],
'ajax' => [
'url' => app\components\UrlMaker::link('data/court-house-list'),
'dataType' => 'json',
'data' => new JsExpression('function(params) { return {q:params.term}; }')
],
'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
'templateResult' => new JsExpression('function(courthouse) { return courthouse.text; }'),
'templateSelection' => new JsExpression('function (courthouse) { return courthouse.text;}'),
]])
->label(Yii::t('app', 'Court House'), ['class' => FormHelper::GenerateLabelClassTemplate([3])]);
?>
Intentionally pasting all of the code, although most of it is irrelevant I would assume.
I have this loaded in multiple dynamically created forms thus all the strange ids and selectors. However, the form has different dropdown which controls whether some of the fields are shown (and required) or not. This particular field above is only shown in one of the scenarios which all the other variations of the form do not have it. So the model has the following validation:
[['court_house_id', 'staff'], 'required', 'on' => self::SCENARIO_ONE],
By the way staff is just a regular text field and everything works for it.
In order to change the scenario, I have the following line in the view with the form:
<?php $model->scenario = \app\models\MyModel::SCENARIO_ONE; ?>
The problem is that when I submit the form empty, the staff field gets marked in red as invalid but the court house is marked in green as valid although it is empty.
If I go into the model and remove the 'on' => self::SCENARIO_ONE part then everything works as expected - on empty submit the court house field also lights up in red but that would be a problem for the rest of my scenarios where this field is not needed.
Any ideas what might be causing the problem and how to resolve it?
Try to set the scenario in controller before calling save() method, for example
$model = new MyModel(['scenario' => MyModel::SCENARIO_ONE])

Passing variable to the view from zend form using AJAX load

controller:
public function searchAction()
{
$form = new Application_Form_Search;
$k = $form->getValue('keyword');
$car = new Application_Model_Car();
$mapper = new Application_Model_CarMapper();
$this->view->cars = $mapper->search($keyword);
}
form
<?php
class Application_Form_Search extends Zend_Form {
public function init(){
$this->setMethod('post');
$this->addElement('text', 'keyword', array(
'required' => true,
'label' => 'Keyword:'
));
$this->addElement('button', 'submit', array(
'required' => false,
'ignore' => true,
'label' => 'Search'
));
}
}
?>
original view page
$(document).ready(function(){
$("#submit").click(function(){
$("#main").load('/cars/search');
});
});
Here I am trying to return the search view at the "id=main" section in the original view page by using .load()
In the search view, <?=$this->keyword?> is showing null
it seems like there's problem fetching the keyword field from the form
Ok, you need to set the second variable of .load to send the data. At present you aren't sending the data to the .load function
so it would be more like this. This is assuming that you have id of keyword
$("#main").load('/cars/search', {'keyword':$('input#keyword').val()});
or you could use
$("#main").load('/cars/search', {'keyword':$('[name="keyword"]').val()});
Please see
http://api.jquery.com/load/
Also remember to escape the value so people can't send malicious code through your form.
Hope that helps

cakephp passing selection to JS Helper

I want to get the selected schoolFilter form value and place in [SELECTED VALUE HERE].
$data = $this->Js->get('#SchoolFilter')->serializeForm(array('isForm' => true, 'inline' => true));
$this->Js->get('#SchoolSchoolId')->event(
'change', $this->Js->request(
array('action' => 'assign_school_ads/', [SELECTED VALUE HERE], array(
'update' => '#results',
'data' => $data,
'async' => true,
'dataExpression' => true,
'method' => 'POST'
)
)
);
// School Filter
$schoolFilter = $this->Form->create('School', array('id' => 'SchoolFilter');
$schoolFilter .= $this->Form->input('school_id', array('label'=>'Schools', 'empty'=>'- select -');
$schoolFilter .= $this->Form->end();
I have seen variations on this question but without any clear answer, except to just forget using JS Helper. Is it possible within the context of JS Helper? And if not, can I get the value using regular JQuery, then inject it into JS Helper.
Use following code :-
$this->Js->get('#SchoolFilter');
$this->Js->event('change', $this->Js->request(array('controller'=>'put-ur-controller-ame-here','action' => 'assign_school_ads'),array('async' => true,'update' => '#results','method' => 'post','dataExpression'=>true,'data'=> $this->Js->serializeForm(array('isForm' => true,'inline' => true)))));
the serialize() function sends the form data to the php action so we can see which option was selected and decide what to update in the ajax call.
the form data will be found in $this->data in the action (just like after a form has been submited).
Don't forget to add $this->Js->writeBuffer(); in your layout just before the body closing tag. Otherwise all the ajax code will not be added to your page.

CodeIgniter: validating form array not working

I have an array of profile data I need to validate:
$user_group_profiles = $this->input->post('user_group_profiles', TRUE);
foreach ($user_group_profiles as $key => $user_group_profile)
{
$this->form_validation->set_rules("user_group_profiles[$key][profile_name]", 'Profile Name', 'trim|required');
$this->form_validation->set_rules("user_group_profiles[$key][birthdate]", 'Birthdate', 'trim|required');
// TODO: heigth/weight not required, but the validation somehow makes it required
$this->form_validation->set_rules("user_group_profiles[$key][height]", 'Height', 'trim|greater_than[0]');
$this->form_validation->set_rules("user_group_profiles[$key][weight]", 'Weight', 'trim|greater_than[0]');
}
Height and weight are option, but when no value is set for those fields, CI validation complains. A var_dump($user_group_profiles); shows this:
array
'ugp_b33333338' =>
array
'profile_name' => string '' (length=0)
'birthdate' => string '' (length=0)
'height' => string '' (length=0)
'weight' => string '' (length=0)
Any ideas what might be wrong?
EDIT 1:
I went into CI's Form_validation library and made $_field_data and public member. When I var_export it after, I got this:
'user_group_profiles[ugp_833333338][height]' =>
array
'field' => string 'user_group_profiles[ugp_833333338][height]' (length=42)
'label' => string 'Height' (length=6)
'rules' => string 'greater_than[1]' (length=15)
'is_array' => boolean true
'keys' =>
array
0 => string 'user_group_profiles' (length=19)
1 => string 'ugp_833333338' (length=13)
2 => string 'height' (length=6)
'postdata' => string '' (length=0)
'error' => string 'The Height field must contain a number greater than 1.' (length=54)
Ok - I've just spent an hour on this - and I've worked out the problem + solution
The issue is that when the variable you are testing is part of an array, CI interprets the field as being set, and thus "contains" a value (even when it is empty). Because that "value" is NOT a number greater than 0 - it fails.
Therefore you'll need to unset the $_POST (NOT $user_group_profiles) variable when it is "empty" so that it passes validation. Note - validation is run on $_POST - that is why you are unsetting $_POST and not $user_group_profiles
So the workaround is this:
$user_group_profiles = $this->input->post('user_group_profiles', TRUE);
foreach ($user_group_profiles as $key => $user_group_profile)
{
// Set the rules
$this->form_validation->set_rules($key."[profile_name]", "Profile Name", "trim|required");
$this->form_validation->set_rules($key."[birthdate]", "Birthdate", "trim|required");
$this->form_validation->set_rules($key."[height]", "Height", "trim|greater_than[0]");
$this->form_validation->set_rules($key."[weight]", "Weight", "trim|greater_than[0]");
// Now check if the field is actually empty
if (empty($user_group_profile['height']))
{
// If empty, remove from array so CI validation doesnt get tricked and fail
unset($_POST[$key]['height']);
}
if (empty($user_group_profile['weight']))
{
unset($_POST[$key]['weight']);
}
}
I've tested this - and it fixes your problem.
You could also program it this way if you dont want to touch the $_POST data:
foreach ($user_group_profiles as $key => $user_group_profile)
{
// Set the rules
$this->form_validation->set_rules($key."[profile_name]", "Profile Name", "trim|required");
$this->form_validation->set_rules($key."[birthdate]", "Birthdate", "trim|required");
// Now check if the field is actually empty
if ( ! empty($user_group_profile['height']))
{
$this->form_validation->set_rules($key."[height]", "Height", "trim|greater_than[0]");
}
if ( ! empty($user_group_profile['weight']))
{
$this->form_validation->set_rules($key."[weight]", "Weight", "trim|greater_than[0]");
}
}
Let's try and break this down into parts. I hope I can help you out here.
I'm assuming you're doing the following for the XSS Filtering with the second "TRUE" argument:
$user_group_profiles = $this->input->post('user_group_profiles', TRUE);
You can actually do the XSS filtering with the form validation rules, or if you prefer filter the post after the rules. See here for my preference:
$this->form_validation->set_rules('profile_name', 'Profile Name', 'xss_clean|trim|required');
So with knowing that now, we can follow the CI convention for their Form Validation Library. It's not necessary to grab the post before using the Form Validation Library because it auto-detects I believe the POST data anyway by the field name. For example:
$this->form_validation->set_rules('profile_name', 'Profile Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('birthdate', 'Birthdate', 'trim|required|xss_clean');
$this->form_validation->set_rules('height', 'Height', 'trim|greater_than[0]|numeric');
$this->form_validation->set_rules('weight', 'Weight', 'trim|greater_than[0]|numeric');
if ($this->form_validation->run() == FALSE) {
$this->load->view('my_view_where_this_post_came_from');
} else {
$profile_name = $this->input->post('profile_name');
//or if you prefer the XSS check here, this:
//$profile_name = $this->input->post('profile_name', TRUE);
$birthdate= $this->input->post('birthdate');
$height= $this->input->post('height');
$weight= $this->input->post('weight');
//$user_group_profiles = $this->input->post();
}
I hope this helps!
EDIT: I also just noticed this. If you're trying to grab the entire post array the code is:
$user_group_profiles = $this->input->post(NULL, TRUE); // returns all POST items with XSS filter
$user_group_profiles = $this->input->post(); // returns all POST items without XSS filter
Not:
$user_group_profiles = $this->input->post('user_group_profiles');
A good help if you don't know your $_POST names or are confused, you can do this to see if that data is even there! Like this as your first line:
var_dump($_POST);
exit();

Problem when I am populating data into selectbox through ajax in Drupal

I have installed drupal 6, added some cck fields in one content type. Added two select box fields.
I am taking the selected value of parent select box and as per that selection passing relates options to next select box field using Ajax. (e.g Country -> State. When user selects country I want to pass state values into next select box.)
But when I am submitting the form it gives the following error:
"An illegal choice has been detected. Please contact the site administrator."
I don't know why it is not taking the ajaxified select box values while saving the node.
Does somebody has the solution on it. Is there any solution to handle this dynamic select options in Drupal.
Thanks in advance.
The same thing I'm working on drupal 7 and its work for me. Below is the code. Hope this help you. What I have done is on selection of car model car variant will change and the data save in the table.
function add_offer_form($form, $formstate) {
$form['add_offer_new_car_model'] = array(
'#type' => 'select',
'#required' => TRUE,
'#options' => $car_model,
'#ajax' => array(
'effect' => 'fade',
'progress' => array('type' => 'none'),
'callback' => 'variant_callback',
'wrapper' => 'replace_variant',
),
);
// Combo box to select new car variant
$form['add_offer_new_car_variant'] = array(
'#type' => 'select',
'#options' => array(),
// The prefix/suffix provide the div that we're replacing, named by #ajax['wrapper'] above.
'#prefix' => '<div id="replace_variant">',
'#suffix' => '</div>',
);
// An AJAX request calls the form builder function for every change.
// We can change how we build the form based on $form_state.
if (!empty($formstate['values']['add_offer_new_car_model'])) {
$model_id = $formstate['values']['add_offer_new_car_model'];
$rows = array();
$result = db_query("SELECT id, variant_name from {va_car_variant} where car_model_id in ($model_id,1) order by variant_name");
while ($data = $result->fetchObject()) {
$id = $data->id;
$rows[$id] = $data->variant_name;
}
$form['add_offer_new_car_variant']['#options'] = $rows;
}
}
////////////////////////////////////////////////////////
///////// FUNCTION FOR AJAX CALL BACK
function variant_callback($form, &$form_state) {
return $form['add_offer_new_car_variant'];
}

Resources