Search code examples
phpvalidationprestashopprestashop-1.6required

Php phone validation doesn't work


I'm working with Prestashop 1.6, I'm not really into PHP, but I wanted to make 1 change in website look. In default I have 2 phone inputs: for home and mobile. It looks like that:

<div style="display: none" class="form-group is_customer_param">
                    <label for="phone">{l s='Home phone'}{if isset($one_phone_at_least) && $one_phone_at_least} <sup>**</sup>{/if}</label>
                    <input type="text" class="text form-control validate" name="phone" id="phone" data-validate="isPhoneNumber" value="{if isset($guestInformations) && isset($guestInformations.phone) && $guestInformations.phone}{$guestInformations.phone}{/if}" />
                </div>
                <div class="{if isset($one_phone_at_least) && $one_phone_at_least}required {/if}form-group">
                    <label for="phone_mobile">{l s='Mobile phone'}{if isset($one_phone_at_least) && $one_phone_at_least} <sup>*</sup>{/if}</label>
                    <input type="text" class="text form-control validate" name="phone_mobile" id="phone_mobile" data-validate="isPhoneNumber" value="{if isset($guestInformations) && isset($guestInformations.phone_mobile) && $guestInformations.phone_mobile}{$guestInformations.phone_mobile}{/if}" />
                </div>
                {if isset($one_phone_at_least) && $one_phone_at_least}
                    {assign var="atLeastOneExists" value=true}
                    <p class="inline-infos required">* Podaj nr telefonu</p>
                {/if}

I've made first one not visible, because I thought that one filled field will be enough. And it seems to be. But I can submit a form without entering mobile phone number. Can you see maybe where is problem here? Or maybe how can I make it working?

Example of field which works good, is really required and not paired with another field:

    <div class="required form-group">
                        <label for="lastname_invoice">{l s='Last name'} <sup>*</sup></label>
                        <input type="text" class="form-control validate" id="lastname_invoice" name="lastname_invoice" data-validate="isName" value="{if isset($guestInformations) && isset($guestInformations.lastname_invoice) && $guestInformations.lastname_invoice}{$guestInformations.lastname_invoice}{/if}" />
                    </div>

I've tried to validate mobile this way:

<div class="required phone_mobile form-group">
                    <label for="phone_mobile">{l s='Mobile phone'} <sup>*</sup></label>
                    <input type="text" class="text form-control validate" name="phone_mobile" id="phone_mobile" data-validate="isPhoneNumber" value="{if isset($guestInformations) && isset($guestInformations.phone_mobile) && $guestInformations.phone_mobile}{$guestInformations.phone_mobile}{/if}" />
                </div>

but without success...


Solution

  • It looks like the $one_phone_at_least variable is not set to true. From the back-office, go to Preferences > Customers and make sure that "Phone number is mandatory" is turned on. That should fix it.

    By the way, the required class should be used on the input, not the form-group container. If you made that one change, you could have prevented the user from submitting the form, but if they worked around it to send the data without a phone number, the server would allow it. By following the steps in my first paragraph, the server will also demand that a phone number is set.