0

I have a lightgning component for Account Creation / update. In this component i use as input field a date field. I want somehow to validate in Apex (Component Controller) that the date the user has input is valid. For example, now what happens is if user inserts on this field the value "asdasdsadas", the record is saved with null date. I want before it gets saved to call an apex function to check that in this field the format is 'yyyy-mm-dd'. Any suggestions ?

0

You can try casting the user input into a date and return a custom error if it is not a valid date string.

String input = '2019-04-08';

try {
   Date d = Date.valueOf(input);
}
catch(TypeException e) {
   // Throw a custom exception and handle the message in your component
}
| improve this answer | |
0

Since lightning:inputField already validates the date entry, why do you need to do it in Apex? See documentation.

Date: Displays an input field for entering a date. The date format is automatically validated against the user’s Salesforce locale format

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.