0

What is the right approach for the below line?

ng-pattern="(true)? apply-pattern : dont-apply-pattern"

My Issue is, I'm hiding the ng-pattern field when its not needed, but still its not allowing the form to be valid.

I tried the field to be disabled, but no luck.

The code is:

<input type="text" ng-hide="param.paramType == 'java.lang.boolean'" 
       ng-pattern="param.paramValidatePattern" class="form-control input-sm" name="paramV" 
       ng-model="param.paramValue" ng-required="param.paramType != 'java.lang.boolean'" 
       ng-disabled="param.paramType == 'java.lang.boolean'" />
SoEzPz
  • 14,958
  • 8
  • 61
  • 64
shaik
  • 275
  • 2
  • 6
  • 11
  • Try ng-if instead of ng-hide. If the condition is false the element will be removed from the dom and therefore from the form. – Gruff Bunny Feb 25 '14 at 16:52
  • I've marked my answer for deletion. I was referring to latest Angular docs (http://docs.angularjs.org/guide/expression) where they state that expressions do not accept control flow statements but after testing with latest branch it seems this is no longer true. – package Feb 25 '14 at 20:19
  • It seems ng-if does not work in the used angularjs version. is there any other choices. I'm using (AngularJS v1.0.5) – shaik Feb 26 '14 at 06:54

1 Answers1

0

See if this special conditon will work for you in your version...

<form name='myFormName'ng-submit="submitFormFunction()">
<input type="text" name="mySpecialPatternField" .../>

In JS code (directive || controller)

$scope.submitFormFunction = function(){
   if( specialConditionIsMet ){
       // then we don't want the pattern input to matter in form validation                                    
       $scope.myFormName.mySpecialPatternField.$setValidity('pattern', true); 
   }
}

might need to be false, I don't recall, but they are swapped in angularjs from what you would think they should be.

If you use $setValidity, and if you do and have more than one validation for that input (besides pattern, say requierd as well) you will need to loop through the $scope.myFormName.mySpecialPatternField.$error

object to get the keys, then set each one of them to true; which will set $valid to true for each error, and thus the form to $valid = true as well.

However, this may or may not be available in your version. Check...

graphefruit
  • 364
  • 4
  • 15
SoEzPz
  • 14,958
  • 8
  • 61
  • 64