How to save a recordset without overwritting existing records with Cakephp 3 - database

Is there an easy way to save a recordset, i mean multiple records, but only the "new ones"?
I have a table users and a users form in the view. First field you must enter is passport number, so if user already exists in database the rest of the form will be auto completed and disabled to prevent changes but if passport dont exist then you have to enter all data. As anyone can change those existing users data controls from the browser even if they are disabled, i want to make sure only new records are saved in database. First of all i thought i could find again in database and delete existing users from recordset before save, but i wonder if there is a more elegant approach.
Ty in advance.
I write this here, cause comments are too short:
Thank you for your answer, André. I'm sorry if i didnt explain perfectly, but the form is done by disabling all controls except passport and if passport dont exist (i check it on passport focusout) then the rest of controls are set to enabled. I mean, that is already done. The question was only about the saving.
The validation method you talk about, well i'm actually validating all the controls in the form and i must disable the 'unique' rule so a user can link an existing user to the current bill, otherwise it will fail validation on submit and it wont allow the user to proceed (i did this because it happened to me when testing). The actual setting is much more complicated: the form belongs to a model (bills) which is associated with 4 other models and 2 of those relationships are many to many, bills_users and bills_clients, where users are the persons who do the job and clients pay for it, but I was trying to make the question easier. Anyway, what I am looking for is, in fact, some kind of saving setting which I can't find. In documentation I found "When converting belongsToMany data, you can disable the new entity creation, by using the onlyIds option. When enabled, this option restricts belongsToMany marshalling to only use the _ids key and ignore all other data." The first half of the sentence was promising, but the explanation says different, and I actually tried it without success.

First:
Is there an easy way to save a recordset, i mean multiple records, but only the "new ones"?
Yes there is you can validate it in model, something like this:
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->isUnique('passportNumber'));
return $rules;
}
This will prevent to save a duplicate passport number register, but you can different.
I have a table users and a users form in the view. First field you must enter is passport number, so if user already exists in database the rest of the form will be auto completed and disabled to prevent changes but if passport dont exist then you have to enter all data.
There is two different ways to do this:
First you have your form, you develop an ajax function when you fill the first field (passport number) this ajax function do a request to your controller to search for a passport with that number if find something get data and fill others fields and turn them just readable, else just nothing and let user fill the fields by himself
second add a step-by-step where you first do a form to try find by pass number, user fill this only field with a number then submit, on submit this search for a record, if find fill the entire next step fields, else the next step will be the rest of form with the fields to be filled.
This may help you too: https://book.cakephp.org/3.0/en/orm/validation.html
Tell me how you decided to do :)

Related

Salesforce Screen flow search and display record information

I'm working on a screen flow. The idea is to have a lookup component the user can search a contact. Then I would like to display the information from the Contact (Account Name, Contact Name, Number Email) and I would like to be able to have the user update that information if needed. I just stomped on how it can be done. I know it should be able to be pulled from the record ID in some type of way and maybe use an Assignment to display the information. Can someone guide me on a next step or if anyone has an instructional video would be helpful.
Thanks
You wouldn't be able to display the looked up contact's fields on that screen as soon as you populate that field. That would be something only possible in code (aura component or lwc).
What you can do, however is -
Get Record element after that screen element. (Get Contact, where Id = lookupcomponent.recordId)
Then EITHER:
use the new Fields (BETA) option on another screen Fields (BETA)
(this method is easier, doesn't have as much control and is limited on fields, depending on data type, you can use)
add inputs one by one and set the default values Add Fields One By One
(this method allows more control)
Then, you will need an update element. If you used the Fields (BETA) you can just update the record variable. If you did the inputs one by one, you will need to update the contact and set fields individually.
Full Flow Example

Revoking edit access based on record's field

Background
I'm trying to prevent editing of a record based on one of it's fields having a non-blank value. I'd like to do it with permissions and sharing rules, but I'm not sure what the best way to set it up is. I'm trying to avoid triggers and adding extra fields if possible.
At my work, we're using an object to represent a Job being worked on in the field. A lot of different users need access to edit a Job in progress and so it's set with public read/write sharing rules. Once a job is completed though, it's processed by billing and has an Invoice # field filled in. Once that's set, only billing users should be able to edit the record.
What I've Tried
I first tried a validation rule that fires when Invoice # is filled and the user's profile is not the billing team. This worked initially, but caused triggers to fail when a billing user tried to edit afterwards allows the Invoice # to be set and saved, but triggers that fire afterwards then trip and fail on the validation rule.
Next and currently, I'm playing with sharing rules to accomplish this, which seems like the right path to go down. However, I also want to prevent editing of child records in a related list under the Job. It doesn't seem like I can use cross-object fields or any formula fields in a sharing rule, so I'm not sure if this is possible without using triggers.
What I Could Try
I know I can use sharing rules with a trigger to set a sort of Editable or Locked checkbox field on the record and children when the Invoice # field is set, but I really don't like the idea of that much overhead for something seemingly simple.
Question
What's the cleanest way to allow users to edit a record until a certain field is non-null, after which only a subset of users can edit? If possible, how can I extend that to child objects in a related list?
I would use an on before update trigger to accomplish this.
Something like this:
if (Trigger.isUpdate && trigger.isBefore && UserInfo.getUserRoleId() != billingRoleId) {
for (Job__c theJobRecord : Trigger.new) {
if (String.isNotBlank(theJobRecord.Invoice_Number__c)) {
theJobRecord.Name.addError('You do not have permissions to modify this record!');
}
}
}
If you want to do this inside a Validation rule check if the job record invoice number is blank or use the global merge field $UserRole and check the name of the role else fail the Validation rule.

CakePHP - Prevent model data being save in a saveMany call when certain fields are blank

I have scenario where I have 3 different models data being saved from one form, by means of the saveAll pass through method in CakePHP 2.x
The models on the form are:
Project
ProjectImage
ProjectAttachment
1 and 2 are saving perfectly, but I am having problems with the ProjectAttachment fields. If you look at the following image, the fields in question are at the bottom right, highlighted by a red frame: http://img.skitch.com/20120417-bnarwihc9mqm1b49cjy2bp13cf.jpg
Here is the situation:
I have named the fields as follows: *ProjectAttachment.0.project_id*, ProjectAttachment.0.name .... *ProjectAttachment.6.project_id* etc where the numbers are relative to the already present amount of attachments the user has added, as there is no limit. So if the user has ALREADY added 6 documents, the field would be called ProjectAttachment.7.id and so on. This is because of the naming convention when using the saveAll method.
I have made use of two hidden fields, one to store the user ID, the other to store the project ID.
The problem is that if the user does not fill in the Document Title and select a file to upload, the form fails! If I remove all validation rules, the form no longer fails BUT a blank ProjectAttachment record is inserted.
I suspect the problem may also be that the project_id and user_id fields (that are hidden) already have values in them?
I gave it some thought, and came up with a simple concept: In my controller, before the saveAll call, I would check to see if a blank Document Title field was submitted, and if so, I would completely eliminate the relevant array entry from the $this->request->data['ProjectAttachment'] array, but this did not seem to work.
To clarify, I am not looking for validation rules. If the user decides they only want to upload images and not touch the ProjectAttachment form, them the saveAll operation must not fail, it must simple not attempt to save the blank fields in the form, if this is at all possible.
Any suggestions?
Kind regards,
Simon
Well it seems that the solution was far simpler than I had initially thought! I was partially on the right track, and I had to unset the variable ProjectArray key in the $this->request->data array if I had encountered any blank fields.
I did this as follows:
if(!empty($this->request->data['ProjectAttachment'])){
foreach($this->request->data['ProjectAttachment'] as $key => $value){
if(is_array($value) && array_key_exists('upload_file', $value)){
if($value['upload_file']['name'] == ''){ // Blank document file
unset($this->request->data['ProjectAttachment']);
}
}
}
}
I hope someone finds this somehow useful in some form or another.
Kind regards,
Simon

asp mvc, best solution to store user input data before it will be saved to database

for example i've got a form with some input fields(every form and it's inputs with validation rules are stored in database).
Every input got it's own OnChange() which posts json (i.e. new value, input element name, ...) to controller for validation, if validation passes the new value must be saved somewhere until user clicks submit button, which will save all data to database table.
And here i'd like to ask, what this special place between ui and database can be ?
p.s.
also if user closes browser/form the next time he'll come back i need to ask him if he would like to start from an empty form or fill form with values he previously entered there.
Thank You !
Cookies or intermediary database table would work for this case.
for an intermediary database like that, you could use something like MongoDB, it is really easy to get it started, you just work with the classes you have, don't need to setup any schema, you just save the objects
http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial
If you are submitting the entire form at the end, why can't you just store the values at that time? Is this a multi-page form(s)? Why not allow the database records to be partially filled? You could always add a bit column to mark the record as complete or incomplete. This would be much simpler than duplicating your table structure.

Is it possible in Quickbase to create a URL to an Add Record page which partially fills in fields on the form?

I have a Quickbase app with a form for adding records. On an intranet page, I have a link to the add record form. When a user clicks on that link, Quickbase opens the add record form. However, I would like to supply values for some of the fields on the form as parameters in the URL.
I am aware of API_AddRecord, but as I understand it, that can only be used for completely filling in all required fields and saving the record. The disprec parameter can be used to see the record, but doesn't keep the record in add mode without committing the record.
What I need to do, is to fill in a couple of the fields, but keep the record in add mode, allowing the user to fill in a couple more fields. The URLs on the intranet page are actually generated in a grid, so there are some fields that are already known then the user clicks on the link, and I don't want the user to have to type them in again.
Can this be done? Thanks for your input.
Sure. Folks do this all the time. Use a=API_GenAddRecordForm&_fid_1="foo"&_fid_2="bar" where the number are the Field IDs of the fields you want to fill out, and "foo" and "bar" are the values you want to fill in.

Resources