React-hook-form update validation - reactjs

I want to update validation on one field depending on the state of the other. The situation I've got is as follows:
Fill the email field properly (no errors shown)
Fill the phone number field properly (no errors shown)
Delete phone number (no errors shown because email is still valid and phone number is dependent on it).
Delete email - now here is a weird behaviour - email field shows proper error but phone number shows old error ('invalid phone number') but it should show new error, same as email does ('we need at least one form of contact').
When I focus on the phone number field and then unfocus it 'updates' and shows proper error.
What I want to do is show proper phone number error on step 4. Without the need to focus/unfocus.
I hope that codesandbox will make things clearer:
https://codesandbox.io/s/hopeful-nightingale-0m41z

I think you need to update the mode based on your requirement in place of onTouched.
https://react-hook-form.com/api#errors

Related

Codename One - Validate only one input at a time

A problem of the Validator class is that it validates all the inputs to which a constraint is added (that means that an error message is shown in not already filled inputs or selected pickers):
Validator val = new Validator();
val.addConstraint(title, new LengthConstraint(2));
val.addConstraint(price, new NumericConstraint(true));
In this example, the validator will show an error for both title and price even if the user didn't entered a price yet.
It would be more intuitive that an error is shown only after the user has given the input, that means to show an error for the price only after that a price is given.
I didn't find a code to implement this behavior.
My first thought was just create a validator which will check if something was modified and add it to the validation chain. However, that would mean the input could be valid if you didn't enter everything.
This is a feature that should be implemented in the rendering logic for this class. In Validator itself. You can file an RFE for that or just implement it in a pull request.

How to validate form field with Parsley JS if field input has been populated automatically?

I have a signup form where the user is supposed to enter amongst other personal details also his phone number's country prefix (e.g. '+49' for Germany). Now I have a script running which automatically detects the country prefix according to the user's IP geo location.
If the IP location is successfully determining the user's current location, it puts the country code automatically in the signup form's prefix field.
This means that the user doesn't have to enter the prefix manually.
My question is: once the signup form is loading, and all fields are empty except for the country prefix, how can I validate the country prefix field successfully prior to hitting the submit button?
I tried all kinds of triggers from the jQuery Events page as suggested by the ParsleyJS documentation but I don't get it done..
Any help on this would be gladly appreciated!
Edit to further clarify: If you're visiting the signup page, the IP location script detects your location, translates that into your country's phone prefix, and puts that number into the "Prefix" form field. So you don't have to put there anymore by yourself. This works only if the the IP locations script successfully identifies your location of course, however this works in most of the cases. So what I would like to achieve is that this form gets it's green validated background, although you didn't put in the prefix yourself. In other words, the prefix is already in the field in most of the cases.
I'm using the data-parsley-triggers, no jQuery or JS to validate the whole form.
You can see in the picture that I entered a first name, Foo, which validated upon data-parsley-trigger="change". The blue arrows show the fields which have been filled automatically by the IP location - and they are not validated as I'm missing the right trigger.
It's not clear what you are trying to achieve (why validate the field when it's clear it will be valid), but why not simple $('input[name="phone_prefix"]').parsley().validate()?

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 do I make it so that a user must either enter text or select a check box on Salesforce?

I am using salesforce for a group project here at SJSU, the thing is this is our first time using it, and we are having a little bit of trouble programming some things on Salesforce.
What we are trying to do:
We have a section on one of our forms where users will give us authorization to use Data they submit, the usage of this data will be under the terms set by those who submit the data. If the user does not want to set limitations on their authorization for us to use the data then they can select a checkbox called labeled "none" which basically means they are setting no limitations on what we can use their data for.
What we want to do is, if users select the checkbox then we want the users to not be able to enter any text into the textbox. If users enter text into the box while the checkbox is checked we want an error message to appear which will let the user know that no data can be entered into the box if the checkbox is checked. However if no checkbox is checked then we want users to be able to enter the data. How do we go about doing this
AND (None_c = True then Limitations_c has to be empty, elseif None_c = False then Limitations_c cannot be empty.)
If you want to leverage the out of the box UI (Page Layout), then you will likely want to use Validation Rules.
https://help.salesforce.com/HTViewHelpDoc?id=fields_defining_field_validation_rules.htm&language=en_US
The gist of it is that you want to define Error criteria, so when this criteria evaluates to true, and error is thrown. In that case, you should be able to construct something similar to the following:
Checkbox1__c && NOT(ISBLANK(Text2__c))
Validation rules would be the easiest way to execute this, though checking the box would not dynamically prevent users from entering text. With a validation rule, it just wouldn't let a users save.
The nice thing about using validation rules is that you can construct them such that checking None__c will not vomit on the user unless they actually modify Limitations__c. ISCHANGED() is great for that.
If I could suggest an alternative, the way I would implement this is to treat an empty Limitations__c as None__c = True. It simplifies things for users, and you can add a formula-driven checkbox if a checkbox element is truly required.

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