I have an exposed filter with a select options drop-down field that collects all the actual values in my current view. Unfortunately it also collects and displays the empty fields in my view.
I have tried to create a module that handles this, but I cannot make it to actually update my current exposed filters. Any suggestions?
function remove_duplicated_publications_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'views_exposed_form'){
foreach($form as $tmp){
if ($tmp["#type"]=="select"){
foreach($tmp["#options"] as $tjek){
if ($tjek==NULL){
unset ($tmp["#options"][$tjek]);
}
}
}
}
}
}
--- SOLUTION FOUND !! ---
I needed to unset the $form array instead. The following code works:
function remove_duplicated_publications_form_alter(&$form) {
foreach($form['#info'] as $field){
$field_id = $field['value'];
if ($form[$field_id]["#type"]=="select"){
foreach($form[$field_id]["#options"] as $optionvalue){
if ($optionvalue==NULL){
unset ($form[$field_id]["#options"][$optionvalue]);
}
}
}
}
}
Use array_walk() in conjunction with unset()
Related
I want module sync-fetch to be accessible globally without need to import in each component and be named as simple fetch.
Also I want to extend it with custom method then.
Now in rollup.config.js there are:
export default {
...
output: {
...
intro: `const fetch = require('sync-fetch');
fetch.Response.prototype.then = function(foo) {
return foo(this);
}`
},
};
And it works, but looks dangerous) Is intro is the only way to do it?
If you want to make it seem less dangerous, you could put that code in a file and then return the contents of it in a function. The output.intro option also takes a function that returns the code as a string.
{
output: {
intro: () => require('fs/promises').readFile('path/to/the/file.js', 'utf-8')
}
}
The code is on DartPad if you need a complete example (see the while loop towards the end.)
I have a loop,
Place place = places[0];
while (places.isNotEmpty) {
// Get a list of places within distance (we can travel to)
List reachables = place.getReachables();
// Get the closest reachable place
Place closest = place.getClosest(reachables);
// Remove the current place (ultimately should terminate the loop)
places.remove(place);
// Iterate
place = closest;
}
But it's not removing place on the second-to-last line. i.e., the length of the places list remains the same, making it an infinite loop. What's wrong?
This could be because the object in the list has a different hashCode from the object you are trying to remove.
Try using this code instead, to find the correct object by comparing the objects properties, before removing it:
var item = list.firstWhere((x) => x.property1== myObj.property1 && x.property2== myObj.property2, orElse: () => null);
list.remove(item);
Another option is to override the == operator and hashCode in your class.
class Class1 {
#override
bool operator==(other) {
if(other is! Class1) {
return false;
}
return property1 == (other as Class1).property1;
}
int _hashCode;
#override
int get hashCode {
if(_hashCode == null) {
_hashCode = property1.hashCode
}
return _hashCode;
}
}
I have faced the very same issue. Unfortunately I haven't found the root cause, but in the same situation I replaced
places.remove[place]
with
places.removeWhere(p => p.hachCode == place.hashCode)
as a workaround. One more approach was helpful too:
// Get the place from your set:
final place = places.first;
// Replace the place in the set:
places.add(place);
// Remove the place from the set:
places.remove(place);
Most likely place is not in the list for some reason. It's hard to debug without knowing the exact data used, the problem doesn't reproduce with the three-place sample you have in the linked DartPad.
Try figuring out which element is causing the problem. For example you can
try adding an if (!places.contains(place)) print("!!! $place not in $places"); before the remove, or something similar that detects the state when the problem occurs.
This way you can remove object from dynamic list
List data = [
{
"name":"stack"
},
{
"name":"overflow"
}
];
data.removeWhere((item) => item["name"]=="stack");
print(data);
Output
[{name: overflow}]
Use the plugin Equatable
class Place extends Equatable {
...
}
https://pub.dev/packages/equatable
I was having the same issue. I did something like this using removeWhere.
myList.removeWhere((item) => item.id == yourItemId.id)
I've got a pretty clean filter which is intended to add an attribute to the model Map so that it can be shown on every page:
def filters = {
someFilter(controller:'*', action:'*') {
after = { Map model ->
model.something = 'hey!' // can't, since it's null
}
}
}
If model is null it fails horribly (NullPointerException, as expected). But if I add an if (!model) and try to instantiate it, it's local and doesn't behave as intended.
How can I put an empty map there when model is null?
I think you can do it with a little groovy triks. You can create a groovy interceptor and apply it to all controllers class in bootstrap. The interceptor implements the afterInvoke method in which check for null results and turn them in empty maps [:]
Try this:
def filters = {
someFilter(controller:'*', action:'*') {
after = { Map model ->
model.put('something', 'hey!')
}
}
}
I have a request to create a form filter that has two fields, one a freeform text
and the other a select. The value in the select will determine how to handle the value of the text is turned into a criteria.
I can create a custom addXXXColumnCriteria for either field, but how can I access the other field from within this function?
I suggest you not to use de addXXXColumnCriteria, but overwrite the FormFilter doBuildCriteria (Propel) or doBuildQuery(Doctrine) methods.
I have never used Propel, but I guess that works as good as for Doctrine.
For example:
class yourPropelFormFilter extends anyKindOfSfFormFilterPropel {
public function doBuildCriteria(array $values) {
$criteria = parent::doBuildCriteria($values);
// ... change the criteria behaviour with the $values array (do some print_r to $values to see how the data array is formatted)
return $criteria;
}
}
For Doctrine (remember to use the getRootAlias query method):
class yourDoctrineFormFilter extends anyKindOfSfFormFilterDoctrine {
public function doBuildQuery(array $values) {
$q = parent::doBuildQuery($values);
$rootAlias = $q->getRootAlias();
if(...) {
$q->innerJoin($rootAlias.'.RelationX rx')
->addWhere('rx.value = ?',$values['...']);
}
return $q;
}
}
Please, remember to return the criteria/query modified object!
I often write something like:
def myAction{ MyActionCommand cmd ->
if( cmd.hasErrors() ){
return render(status:HttpServletResponse.SC_BAD_REQUEST );
}else{
// actual action logic
}
So, I'd like to extract that common pattern into some reusable location. Filter looks like good candidate, but I can't find the way to get command object from the filter. Tryed something like this (in filters closure):
formValidation( controller:'*', action:'*' ){
before = { cmd ->
if( cmd.hasErrors() ){
response.sendError( HttpServletResponse.SC_BAD_REQUEST );
return false;
}else{
return true;
}
}
}
Intersted in grails 1.3.7 compatible solution. Is it possible at all?
No, it isn't possible to do what you are asking. Command Objects are not full framework artifacts like Controller, Service, etc, and so they do not get their validation logic added to them, unless they are a parameter to a Controller action. To that end a Command Object in a filter wouldn't have a .validate() or .hasErrors() method to check against.
As another option you could use the #Validateable annotation:
http://grails.org/doc/latest/guide/7.%20Validation.html#7.5%20Validation%20Non%20Domain%20and%20Command%20Object%20Classes
Move your Command Object to src/groovy as a regular Groovy class and annotate it with #Validateable. Then in your filter you can do:
def validObj = new MyValidateable(params)
if (!validObj.validate()) {
response.sendError( HttpServletResponse.SC_BAD_REQUEST );
return false;
} else {
return true;
}
Make sure you add the package name of your validateable class to the grails.validateable.packages List in Config.groovy.
What about creating a service like this:
class AutoValidateService {
def onValid(def cmd, Closure onValid) {
if( cmd.hasErrors() ){
return render(status:HttpServletResponse.SC_BAD_REQUEST );
}else{
onValid()
}
}
}
The use it like so:
class FooController {
AutoValidateService autoValidateService
def myAction{ MyActionCommand cmd ->
autoValidateService.onValid(cmd) {
// do something
}
}
}