grails populating g:select from query - list

Trying to become a grails convert I have begun converting an existing application to Grails and Groovy. It works very well but I get stuck on the conversion of select tags.
I have a domain class:
package todo
class Person {
String ssn
String firstname
String familyname
String role
String emailname
String emailserver
...
When creating a new "todo" task an owner may be assigned from those persons in the system who are developers and I get this working (a fairly direct translation from PHP):
<select id="owner" name="owner">
<option>Noboby ...</option>
<g:each in="${Person.list()}">
<g:if test="${it?.role=='developer'}">
<option value="${it?.id}">${it?.firstname} ${it?.familyname}</option>
</g:if>
</g:each>
</select>
But every attempt to make it more "Grails-ish" fails. How can it be moulded into Grails v2.2.1 code? I spent hours reading, trying, failing.

If you woulkd like to make it more Grails style, you should perform all your logic within controllers \ services not in the view.
Assuming you have a view createTodo in the folder person and the PersonController, then modify your createTodo action like this:
class PersonController {
def createTodo() {
def developers = Person.findAllWhere(role: 'developer')
[developers: developers, ... /* your other values */]
}
}
So you don't need to handle with database operations in your view.
Next step is to use the g:select tag like this:
<g:select name="owner" from="${developers}" optionValue="${{'${it.firstName} ${it.familyName}'}}" noSelection="['null':'Nobody ...']" optionKey="id" value="${personInstance?.id}" />

Try this code:
<g:select optionKey="id" from="${Person.findAllByRole('developer')}" optionValue="${{it.fullName}}" value="${yourDomainInstance?.person?.id}" noSelection="['null':'Nobody']"></g:select>
And in your class:
class Person {
....
String getFullName(){
it?.firstname+' '+ it?.familyname
}
static transients = ['fullName']
....
}
See g:select tag for more details

Finally, I got it working as I want to and it works (almost) according to the #"Mr. Cat" solution. One little detail, though, 'it' does not exist in the class so the getFullName method had to become:
String getFullName(){
this?.firstname+' '+ this?.familyname
}
Up and working, thank you for all help.

Related

vue testing vuetify input for disabled

I am very new to testing and I'm struggling my way through all this new stuff I am learning. Today I want to write a test for a vuetify <v-text-field> component like this:
<v-text-field
v-model="user.caption"
label="Name"
:disabled="!checkPermissionFor('users.write')"
required
/>
my test should handle the following case:
an active, logged in user has a array in vuex store which has his permissions as a array of strings. exactly like this
userRights: ['dashboard', 'imprint', 'dataPrivacy']
the checkPermissionFor() function is doing nothing else then checking the array above with a arr.includes('x')
after it came out the right is not included it gives me a negotiated return which handles the disabled state on that input field.
I want to test this exact scenario.
my test at the moment looks like this:
it('user has no rights to edit other user overview data', () => {
const store = new Vuex.Store({
state: {
ActiveUser: {
userData: {
isLoggedIn: true,
isAdmin: false,
userRights: ['dashboard', 'imprint', 'dataPrivacy']
}
}
}
})
const wrapper = shallowMount(Overview, {
store,
localVue
})
const addUserPermission = wrapper.vm.checkPermissionFor('users.write')
const inputName = wrapper.find(
'HOW TO SELECT A INPUT LIKE THIS? DO I HAVE TO ADD A CLASS FOR IT?'
)
expect(addUserPermission).toBe(false)
expect(inputName.props('disabled')).toBe(false)
})
big questions now:
how can I select a input from vuetify which has no class like in my case
how can I test for "is the input disabled?"
wrapper.find method accepts a query string. You can pass a query string like this :
input[label='Name'] or if you know the exact index you can use this CSS query too : input:nth-of-type(2).
Then find method will return you another wrapper. Wrapper has a property named element which returns the underlying native element.
So you can check if input disabled like this :
const buttonWrapper = wrapper.find("input[label='Name']");
const isDisabled = buttonWrapper.element.disabled === true;
expect(isDisabled ).toBe(true)
For question 1 it's a good idea to put extra datasets into your component template that are used just for testing so you can extract that element - the most common convention is data-testid="test-id".
The reason you should do this instead of relying on the classes and ids and positional selectors or anything like that is because those selectors are likely to change in a way that shouldn't break your test - if in the future you change css frameworks or change an id for some reason, your tests will break even though your component is still working.
If you're (understandably) worried about polluting your markup with all these data-testid attributes, you can use a webpack plugin like https://github.com/emensch/vue-remove-attributes to strip them out of your dev builds. Here's how I use that with laravel mix:
const createAttributeRemover = require('vue-remove-attributes');
if (mix.inProduction()) {
mix.options({
vue: {
compilerOptions: {
modules: [
createAttributeRemover('data-testid')
]
}
}
})
}
as for your second question I don't know I was googling the same thing and I landed here!

GRAILS: Can I call service logic from _FORM.GSP template?

I'm refactoring scaffolding templates and I hit a wall with this issue:
I was trying to call service (some security logic) from template _FORM.GSP - but in the code part, not in the output part
I've read and tried what was suggested in here: How do I call a Grails service from a gsp?
I've tried to use taglib, but my knowledge of grails may not be extensive enough for that
I've tried add import and def to the beginning of _FORM.GSP file (both grailsApplication and application instantiation of service were crashing on missing property application resp. missing property grailsApplication)
I've even tried to call the taglib from the code both directly as method isAllowedToEdit and also as g.isAllowedToEdit both crashing on unknown method resp. "no such property g"
it seems that template _form.gsp has different rules than standard gsp view
I want to do something like this:
private renderFieldForProperty(p, owningClass, prefix = "") {
boolean hasHibernate = pluginManager?.hasGrailsPlugin('hibernate')
boolean display = true
boolean required = false
if (hasHibernate) {
cp = owningClass.constrainedProperties[p.name]
display = (cp ? cp.display : true)
required = (cp ? !(cp.propertyType in [boolean, Boolean]) && !cp.nullable && (cp.propertyType != String || !cp.blank) : false)
}
/* trying to do this part */
// I want to assign value to cp.editable - so later I can render read-only fields in renderEdit
if (!mySecurityService.canEdit(springSecurityService.currentUser, owningClass.getClass(), actionName, p.name)) {
cp.editable = false
}
/* trying to do this part */
if (display) { %>
<div class="fieldcontain \${hasErrors(bean: ${propertyName}, field: '${prefix}${p.name}', 'error')} ${required ? 'required' : ''}">
<label for="${prefix}${p.name}">
<g:message code="${domainClass.propertyName}.${prefix}${p.name}.label" default="${p.naturalName}" />
<% if (required) { %><span class="required-indicator">*</span><% } %>
</label>
${renderEditor(p)}
</div>
<% } } %>
if there is any way to assign cp.editable - I'll try your suggestions
it seems that template _form.gsp has different rules than standard gsp view
The generated _form.gsp works same as other gsps but the template inside the scr/templates/scaffolding/ is different. Customizing the templates like you are doing is a bit more tricky. Keep in mind that the logic you are writing is for Grails on how to generate views(gsp). Meaning you are telling Grails to check some logic before generating the views in memory or in the file. You might be able to accomplish that to some extend for dynamic (in memory) scaffolding at run-time but for sure not for static scaffolding. That's because Grails
is not aware of currentUser when generating the templates.
Your problem will be much simpler if you generate your views and then customize them instead of modifying their templates. Then you can inject your services and do other checks. However, as you also mentioned those logics are better off in a tag library here.
Also since you mentioned security, rendering a field non-editable does not guaranty inability to edit your fields. I would suggest to put the check logic inside your controller for example in SAVE or UPDATE action to prevent any unauthorized user editing fields.
Did you try this?
<%# page import="com.myproject.MyService" %>
<%
def myService = grailsApplication.classLoader.loadClass('com.myproject.MyService').newInstance()
%>
this will work for sure.
go through this link : click here

Make regular expression field optional which is displayed as mandatory field in MVC 4

I'm working on ASP.NET MVC 4. I'm using Data Annotations in model for validation.
There is one field name Mobile and validating using regular expression like below.
[RegularExpression(#"[0-9]{10}", ErrorMessage = "Mobile Number is Not Valid")]
public string Mobile { get; set; }
As per the application requirement above field is not mandatory but need to validate Mobile number if user insert it.
My problem is that when I submit form it displays "Mobile field is required." but the mobile field is not decorate with [Required] attribute. So How it display as required?
What could be the reason?
How to solve it?
In your project, there is a folder "views" where you probably created your view with your needed controls.
there should be something like:
#Html.LabelFor(m => m.Mobile)<br />
#Html.TextBoxFor(m => m.Mobile)<br />
in order to make the regularExpression work.
If you already did this, another problem could be your model.
At the top of your view you should add your model by:
#model yourModelName
As you did not post any code in you controller, Index is assumed as action of your controller and MyModel is assumed as model.
Try the following code in your controller:
public ActionResult Index(MyModel model)
{
//This will check if `Mobile` is empty and remove the Mobile from validation if the field is empty
if (collection.Get("Mobile") == "") //Mobile is the name of the Mobile number field in view
{
ModelState.Remove("Mobile");
}
if(ModelState.IsValid){
//do something
}
else{
return View(model);
}
}
Hope it helps
You can do it very easily using following simple jQuery..
//Remove Required field validation on pin and mobile number
$(document).ready(function () {
$("#Mobile").removeAttr("data-val");
});
Pls try and let me us know if that works for you or not.
Thanks

Play Framework Template list only top 5

In Play framework how do I list only a certain number of object from database instead of all.
Suppose that I have this Post class that has a #OneToMany relation with Comments just like this
public class Post extends Model {
#ManyToOne
public User user;
public String name;
public String description;
public String image;
public Date created_date;
#OneToMany(mappedBy="post", cascade=CascadeType.ALL)
public List<Comment> comments;
....
}
How do I only list top 5 comments for each post at the template? I've done this, but it shows all comments
#{list items:post.comments, as:'comment'}
<p>${comment.comment}</p>
#{/list}
Thanks
You can just use standard Java to get the sub list
#{list items:post.comments.subList(0,5), as:'comment'}
<p>${comment.comment}</p>
#{/list}
The above will return an array index out of bounds if there are less than 5 comments though, so you can either put some more logic in the template (as below), or you could create a getter method in your Post model that gets the top5 comments, and just call this instead (which is probably the cleaner and preferred option.
The extra logic in the template would look like
#{list items:post.comments.subList(0, Math.min(5, post.comments.size())), as:'comment'}
<p>${comment.comment}</p>
#{/list}
If you agree that this is too much logic in your view, and want to encapsulate it in your Model, you could do the following.
The extra logic in your view may look like
public List<Comment> getTop5() {
return comments.subList(0, Math.min(5, comments.size());
}
And then your controller would be
#{list items:post.top5, as:'comment'}
<p>${comment.comment}</p>
#{/list}

Adding a search box to filter a list of results in Symfony?

I need to put a search box within a list of objects as a result of a typical indexSuccess action in Symfony. The goal is simple: filter the list according to a criteria.
I've been reading the Zend Lucene approach in Jobeet tutorial, but it seems like using a sledge-hammer to crack a nut (at least for my requirements).
I'm more interested in the auto-generated admin filter forms but I don't know how to implement it in a frontend.
I could simply pass the search box content to the action and build a custom query, but is there any better way to do this?
EDIT
I forgot to mention that I would like to have a single generic input field instead of an input field for each model attribute.
Thanks!
I'm using this solution, instead of integrating Zend Lucene I manage to use the autogenerated Symonfy's filters. This is the way i'm doing it:
//module/actions.class.php
public function executeIndex(sfWebRequest $request)
{
//set the form filter
$this->searchForm = new EmployeeFormFilter();
//bind it empty to fetch all data
$this->searchForm->bind(array());
//fetch all
$this->employees = $this->searchForm->getQuery()->execute();
...
}
I made a search action which does the search
public function executeSearch(sfWebRequest $request)
{
//create filter
$this->searchForm = new EmployeeFormFilter();
//bind parameter
$fields = $request->getParameter($this->searchForm->getName());
//bind
$this->searchForm->bind($fields);
//set paginator
$this->employees = $this->searchForm->getQuery()->execute();
...
//template
$this->setTemplate("index");
}
It's important that the search form goes to mymodule/search action.
Actually, i'm also using the sfDoctrinePager for paginate setting directly the query that the form generate to get results properly paginated.
If you want to add more fields to the search form check this :)
I finally made a custom form using the default MyModuleForm generated by Symfony
public function executeIndex {
...
// Add a form to filter results
$this->form = new MyModuleForm();
}
but displaying only a custom field:
<div id="search_box">
<input type="text" name="criteria" id="search_box_criteria" value="Search..." />
<?php echo link_to('Search', '#my_module_search?criteria=') ?>
</div>
Then I created a route named #my_module_search linked to the index action:
my_module_search:
url: my_module/search/:criteria
param: { module: my_module, action: index }
requirements: { criteria: .* } # Terms are optional, show all by default
With Javascript (jQuery in this case) I append the text entered to the criteria parameter in the href attribute of the link:
$('#search_box a').click(function(){
$(this).attr('href', $(this).attr('href') + $(this).prev().val());
});
And finally, back to the executeIndex action, I detect if text was entered and add custom filters to the DoctrineQuery object:
public function executeIndex {
...
// Deal with search criteria
if ( $text = $request->getParameter('criteria') ) {
$query = $this->pager->getQuery()
->where("MyTable.name LIKE ?", "%$text%")
->orWhere("MyTable.remarks LIKE ?", "%$text%")
...;
}
$this->pager->setQuery($query);
...
// Add a form to filter results
$this->form = new MyModuleForm();
}
Actually, the code is more complex, because I wrote some partials and some methods in parent classes to reuse code. But this is the best I can came up with.