sbayrak
asked on
Making address2 field required in PrestaShop
We're using PrestaShop 1.5.4.1. We need to make address2 field "required" like address1, so that users cannot submit the address form if they don't fill the address2 field. I don't mean putting asterisk* to its label inside its .tpl file, I mean making it really required.
SSCCE -- What have you tried so far? Please show us the code you're using, thanks.
ASKER
I only edited the relevant address2 label inside /themes/mytheme/address.tp l and added
an asteriks * by using <sup>*</sup> so far, nothing else...
{if $field_name eq 'address2'}
<p class="required text">
<label for="address2">{l s='Address (Line 2)'} <sup>*</sup></label>
<input type="text" id="address2" name="address2" value="{if isset($smarty.post.address 2)}{$smart y.post.add re
ss2}{else}{if isset($address->address2)} {$address- >address2} {/if}{/if} " />
</p>
{/if}
thanks in advance.
an asteriks * by using <sup>*</sup> so far, nothing else...
{if $field_name eq 'address2'}
<p class="required text">
<label for="address2">{l s='Address (Line 2)'} <sup>*</sup></label>
<input type="text" id="address2" name="address2" value="{if isset($smarty.post.address
ss2}{else}{if isset($address->address2)}
</p>
{/if}
thanks in advance.
What is the code of address one?
You have to add validation field
* will only display it, this will not validate the field.
You have to add validation field
* will only display it, this will not validate the field.
ASKER
Well I can't see a validation in address1 field also, at least in address.tpl file.
They're both almost identical. Somewhere else, there should be a separate function to control the validation doings I think...
They're both almost identical. Somewhere else, there should be a separate function to control the validation doings I think...
{if $field_name eq 'address1'}
<p class="required text">
<label for="address1">{l s='Address'} <sup>*</sup></label>
<input type="text" id="address1" name="address1" value="{if isset($smarty.post.address1)}{$smarty.post.addre
ss1}{else}{if isset($address->address1)}{$address->address1}{/if}{/if}" />
</p>
{/if}
{if $field_name eq 'address2'}
<p class="required text">
<label for="address2">{l s='Address (Line 2)'} <sup>*</sup></label>
<input type="text" id="address2" name="address2" value="{if isset($smarty.post.address2)}{$smarty.post.addre
ss2}{else}{if isset($address->address2)}{$address->address2}{/if}{/if}" />
</p>
{/if}
The address validation is located in classes/Validate.php
You can duplicate the class and add a new variable name $address2
I have not tested I'm not sure if it's will work ok.
Backup your files prior trying this modification.
add this class to
classes/Validate.php
and change your new code in address.tpl for this
You can duplicate the class and add a new variable name $address2
I have not tested I'm not sure if it's will work ok.
Backup your files prior trying this modification.
add this class to
classes/Validate.php
/**
* Check for a postal address2 validity
*
* @param string $address2 Address2 to validate
* @return boolean Validity is ok or not
*/
static public function isAddress($address2)
{
return empty($address2) OR preg_match('/^[^!<>?=+@{}_$%]*$/ui', $address2);
}
and change your new code in address.tpl for this
{if $field_name eq 'address2'}
<p class="required text">
<label for="address2">{l s='Address (Line 2)'} <sup>*</sup></label>
<input type="text" id="address2" name="address2" value="{if isset($smarty.post.address2)}{$smarty.post.addre
ss2}{else}{if isset($address2->address2)}{$address2->address2}{/if}{/if}" />
</p>
{/if}
ASKER
Adding your code to classes/Validate.php causes WSOD (white screen of death). I tried some different variations of it, but the result didn't change, all WSOD. I think your logic is right but somehow it does not work within classes/Validate.php. I changed static < > public order, OR with || and etc., it didn't work...
Have you also edited your address2 code line address.tpl ?
I will install it and make test tonight
I will install it and make test tonight
I think this is because the variable is not declared at the begining of code page.
Check for other occurrence of $address in the code.
Could you enable error display
http://www.php.net/manual/en/function.error-reporting.php
error_reporting(E_ALL);
I will try to install it and make test tonight
Check for other occurrence of $address in the code.
Could you enable error display
http://www.php.net/manual/en/function.error-reporting.php
error_reporting(E_ALL);
I will try to install it and make test tonight
ASKER
I refered the changes I did in address.tpl previously above;
https://www.experts-exchange.com/questions/28300789/Making-address2-field-required-in-PrestaShop.html?anchorAnswerId=39670950#a39670950
Yes, as you said, it cannot be redeclared;
PHP Fatal error: Cannot redeclare ValidateCore::isAddress() in classes/Validate.php on line 359
Actually there're no other occurrences of $address in Validate.php file.
Thank you.
https://www.experts-exchange.com/questions/28300789/Making-address2-field-required-in-PrestaShop.html?anchorAnswerId=39670950#a39670950
Yes, as you said, it cannot be redeclared;
PHP Fatal error: Cannot redeclare ValidateCore::isAddress() in classes/Validate.php on line 359
Actually there're no other occurrences of $address in Validate.php file.
Thank you.
Ok we have to rename isAddress to isAddress2
static public function isAddress($address2)
in classes/Validate.php
So it should now be like this:
Let me know if there are other occurrence of isAddress
static public function isAddress($address2)
in classes/Validate.php
So it should now be like this:
/**
* Check for a postal address2 validity
*
* @param string $address2 Address2 to validate
* @return boolean Validity is ok or not
*/
static public function isAddress2($address2)
{
return empty($address2) OR preg_match('/^[^!<>?=+@{}_$%]*$/ui', $address2);
}
Let me know if there are other occurrence of isAddress
ASKER
Ok., now it doesn't end with WSOD. Site is working but address2 field can still be submitted empty. I mean it didn't become a required field.
Here is the latest version of funct. I derived from its working brother ($address);
And, there's no any other occurrence of isAddress.
Thanks
Here is the latest version of funct. I derived from its working brother ($address);
/**
* Check for a postal address validity
*
* @param string $address2 Address to validate
* @return boolean Validity is ok or not
*/
public static function isAddress2($address2)
{
return empty($address2) || preg_match('/^[^!<>?=+@{}_$%]*$/u', $address2);
}
And, there's no any other occurrence of isAddress.
Thanks
Just for a test could you comment the derived function and replace it with the one I provided
ASKER
Sure, I copied and pasted your code as is and nothing changed. No WSOD but field is still optional.
ok thanks I will try to find out and get back to you asap
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
No, actually I didn't add anything to anywhere else different than I mentioned here up until now.
ASKER
It works now. Thank you so much for your efforts to resolve it.