React Final Form: has a value existed? - reactjs

How is it possible to check has a value already existed using React Final Form? I'm interested in the general approach. Also, be thankful for some links, etc, because could find nothing relevant.
Now details:
I have a form: a text input "User name" and a button "Submit".
I type a value (let say, "John") and press "Submit".
The value is saved into a database and becomes to display on my web page.
The input field is cleared and I am able to enter the new value.
I'm entering "John" again.
I should get an error message "Such name has already existed" once I moved the focus out of the field, or click on the "Submit" button.

You'd generally accomplish this by doing a preflight request to the server via fetch.
Send the field value(s) over whenever you do validation (e.g. on change or on blur) and have the server report back any issues (e.g. "Name already taken").

Implement some checking logic In the POST request. Before you write the logic to add the user to the database, check If a user with that username already exists, and If It does, return an error. It also depends on the database you are using how they handle searching for documents.
If its MongoDB you need to use the .find() method and find by username. If you find a matching username, return an error saying to enter a unique username.

Related

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

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 :)

Show and hide server-side errors in AngularJS 1.3+ forms

I'm using the Angular framework with Angular Material controls in my recent application. I'm looking for a good solution for the following problem:
A form form with an input field named nickname is shown to the user. After the user has chosen a nickname and submitted the form, the server checks whether the nickname has already been taken. In that case, it returns an error to the Angular client.
To show an appropriate error to the user, the code then calls form.nickname.$setValidity('nicknameTaken', true). The new ngMessages module is used to display the error to the user. Further form.$isInvalid is used to disable the form controls to prevent the user from resubmitting the invalid nickname.
My problem is now the following: I'd like to have the error nicknameTaken automatically being removed as soon as the user begins to edit the form fields again. What is a good way to do this? Is there a predefined route to go when it comes to server-side validation errors of this kind? (Note that I am not asking for asynchronous validation because I only want to contact my server when the form is actually being submitted.)
I would write a normal validator directive instead. Something like
<input blacklist="takenNickNames" .../>
This directive would simply add a validator to the input, and this validator would make the input invalid if the model value is contained inside the given takenNickNames array (and valid if it's not present).
The takenNickNames array would be empty initially. When the form is submitted and the error comes back, the controller would add the invalid nick name to the array.
So, every time the user would enter something, the validator would be triggered, and would set the field valid or not, based on the taken nicknames stored in the array.
Here is a working example.

How can I check to see if a username is already used with AngularJS?

In my form I have a username field that is user for registration. I would like to make this trigger an error message on the field. Something like the error message I would get if I did not enter a value on a field with ng-required.
Can someone tell me how I can make it so that when a user leaves the field then an HTTP request is sent to the database to check for an existing username and then if present an error condition is set for that field.
I am not really looking for specific HTTP request code but just even some pointers would be a big help.
Here's a nice guide on how to check a name field using custom directive and new features from 1.3

How to constantly check a JTextArea

I'm trying to make a JTextArea that returns the value "username". I have a database
that has Username as a column, and I already did research on how to SELECT and query. The only
thing is, that I wish for the JTextArea to validate username AS THEY ARE TYPING their username down, like YouTube. This is primarily to keep each person from having the same username. ("Smilese is unavailable" for example, outputed as a Jlabel as the person is typing. ) Is there any Listener of JTextArea that checks AS THE PERSON IS TYPING? Or is there any code that anyone would recommend? Anyone? So to recap:
1) I have a database with a column name
2) I know how to query and find the username in the column when necessary
3) The problem is that I don't see a JTextField property (eg Action event) that can check "if the user is busy typing", only KeyTyped, which stops the user from entering more than 1 charactor before running(taking THAT one letter as a variable and outputting).
4) Any listeners? or anything of the JTextArea that can help? Any other components (JPanel, etc) that might do the trick can be mentioned here too :)
Swing uses DocumentListeners for this purpose. There are two entries in the Java tutorial: How to Write a Document Listener and Listening for Changes on a Document.
You basically need to implement a DocumentListener and add it to the Document of the JTextArea / JTextField. You can then react to any changes.
JTextArea that returns the value "username".
I wonder why you are not using JTextField for username.
Anyways, you can simpy bind a keyListener and do the required as the keys are typed.
Here's how to do it.

Display Alert box through the trigger - salesforce

Is it possible to display an alert message (not error message) through a trigger? I have written a trigger that checks for duplicate accounts in the system. The trigger at the moment gives an error message to the user telling that there is a duplicate account. But, if the user changes the value of a field "Is record near to duplicate?" to YES, the trigger allows the user to save the record.
But, I want to display the error message in an alert pop up box like "Account with this Name exists,are you sure you want to continue" and then user clicks Yes and the record gets created. Any thoughts on how I can do this. My code is below:
for(Account account: System.Trigger.New)
{
if(accountMap.containsKey(account.Physical_Street__c)==accountMap2.containsKey(account.Phone))
if(accountMap.containsKey(account.Physical_Street__c)==accountMap3.containsKey(account.Name))
if(account.Is_record_near_to_duplicate__c.equals('No'))
{
account.addError('Account with this Name,Street and Phone Number already exists. If you still wish to create the agency change the value of field "Is Record Near To Duplicate" to YES');
}
}
If you really need an alert box then you could create a custom javascript button that uses the ajax toolkit to replace the standard save button.
However, as Baxter said you are getting pretty far from standard salesforce look and feel. I would recommend instead to add an error on the checkbox field instead of the object so it is clear to the user what they need to select.
if(account.Is_record_near_to_duplicate__c.equals('No'))
{
account.Is_record_near_to_duplicate__c.addError('Agency with this Name, Physical Street and Phone Number already exists. If you still wish to create the agency change the value of field "Is Record Near To Duplicate" to YES');
}
Unless you write a custom visualforce page that processes the error message and then displays it as a popup there's no way to do this.
If you wanted the alert to modify the data you may look at using the ajax api to set the Is_record_near_to_duplicate__c field to 'Yes' but either way you're getting pretty far from the standard functionality and interface with this.
It is not straight forward to implement a pop-up alert for a trigger error, and pop-ups went out-of-fashion a long time ago. If you are going through the hassle, you might as well implement a custom soultion on a VF page.
Throw a custom exception(for dupe accounts) from the trigger, and catch it in your controller. When you catch this exception, you can dynamically displayed section on the page which asks the user to confirm his/her action. When the user confirms the action, the page will do the rest.
Hope this makes sense!
Anup

Resources