what function can I use when I need to validate a form with Apex on the server side controller?
I can't find functions on the internet.
This is my code:
View:
<form class="slds-form--stacked">
<table>
<tr>
<th>Nom *</th>
<th>Prénom *</th>
<th></th>
</tr>
<tr>
<td>
<lightning:input aura:id="lastname" value="{!v.account.LastName}" />
</td>
<td>
<lightning:input aura:id="firstname" value="{!v.account.FirstName}" />
</td>
<td></td>
</tr>
<tr>
<th>Téléphone fixe *</th>
<th>Téléphone portable *</th>
<th></th>
</tr>
<tr>
<td>
<lightning:input aura:id="homephone" value="{!v.account.Phone}" />
</td>
<td>
<lightning:input aura:id="mobilephone" value="{!v.account.PersonMobilePhone}" />
</td>
<td><p>Merci de renseigner au moins un des deux numéros</p></td>
</tr>
</table>
<table>
<tr>
<th>Adresse e-mail *</th>
</tr>
<tr>
<td><lightning:input aura:id="mobilephone" value="{!v.account.PersonEmail}" /></td>
</tr>
</table>
<lightning:checkboxGroup aura:id="marketingNews" label="" options="{!v.options}" value="{!v.valueMarketingNews}" onchange="{!c.handleChange}" />
<lightning:button label="Enregistrer les modifications" class="slds-m-top--medium" variant="brand" onclick="{!c.submitForm}"/>
</form>
This is the client-side controller:
submitForm: function(component, event, helper) {
var action = component.get("c.updateAccount");
action.setParams({
"salutation": component.get('v.salutationResult'),
"firstName": component.get('v.account.FirstName'),
"lastName": component.get('v.account.LastName'),
"phone" : component.get('v.account.Phone'),
"personMobilePhone" : component.get('v.account.PersonMobilePhone'),
"personEmail" : component.get('v.account.PersonEmail'),
"marketingNews" : component.get('v.valueMarketingNews')
});
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
var storeResponse = response.getReturnValue();
}
});
$A.enqueueAction(action);
}
And the server-side controller (Apex):
public static Boolean updateAccount(String salutation, String firstName, String lastName, String phone, String personMobilePhone, String personEmail, String marketingNews){
Boolean result;
try{
String userId = UserInfo.getUserId();
map<String, String> userMap = Utils.getAccountIdFromCommunityUser(new List<String>{userId});
if(userMap.containsKey(userId)){
Account accountToUpdate = new Account(
Id = userMap.get(userId),
Salutation = salutation,
FirstName = firstName,
LastName = lastName,
Phone = phone,
PersonMobilePhone = personMobilePhone,
PersonEmail = personEmail
);
update accountToUpdate;
result = true;
}else{
result = false;
}
}catch(Exception e){
result = false;
System.debug('--- error : ' + e.getMessage());
}
return result;
}
It is a simple form, just validate input data.