Cakephp:How to validate my select options so that it restricts duplication of options selected - cakephp

I have a selection drop down menu, and I want the user to select two options for a field in the database. The problem is how do I make it to disallow duplication of select options, currently it's saving all options even when they are the same.
Code in my add.ctp for the select options is:
echo $this->Form->select("ProgrammeChoice.programme_code.0",$finals);
echo $this->Form->select("ProgrammeChoice.programme_code.1",$finals);
And the variable $finals is bringing the select options from another table in the database, it's in the controller and the code is:
$finals = array_merge($filtered_programs,$non_preq_programs);
So please, I want help to validate my select menu to deny duplicate selections on submission.

Create a custom validation rule and compare the values as described here: http://book.cakephp.org/2.0/en/models/data-validation.html#adding-your-own-validation-methods
In the validation method your data is stored in $this->data.
It should look similar to this:
public function compare($field1) {
if($field1 === $this->data['ProgrammeChoice']['programm_code']['1']) {
return false;
}
return true;
}

Related

React-Select isMulti retrieve selected options

I'm currently working on a springboot/reactjs project. I'm using the react select library to set a multi select input in one of my forms but I could not get the selected values here's some code to make it a bit clearer.
these are my options generated dynamically from the database each option has the webService Id as a value
this is my select input, I need to get the selected values "Ids" and then call the method that retrieves the webservices from the database and then assign the list of webServices to my newApplicationData.webservices
this is the get web service function
Update : I kind off found a solution to my problem : on the onChange prop I used this
onChange={(selectedOptions) => {
const state = this.state;
state.selectedWebServices = [];
selectedOptions.forEach((option) => {
this.getWS(option.value);
state.selectedWebServices.push(this.state.getWSData);
});
state.newApplicationData.webServices =
state.selectedWebServices;
this.setState(state);
}}
and then I found out another problem: even if I select two deffrent options the list of selected options only gets one duplicated option????!!!!

Adding multiple owners for Google App Maker records

I need help with Google App Maker data model security. I want to set multiple owners for a single record. Like the current user + the assigned admin + super admin.
I need this because all records can have different owners and super-owners/admins.
I know that we can point google app maker to a field containing record owner's email and we can set that field to the current user at the time of the creation of the record.
record.Owner = Session.getActiveUser().getEmail();
I want to know if it is possible to have field owners or have multiple fields like owner1, owner2 and then assign access levels to owner1, owner2...
Or how can we programmatically control the access/security/permissions of records?
The solution I'd use for this one definitely involves a field on the record that contains a comma separated string of all the users who should have access to it. I've worked on the following example to explain better what I have in mind.
I created a model and is called documents and looks like this:
In a page, I have a table and a button to add new document records. The page looks like this:
When I click on the Add Document button, a dialog pops up and looks like this:
The logic on the SUBMIT button on the form above is the following:
widget.datasource.item.owners = app.user.email;
widget.datasource.createItem(function(){
app.closeDialog();
});
That will automatically assign the creator of the record the ownership. To add additional owners, I do it on an edit form. The edit form popus up when I click the edit button inside the record row. It looks like this:
As you can see, I'm using a list widget to control who the owners are. For that, it is necessary to use a <List>String custom property in the edit dialog and that will be the datasource of the list widget. In this case, I've called it owners. I've applied the following to the onClick event of the edit button:
var owners = widget.datasource.item.owners;
owners = owners ? owners.split(",") : [];
app.pageFragments.documentEdit.properties.owners = owners;
app.showDialog(app.pageFragments.documentEdit);
The add button above the list widget has the following logic for the onClick event handler:
widget.root.properties.owners.push("");
The TextBox widget inside the row of the list widget has the following logic for the onValueEdit event handler:
widget.root.properties.owners[widget.parent.childIndex] = newValue;
And the CLOSE button has the following logic for the onClick event handler:
var owners = widget.root.properties.owners || [];
if(owners && owners.length){
owners = owners.filter(function(owner){
return owner != false; //jshint ignore:line
});
}
widget.datasource.item.owners = owners.join();
app.closeDialog();
Since I want to create a logic that will load records only for authorized users, then I had to use a query script in the datasource that will serve that purpose. For that I created this function on a server script:
function getAuthorizedRecords(){
var authorized = [];
var userRoles = app.getActiveUserRoles();
var allRecs = app.models.documents.newQuery().run();
if(userRoles.indexOf(app.roles.Admins) > -1){
return allRecs;
} else {
for(var r=0; r<allRecs.length; r++){
var rec = allRecs[r];
if(rec.owners && rec.owners.indexOf(Session.getActiveUser().getEmail()) > -1){
authorized.push(rec);
}
}
return authorized;
}
}
And then on the documents datasource, I added the following to the query script:
return getAuthorizedRecords();
This solution will load all records for admin users, but for non-admin users, it will only load records where their email is located in the owners field of the record. This is the most elegant solution I could come up with and I hope it serves your purpose.
References:
https://developers.google.com/appmaker/models/datasources#query_script
https://developers.google.com/appmaker/ui/binding#custom_properties
https://developers.google.com/appmaker/ui/logic#events
https://developers-dot-devsite-v2-prod.appspot.com/appmaker/scripting/api/client#Record

LOV not displaying drop down values after fetching values via ViewCriteria

I have created a LOV which is dependent on OrganizationId and ManagerId . It should display EmployeeName.
Flow is : User select Organization then Manager(It is also an LOV) and then he can see EmployeeName.
public void applyReleaseRuleValues(){
ViewObject projectCostingTaskName =this.getProjCostingTaskVA().getViewObject();
try{
projectCostingTaskName.setNamedWhereClauseParam("bindInvOrgId",releaseOrganizationId);
projectCostingTaskName.setNamedWhereClauseParam("bindProjectId",releaseManagerId);
projectCostingTaskName.setNamedWhereClauseParam("bindTaskId",releaseEmployeeId);
}catch (oracle.jbo.NoDefException e) {
;
}
projectCostingTaskName.executeQuery();
Row pjcTask=projectCostingTaskName.first();
String setPjcTaskId=(String)pjcTask.getAttribute("EmployeeName");
setAttributeInternal(EMPLOYEENAME,setPjcTaskId );
I don't think it's an UIProject issue as Manager & Employee Name is getting displayed.In Manager LOV is visible but not for Employee.
Any suggestions?
I think you are going about trying to achieve this in the harder way. There is a much easier way to achieve Cascaded List Of Values, via simple View Object query and drag and drop on the form from the controller. If this all you want, in that case check this out:
https://waslleysouza.com.br/en/2014/10/defining-cascading-list-values-adf/
or
http://jjzheng.blogspot.com/2010/06/implement-cascading-lists-of-values-in.html
or
this video: https://www.youtube.com/watch?v=nXwL2_RP7AQ
If you need something more complex, please add it to the question

drupal7 Filter content type based on session in view

I have created a custom type with multiple fields.
1 field is a checkbox to "show for all people"
2nd field is a textfield ( you can add multiple textfields ) for adding a code.
I created a view where all those content types are being shown in a page. ( this works )
But now:
When a person enters the site, he has to insert a code. This code is saved into a cookie because it needs to be remembered for about 2 weeks.
So I can't use the contextual filters.
If the checkbox "show for all people" is checked, this block is shown.
if the checkbox "show for all people" is unchecked, this block is hidden, except for people who came in without a code, or if the code is one of the values that was inserted in the 2nd field.
I don't wan't to use views php_filter. But I have no clue how to proceed with this problem.
I tried some solutions on the web to create a custom filter, but the problem here is, that we can't access the form values.
I found a solution, but I'm not sure if this is the correct drupal way.
I used the hook_node_view function to get all nodes that are printed on that page. I check if the code that was inserted into a cookie with the codes that are allowed ( created in the text fields of the content type )
function code_node_view($node, $view_mode, $langcode) {
if ($node->type == 'winning_codes') {
$code = _code_read_cookie('code');
$winning_codes = (!empty($node->field_winning_codes['und'])) ? $node->field_winning_codes['und'] : array();
$winning_codes = array_map(function ($ar) {
return $ar['value'];
}, $winning_codes);
if (!empty($code) && (!in_array($code, $winning_codes))) {
hide($node->content);
}
}
}

Agile Toolkit - OR statement in combination with Model

I have a Model from which I want to select all rows for which 'caller' or 'callee' is a given value for presentation in a Grid.
I have tried lots of different avenues of accomplishing this and cannot get anywhere with it.
I have a working filter on the Grid which narrows down the results by date (starting and ending dates), by status ("ANSWERED","NO_ANSWER"), I can also add conditions for 'caller' and 'callee', but how do I get it to show all rows where either 'caller' or 'callee' is a match to the current $UserID? Basically showing all calls (rows) a user was involved in?
The MySQL query itself is a simple OR construction, but how do I 'feed' it into the Model or the Grid so that it plays nicely with the other filters on the page?
You can use orExpr() method of DSQL to generate SQL for your needs.
For example,
$m = $this->model->debug(); // enable debug mode
$user_id = $this->api->auth->model->id;// currently logged in user ID
$q = $m->_dsql(); // get models DSQL
$or = $q->orExpr(); // initialize OR DSQL object
$or->where('caller', $user_id) // where statements will be joined with OR
->where('callee', $user_id);
// use one of these below. see which one works better for you
$q->where($or); // add condition with OR statements
$q->having($or); // add condition with OR statements
Of course you can write all of this shorter:
$m = $this->model->debug(); // enable debug mode
$user_id = $this->api->auth->model->id;// currently logged in user ID
$q = $m->_dsql(); // get models DSQL
$q->where($q->orExpr()
->where('caller', $user_id)
->where('callee', $user_id)
);

Resources