How to Remove Validation for CITY Field in Magento Onepage Checkout

onepage-checkout

I want to remove validation for field if specific country is selected for example if User selects India from country then I need to skip validation fro city in other words if India is selected no need for user to enter City

So far I tried getting this using if..else conditions but I cannot able to achieve this

this is my code

<div class="field">
            <?php $country=$this->getCountryHtmlSelect('shipping')?>
            <?php echo $country; ?>
            <?php if($country!=India)?>
            <label for="shipping:city" class="required"><em>*</em><?php echo $this->__('City') ?></label>
            <div class="input-box">
            <input type="text" title="<?php echo $this->__('City') ?>" name="shipping[city]" value="<?php echo $this->escapeHtml($this->getAddress()->getCity()) ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('city') ?>" id="shipping:city" onchange="shipping.setSameAsBilling(false);" />
            </div>
            <?php else: ?>

            <label for="shipping:city" <?php echo $this->__('City') ?></label>
            <div class="input-box">
            <input type="text" title="<?php echo $this->__('City') ?>" name="shipping[city]" value="<?php echo $this->escapeHtml($this->getAddress()->getCity()) ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('city') ?>" id="shipping:city" onchange="shipping.setSameAsBilling(false);" />
            </div>

           <?php endif; ?>
</div>

Best Answer

You need to implement the switch for this validation on client-side.

What you are trying above is, to check the country during template generation. It could work only once (on load) when the customer selected India before but it won't because it's syntactically wrong in any way: <?php if($country!=India)?> as $country is the output for the dropdown and it's syntactically wrong in PHP. 'India' needs to be put in quotes.

What I recommend:

  1. Remove the if/else from the phtml file
  2. Add a JavaScript event that fires when the checkbox is changed.
  3. On change of the country dropdown, add/remove the validation class (<?php echo $this->helper('customer/address')->getAttributeValidationClass('city') ?>) from the input field class attribute.
Related Topic