5

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>

1 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.