0

I'm trying to validate a phone number input field in a form. I'm using intl-tel-input (which is great, BTW) to get the extension and full number, then pass the value to a hidden field so I can send the form data to the server.

This is the displayed form:

<div class="col-md-6">
   <div class="form-group">
      <label class="control-label" translate="contacta.form.phone" for="field_phone_pasar">Teléfono móvil</label>
      <!-- <input type="hidden" name="phone" id="field_phone" ng-model="vm.form.phone" ng-maxlength=12 maxlength=12 /> -->
      <div>
         <input type="text" class="form-control form-control-200" name="phone_pasar" id="field_phone_pasar" ng-maxlength=15 maxlength=15 ng-model-options="{ updateOn: 'blur' }"/>
         <div ng-show="contactaForm.phone.$invalid && contactaForm.phone.$touched">
            <p class="help-block" ng-show="contactaForm.phone.$error.required"
               translate="contacta.form.validation.required">This field is required.</p>
            <p class="help-block" ng-show="contactaForm.phone.$error.minlength"
               translate="contacta.form.validation.phoneminlength">Your phone is required to be at least 8 characters</p>
            <p class="help-block" ng-show="contactaForm.phone.$error.maxlength"
               translate="contacta.form.validation.phonemaxlength">Your phone cannot be longer than 12 characters</p>
         </div>
         <p id="error-msg" class="hide" translate="contacta.form.validation.required" style="color: #a94442; padding-top: 5px">Invalid number</span>
      </div>
   </div>
</div>

This is the hidden input field, with the REQUIRED attribute, if I remove it, phone value disappears from the data sent to the server.

<input type="hidden" class="form-control form-control-200" name="phone" id="field_phone" ng-model="vm.form.phone" required/>

And I copy the data on blur on the controller:

    $("#field_phone_pasar").bind('blur', function(){

        $("#field_phone").val($("#field_phone_pasar").intlTelInput("getNumber"));

    });

The problem: this is not working. The view does not accept the value passed as a valid value, user must touch the form manually to pass the requirement.

I have tried removing the required attribute, but phone disappears from the data sent to the server. Also, with the displayed form solely, field gets validated, but field data is sent as null/empty.

Maybe I should try an Angular approach? I'm looking into this question, but so far I'm getting no luck.

---UPDATED QUESTION AND PROGRESS---

Still struggling with this. I have tried to clone the field, get the value but still getting phone:"" in the POST. Also tried:

$('#phone_pasar').on('change', function(){      
     $("#phone").val($('#phone_pasar').intlTelInput("getNumber"));      
});

But data sent seems to ignore the value.

---UPDATE #2---

I seem to be getting somewhere. As @tim-vermaelen pointed out, I checked if there were other active validators, and tried the $("#field_phone").val($(this).intlTelInput("getNumber")).tri‌​gger('change') and validate-non-visible-controls methods, with no positive effect.

What I have tried is the Angular approach. I have printed the JSON of the vm.form.phone, and got this:

{
  "$viewValue": "",
  "$validators": {},
  "$asyncValidators": {},
  "$parsers": [],
  "$formatters": [
    null
  ],
  "$viewChangeListeners": [],
  "$untouched": true,
  "$touched": false,
  "$pristine": true,
  "$dirty": false,
  "$valid": true,
  "$invalid": false,
  "$error": {},
  "$name": "phone",
  "$options": {
    "updateOn": "blur",
    "updateOnDefault": false
  }
}

Seems that my problem is that I'm mixing jQuery and Angular approaches, and in the way I have messed the model view. Any thoughts on how could I fix this?

Thanks for the effort :)

xabi_sides
  • 335
  • 3
  • 20

2 Answers2

0

Have you tried with the change event in this case.

$(formSelector).on('change', '#field_phone_pasar', function(){
    $("#field_phone").val($(this).intlTelInput("getNumber"));
});

Also, bind is from earlier versions. Not a bad idea to include your jQuery version.

Second note: Don't use angular to solve one problem. It's usually a "design" choice but it appears to me you are free ^^

Tim Vermaelen
  • 6,869
  • 1
  • 25
  • 39
  • Hello Tim, thanks for the help. I tried `('#field_phone_pasar').on('change', function(){ $("#field_phone").val($('#field_phone_pasar').intlTelInput("getNumber"));});`, but same problem persists. User should access the field somehow to pass validation, as @bluehipy pointed out in the comment above :( – xabi_sides May 17 '17 at 10:11
  • What kind of validation do you have? If it's the unobtrusive validation plugin (jquery and without) hooked with .Net markup attributes `[required]` for example, you'll have to setup the validation plugin to validate hidden fields. Or use the `valid` method for a single field. – Tim Vermaelen May 17 '17 at 11:12
  • I have not used the validation plugin yet, trying other approaches. Maybe I should check it out, as I'm running out of options. – xabi_sides May 18 '17 at 09:47
  • I only saw the `ng-attributes` in your HTML recently, so since it's already available, [angular approach](http://stackoverflow.com/questions/17452247/validate-form-field-only-on-submit-or-user-input) might be better suited in this case. Other than that, `$("#field_phone").val($(this).intlTelInput("getNumber")).trigger('change')` but that's winging it, as I have no idea if another js process (`angular-auto-validate`, `unobtrusive`, something else...?) is blocking form submission for hidden fields, and that's usually what happens in my experience. – Tim Vermaelen May 18 '17 at 16:18
  • `validate-non-visible-controls` on the `form` tag [perhaps](https://github.com/jonsamwell/angular-auto-validate/issues/41)? – Tim Vermaelen May 18 '17 at 16:29
0

The hidden input should have the same value for the id and name attributes.

You set up the id attribute with the value field_phone and name attribute with the value phone, which is probably the cause of the confusion. This means that the element you're testing in the browser (id="field_phone"), will not be the element you receive on the server (name="phone").

More information here. Try instead setting up your hidden input like so...

<input type="hidden" class="form-control form-control-200" name="field_phone" id="field_phone" ng-model="vm.form.phone" />

Also, try using the form's onsubmit event to set up the data to be sent to the server.

$( "form" ).submit(function( event ) {
    $("#field_phone").val($(this).intlTelInput("getNumber"));
});

Doing it this way, is better because your code will no longer be dependent on an onblur or an onchange event on the #field_phone_pasar field.

bastos.sergio
  • 6,684
  • 4
  • 26
  • 36
  • Hello Sergio, thanks for the help. I already tried that: `$("form").submit(function() {$("#field_phone").val($("#field_phone_pasar").intlTelInput("getNumber"));});` and even `$('#field_phone').validate({ignore: [":hidden", "#field_phone"]});`, but the form requires the "required" tag to send the actual data to the server. If I remove it, "#field_phone" is not sent to the server validation, it just dissappears. And If I leave the tag, I can't use a hidden input form because the user needs to touch it to make it work. – xabi_sides May 17 '17 at 10:58
  • Thanks again. Still struggling with this issue, tried your approach, got no luck. I checked the names and id's of the fields, updated them accordingly, and still getting the value only if the field is required, has a model and is actually interacted by the user. – xabi_sides May 18 '17 at 09:45