Skip to content
This repository has been archived by the owner on Mar 12, 2020. It is now read-only.

data-attributes are being read with stale values #475

Closed
irving-caamal opened this issue Jan 5, 2017 · 7 comments
Closed

data-attributes are being read with stale values #475

irving-caamal opened this issue Jan 5, 2017 · 7 comments
Assignees
Labels

Comments

@irving-caamal
Copy link

irving-caamal commented Jan 5, 2017

Hi I try invalid a select with differents values

this is a simple example..

$('#myForm').validator({
  custom: {
    invalidnumber: function ($el) {
      var number = $el.attr('data-invalidnumber')
      if ($el.val() == number) {
        return number + " is not a valid choice"
      }
    }
  }
})

$('#A').on('change', function (e) {
  var $el = $(e.target)
  $('#B').attr('data-invalidnumber', $el.val()).trigger('focusout')
})

and this is my html code

<div>
<form id="myForm" data-toggle="validator">
      <div class="form-group">
        <label>Invalid Number</label>
        <input id="A" type="number" class="form-control" required>
      </div>
      <div class="form-group">
        <label>Value</label>

               <select class='form-control' name='sel_tesoreria'  data-invalidnumber="0"  
                       id='B' required>
   <option value='0' selected='selected'>Seleccione tesoreria....</option>
                    </select>
        <div class="help-block with-errors"></div>
      </div>
      <div class="form-group">
        <button type="submit" class="btn btn-primary">Submit</button>
      </div>
    </form>
</div>

so... I try with custom but... the form gets validate but I want invalid when select is 0

can someone help me?

btw @1000hz u are the man ,love this powerfull plugin...thanks a lot!.

@1000hz
Copy link
Owner

1000hz commented Jan 5, 2017

Thanks for the report. This is a bug. The problem is that the plugin uses $.fn.data(attr) to read values that are set as data attributes, but those stored values don't seem to get updated when the data attribute changes. The fix is to change the plugin to read the values using $.fn.attr('data-' + attr) instead so the values are read directly from the DOM.

@1000hz 1000hz added the bug label Jan 5, 2017
@1000hz 1000hz self-assigned this Jan 5, 2017
@1000hz 1000hz changed the title Invalid select value like 0,1,2. data-attributes are being read with stale values Jan 5, 2017
@1000hz 1000hz closed this as completed in 5d74c22 Jan 6, 2017
@andrews05
Copy link

I think this is not the correct solution. I'm running into the opposite problem: If I change the data using $el.data('foo', 'bar') this will not update the data- attribute, which means the validator will no longer pick up the new value.
Using the .data() function is the correct way to get and set data in jQuery - the original author here just needs to change his code to use .data() instead of .attr().

@irving-caamal
Copy link
Author

Hi @andrews05 man , You are very right, I didn't think about using .data(), works perfectly with first solution but with this is more logical and yes, i think is the correct solution.

what about the commit?,
->
5d74c22

@andrews05
Copy link

Hopefully @1000hz can revert it? (I think it's actually 469c48d)
Until then you can keep using v0.11.8 as I am, since .data() doesn't work in v0.11.9.

@irving-caamal
Copy link
Author

I'm using v0.11.9. because works perfectly with attr()... but , yes... he need revert it, it´s the best way.

I think you should open new Issue @andrews05 , maybe this notifiactions are invalid for him 'cause it's closed, I support your observation.

@1000hz
Copy link
Owner

1000hz commented Mar 21, 2017

I'll probaby revert, but I have to spend some time catching back up with the project first.

@andrews05
Copy link

Sure, thanks in advance :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants