Jump to content

Make a field required programmaticaly


Recommended Posts

Hello, i am experience the following logical problem. Prestashop has a company field in address registration form. When a user types something in this field then the vat field is shown.

 

I want to make the VAT field required if company field is not empty. I don't want to be required if the company is empty.

I cannot make vat field required by default because not all customers have company vat number.

 

I would like not to edit core files. I would prefer make a module for example or something.

 

Is this possible ?

Link to comment
Share on other sites

I found a way to figure it out but i am not sure it is the recommended one.

 

I create two overrides in overrides/controller/front/AuthController.php and another one AddressController.php

 

i override the processSubmitAccount() and the processSubmitAddress() respectively. In processSumbitAccount() for example i added the following code

$error_vat = false;
if (Tools::isSubmit('submitGuestAccount') || Tools::isSubmit('submitAccount') || !Tools::getValue('is_new_customer'))
{
   if (Tools::getValue('company') && !Tools::getValue('vat_number'))
   {
     $error_vat = true;
   }
}

if ($error_vat)
{
   $this->errors[] = Tools::displayError('The VAT should not be blank once the Company name has been typed.');
}

I clean /cache/class_index.php and it works like a charm!

Link to comment
Share on other sites

You can also try to do this with JavaScript so the page won't reload with an error but on the other hand show a pop up message that tells the users to enter the vat_number.

 

I think this is better UXwise

  • Like 1
Link to comment
Share on other sites

You can also try to do this with JavaScript so the page won't reload with an error but on the other hand show a pop up message that tells the users to enter the vat_number.

 

I think this is better UXwise

 

Nice approach :) Thank you sharing with me

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...