Closed Dinuz closed 9 years ago
@joecorcoran
Actually I think this issue is strictly related with my previous issue that brought to the creation of the new version 2.0.5 of judge - I am talking about https://github.com/joecorcoran/judge/issues/39 .
I mean, the validation problem that i was talking about in my post above, is not just for custom validator, but also for the uniqueness validator (that is a standard judge validator). Then after I tested that I was having the same issue with the uniqueness (I turned off my custom validator, and turned on the uniqueness in my model):
validates :email, presence: true,
format: {with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i},
#user_existence: true,
uniqueness: {case_sensitive: false}
I started to think that maybe the problem was not directly related with the custom validator, but instead it was related with the asynchronous validation (uniqueness acts exactly as my custom validator, makes a call to the server).
Then I decided to do a test, loading back the 2.0.4 version, and see if this was a problem also before or was just introduced with the new patch release. Unfortunately (I mean unfortunately, because the result clearly tells that something is wrong in the new patch), everything works fine with the 2.0.4 version of judge (obviously in that version there is still the issue of the multiple validations that the patch 2.0.5 solved).
This means dear @joecorcoran that we should look a bit in what is going on, and fix the asynchronous in the 2.0.5 that appear to be broke. Please let me know what I can do or how do you think we are going to fix it? I am more than happy to help you.
Best Dinuz
@joecorcoran
Hope the follow can help you in figuring out what is going on.
I used the following piece of code:
var _result = judge.validate(element, function() {
return;
});
console.log(_result);
In order to see what was going on with the objects, and the following images are screenshots of my console:
After I used another piece of code:
return judge.validate(element, {
valid: this._valid,
invalid: this._invalid
});
and i put a console.log('valid') inside of the _valid function, and a console.log('invalid') inside the _invalid function. Nothing happened, I mean in console there was no valid or invalid message, only the two get call for the ajax validations. Meaning that no one of the two functions was called at all (like I said the ajax seems to completely annul the existence of other validators that are not ajax).
In other words the callbacks valid and invalid are never called.
What is more strange, is that if i change the above code in:
return judge.validate(element, {
valid: console.log('valid'),
invalid: console.log('invalid')
});
I get in my console the two message valid and invalid. Then it reads the two options, but never call the callbacks functions.
What is going on do you think?
Thanks Dinuz
@joecorcoran
The issues #42 and #23 are apparently related to this issue!
I believe that the problem is related with the way in which the pending ajax works. The callbacks invalid or valid are never called when we use also an asynchronous validator! I need to correct myself the issue is present also with the previous version 2.0.4! At this point I believe this is a fat bug in the judge code, and it affects too much the whole functionality and behaviour of judge, to be not seriously considered and fix! I am more than happy to help if you want and you give me some guide!
Let's try and get to the bottom of this. I think I understand but I'm finding it a bit hard to follow. I thought the spec already covered this, but I might well be wrong.
Can you put together a brand new Rails app with Judge 2.0.5 and the most basic example of this behaviour possible? That would be really helpful. Please try to use plain callback functions in JS wherever you can, so we can eliminate function binding as the source of the problem.
Thanks joe, I will do it tomorrow! Let's understand this thing well!
Sent from my iPhone
On Jan 1, 2015, at 11:52 AM, Joe Corcoran notifications@github.com wrote:
Let's try and get to the bottom of this. I think I understand but I'm finding it a bit hard to follow. I thought the spec already covered this, but I might well be wrong.
Can you put together a brand new Rails app with Judge 2.0.5 and the most basic example of this behaviour possible? That would be really helpful. Please try to use plain callback functions in JS wherever you can, so we can eliminate function binding as the source of the problem.
— Reply to this email directly or view it on GitHub.
@joecorcoran did you have the opportunity to look at the app that I put in a public repository on my profile (Called Judge Test) ? If yes, were you able to reproduce the erroneous behaviour that I experimented?
Did you see that the data-object, has multiple repetition of the same error message?
in the spec i found(Should create one validation for each object?):
it('creates one Validation for each object in data attr JSON array', function() {
expect(queue.validations.length).toBe(2);
});
Did you see that when the custom validator is activate, the other no ajax validations are completely ignored? The js that I used is pretty basic, and is just integrated with bootstrap style.
But also if I use the most basic callback (in the same repository) like:
$(function() {
//$('form').JudgeBootstrapValidator();
$('#invite_request_email').on('blur', function() {
judge.validate(document.getElementById('invite_request_email'), {
valid: function(element) {
element.style.border = '1px solid green';
},
invalid: function(element, messages) {
element.style.border = '1px solid red';
alert(messages.join(','));
}
});
});
});
The effect is the same when there are ajax validations, all the other validations are always skipped.
This should already prove that the binding is not the source of the problem, but maybe how the queue works.
Moreover to create more complications, there is also the issue of validations performed client side, also if a field is not present in that specific form, but the validation is activate in the model.
I gave you the example of the sign-in (and I agreed with you was not the best example), but was important anyway to highlight the weird behaviour. In that specific case, the validations were always failing, because judge was running a password_confirmation validation, and it couldn't find any password confirmation, due to the nature of the form in question(was a sign in form). This was happening because the user model has a password confirmation validation, used for example in a sign up form, or in a change password form.
The last but not the least of this serie of issues, is that judge carry over validations between different models, if the models have an attribute with the same name. In the repository this happen with the two models that I built, because in each model there is an attribute called email. In very simple words, if I have two different type of validations in the two models (let's say I have an uniqueness on the email of the model A, and a format on the email of the model B), then Judge will run client side validations for uniqueness and format on the two models (if I have a form for model A, on the email judge will run the validation of A,uniqueness, and the validation of B, format). This behaviour is completely wrong, and doesn't make any sense.
I hope I was able to explain a bit better what are the core issues, and I also hope that the repository helped you in reproducing these behaviours.
Looking forward to hearing from you ( I am kind of stuck in this limbo, and I need to decide if keep using judge or look for some other alternative and solution).
@Dinuz @joecorcoran, I can confirm that this bug is definitely happening. I set up a basic app to test #38 against. and I ended up setting presence and uniqueness on the attribute. This and #42 seem to be the same issue. I'm just about finished up on #38 and then i was going to start digging into this. As @Dinuz mentioned before #23 might also be tied to this. but i haven't given that issue enough attention to say for sure
@dannysperry I saw you merged some new code! Just curious is the code to solve this issue?
@Dinuz Yes it looks like PR #49 fixed this
@joecorcoran
Hi Joe, This is kind of super weird. I follow your suggestions to add a custom validation (it is an asyncronous validator - need to perform a call to the server in order to check the presence of a specific parameter in a model). What is weird is that without the custom validator, all the other standard validator present on the model work as a charm, instead when i include my custom validator, it just run it and not more the standard validator.
Maybe I am doing something very wrong, and I need some clarifications or help about it.
The follow are the step that I did:
(1) Created my custom validator in rails (it works fine I tested it on the server side). (2) Added uses_messages :validator_error_name to use the i18n with judge (3) Created my custom validator client side (I put it in a js file called judge-extensions, and I included it in my app.js after judge) :+1:
(4) I added the expose Model, :parameter in the config for the judge
(5) I added my custom validator to my model
Now When I run my code, I see that the custom validator is called, and it works fine (gives error when it need to give error, and give valid when need to be valid). This means that the code is right, except that it become the only validation that runs. I mean with my custom validation, format and presence are completely ignored (are not performed at all, like are not present in the model). If I comment out in the model the custom validator, then the other two are automatically working again.
What I am doing wrong? Because I am doing something wrong for sure.
Thanks so much
Best Dinuz