0

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.

4
  • what exactly do you need to validate, please include your relevant code .
    – glls
    Commented Jan 7, 2019 at 14:10
  • I just updated my original post. I don't know why but the code is not beautifull.
    – Oupat
    Commented Jan 7, 2019 at 16:27
  • its till not clear what validations you would like to perform.
    – glls
    Commented Jan 7, 2019 at 17:29
  • I would like to check if the data is not an injection to hack, something like that. But maybe Salesforce do that every time without to need to check by myself?
    – Oupat
    Commented Jan 7, 2019 at 21:18

1 Answer 1

3

You should probably update your post with your last comment, additionally, based on all your recent posts, it would seem you could benefit from following Trailhead modules, such as this one -> Injection Vulnerability Prevention

Once you have gone through the module, if there is anything you still believes need clarification, dont hesitate to open a new post.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .