In Yii2, while you use gridview widget to display your table data, for performing any operation on the record by selecting the checkbox, gridview assign the value of the first column retrieved from your the result set. But you need to set some different value to the checkbox, so to do that you can use the below code GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ ['class' => 'yii\grid\SerialColumn'], [ 'class' => '\kartik\grid\CheckboxColumn', 'checkboxOptions' => function($model, $key, $index, $widget) { return ["value" => $model['id']]; // this can be substituted with any column value of your result data }, ], 'name', ['class' => 'yii\grid\ActionColumn'], ], ]); instead of the default one GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ ['class' => 'yii\grid\SerialColumn'], [ 'class' => '\kartik\grid\CheckboxColumn', 'mergeHeader'=>false ], 'name', ['class' => 'yii\grid\ActionColumn'], ], ]);
Category: web development
General Posting
issue with submit form in ajax success response
Usually when we try to submit the form after ajax call on success response, the form does not’t get submitted.
Like for example in below code:-
$.ajax({
type : “POST”,
url : “to/some/url”,
data : {
userName:userName,
mobileNumber:mobileNumber,
otp:otp
},
dataType : “json”,
success : function(data)
{
if(data.result == true) {
jQuery(“#form_id”).submit();
}else {
return false;
}
}
});
Now why this occur. Since you are calling the submit function from the ajax response, you need to remove the “submit” handler before submitting the form again. I.E. You need to submit the form without doing the ajax call again. Refer below:-
$.ajax({
type : “POST”,
url : “to/some/url”,
data : {
userName:userName,
mobileNumber:mobileNumber,
otp:otp
},
dataType : “json”,
success : function(data)
{
if(data.result == true) {
// Submit this form without doing the ajax call again
jQuery(“#form_id”).unbind().submit();
}else {
return false;
}
}
});
Drupal 8 render block from node twig file
{{ drupal_entity(‘block’, ‘put your block id here’, check_access=false) }}
check_access=false => This is to make check if the block is allowed to access by which kind of user; default is false, which allow the block to be displayed without any check.
How do I gunzip all files .gz recursively in a target directory?
The solution is very simple
gunzip -r *
This will extract all .gz file in the respective directory and sub directory and remove the .gz files as well.
How to Set Access-Control-Allow-Origin (CORS) Headers in .htaccess file
<FilesMatch “\.(ttf|otf|eot|woff|js|css|woff2)$”> # Type all extenstion that you want to allow
<IfModule mod_headers.c>
Header Set Access-Control-Allow-Origin “*”
</IfModule>
</FilesMatch>
How to disable the “Gift Voucher” and “Coupon” fields from opencart
How to search in specific content type in drupal 7
Suppose you have created a content type named as “testimonial”. Now the requirement is to for search for a value “hello” under field name “title” then you can do so by using the below query. Please make sure that you have the “Entity API” module enabled.
$searchkeyword = $_REQUEST['keyword'];
$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', 'node')
->entityCondition('bundle', '[The machine name for your content type]') // in our case it is "testimonial"
->propertyCondition('title', $searchkeyword)
->propertyCondition('status', 1)
->fieldCondition('field_active_status', 'value', 'active',"=") // here you have to define your where condition on additional fields if required
->execute();
validate email id using jquery or javascript
function validateEmail(email) {
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,6})?$/;
return emailReg.test(email);
}
Usage:-
if(validateEmail(emailString)){ alert('Your email is valid'); } else{ alert('Your email is not valid! There should be atleast @ and one dot(.) present in it'); }
How to display hidden files in Mac OS
Use “Command”+”Shift”+”.(dot)” to enable display of hidden files
To hide just press the above three button again.
Validate checkbox array and prefill with selected value on validation error in laravel 5
Say for example you have your checkbox field code in your twig file as below:-
@foreach ($serviceList as $servloop)
<div class=”checkbox”>
<label>
<input type=”checkbox” name=”services[]” value=”{{$servloop->service_id}}” {{ ( is_array(old(‘services’)) && in_array($servloop->service_id, old(‘services’)) ) ? ‘checked ‘ : ” }} />
{{$servloop->service_name}}
</label>
</div>
@endforeach
Controller file code
Validation Rule will be as below:-
$rules = [
‘services’ => ‘required|min:1’, // min:1 means atleast one should be selected
];
$customMessages = [
‘services.required’ => ‘Select the service you will need from us, for and during the Expo.’,
];