-
Notifications
You must be signed in to change notification settings - Fork 823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FIX for #4417: Ensuring ->removeValidation() is defined on instances of Validator. Setup new API for enabling/disabling validation. Documentation and better type handling. #4542
FIX for #4417: Ensuring ->removeValidation() is defined on instances of Validator. Setup new API for enabling/disabling validation. Documentation and better type handling. #4542
Conversation
I forgot to mention two things:
|
Could you please add a test case? It could be a simple validator that fails if a field value isn't a set value, but passes if you call removeValidation() on it. :) Code looks good to me! |
forms/Validator.php
Outdated
*/ | ||
public function requireField($fieldName, $data) { | ||
public function requireField($fieldName, array $data) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see value in type hinting here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, the is_array
check happening below makes it somewhat pointless.
How can we ensure that developers utilizing this API are aware of their mistake ahead of time without causing crashes/issues when in production? Seems like an easy mistake to make since this method expects an array of fields despite the name being singular. I like doing this semantically at the language level (i.e. the array
type hint) since this method will not work if this isn't an array, but I don't like crashing code should it make it into production and I also don't like the inelegant excess of calling trigger_error()
in a minor circumstance such as this.
I'd like to ask you not to use short array syntax too... but our coding conventions doc is mute on this point ;) |
e58df1c
to
0001516
Compare
@dhensby Alright, no problem. I've updated the array syntax to be consistent with the rest of the entire system (d'oh on my part, good habit I suppose). I realize it's in master and the PHP version for On the array type hinting, I was going to be bullheaded about this. I did that initially as a proactive measure since if you're calling this, you're doing so from code you wrote and apparently not CMS/framework code (I searched for references, couldn't find any). So if the user passes anything except an Why on earth is this code producing the error $fieldName = "foobar";
$data = "this is a string";
requireField($fieldName, $data);
function requireField($fieldName, array $data) {
if(is_array($data[$fieldName]) && count($data[$fieldName])) {
echo "I will never execute :(";
}
} That seems like a more important issue, since at this point SilverStripe (or something in it somewhere) is causing unexpected functionality at a reasonably low level. If I were to guess, I'd probably say it's somewhere in that Let me know your opinion on that please. In the meantime I'll try to stamp out that unit test @tractorcow |
@patricknelson Hmm, that should’ve already been solved in master: #2928 |
0001516
to
9400ec9
Compare
Ok, got my test case up @tractorcow @kinglozzer Really? How long ago? Also, now that I'm involved, I suppose everything spirals out of control, hah. I wanted to give an opinionated rant that apparently (now that I'm testing the |
9400ec9
to
e841ea7
Compare
@patricknelson March 2014 is when the fix was merged. I just checked out master and it does error as expected for me:
|
I got you @kinglozzer I see why it didn't happen. I have my own test script (call it So it bootstraps differently than the |
Great, love the tests - I'm not a fan of type hinting, because really any iterable object should work here (the |
That's right, I suppose my goal would be to have it fail earlier rather than later when that array style access is attempted but doesn't work. Also note that Would be a framework level thing, but maybe you could setup a standard method that could be called to detect if something allows that sort of array access (albeit an array or But instead, it'd be like this so you can still use array style syntax function ss_is_array($var) {
return (is_array($var) || $var instanceof ArrayIterator);
} Or something. |
e841ea7
to
60a26f0
Compare
@tractorcow @dhensby @kinglozzer This has sat around a little while, so I've rebased on p.s. Sorry @tractorcow I posted this comment on the wrong issue/PR a few seconds ago, in case you're confused (I'm following up on some of these since I've got some free time this week and next). |
Haha, I replied to your other comment already. It's all good. :) |
@sminnee shall I just merge this in by hand to deal with the conflicts? |
Validator has changed a lot in the last 2 years. I suggest to raise it as a new PR. |
Hmm - there have been too many changes for this to be merged by hand without having it reviewed again. I've rebased and worked it into the new file but I can't push. @patricknelson can you enable maintainers to push to this PR please? 👼 |
Ok @dhensby I've added you, @tractorcow @sminnee and @kinglozzer as collaborators to my fork of |
@patricknelson you don't have to add us to the whole repo, there's a check box on the right hand side of pull request pages saying "allow contributors [write access]" But access to the whole fork works too ;) |
Ah there it is, got it @dhensby . I'll revoke ;) Let's do it the right way, sorry @tractorcow @kinglozzer @sminnee |
cde65f7
to
874cdf1
Compare
src/Forms/Validator.php
Outdated
@@ -30,6 +30,11 @@ public function __construct() | |||
protected $result; | |||
|
|||
/** | |||
* @var bool | |||
*/ | |||
protected $enabled = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably need a getter if we have a setter.
Linting issues:
|
Was there a poll/vote anywhere about these coding conventions per chance? Couldn't find easily after a quick googling (in case you have on-hand). |
@patricknelson we adopted the PSRs; there were previous discussions about whether to step into line with the PSRs but beyond "yes, let's do that" we didn't get into the details of specific rules. I don't think it was a poll, the consensus was pretty clear that PSRs are the way to go. |
Well, the discussion goes back years and years... it took us quite a while to actually get from the initial conception to actually moving over to psr2. https://groups.google.com/forum/#!topic/silverstripe-dev/_ebPef7HgKQ In fact there are still several parts of psr2 we don't yet adhere to (listed exemptions in framework/phpcs.xml.dist), but we are looking to remove those as we improve our adherence to the standard. :) |
Oh, how ever will I learn to Sarcasm aside, I get the need for the formatting standards since you'll have about as many variations in coding style as you have coders, so ¯\_(ツ)_/¯ I just think halting on the @tractorcow I can't find |
I think I answered both questions, sorry. https://github.com/silverstripe/silverstripe-framework/blob/master/composer.json#L87 @tractorcow did you mean |
See #6908. Once this is merged you can just do You can use an IDE plugin to automatically show linting issues as you go. |
@patricknelson most of us run with linters in our editors to pick up on these things as you're editing. What editor do you used? Also: "composer run-script lint" runs the checks locally if that helps |
@tractorcow can we correct the location of the ruleset so that IDEs can be configured more easily? |
Patrick, my view on syntax rules is that they're either hard rules or no-rules-at-all — soft rules merely rot. As a community I think getting the IDE linters into our contributors workflows is probably the best outcome and so happy to help with that/. |
My PR moves it to the standard "default" location #6908, so it should (in more cases) not need specifying if runinng the linter on the repository root. |
08bda6d
to
7ac75f0
Compare
…n instances of Validator. Setup new API for enabling/disabling validation. Documentation and better type handling.
7ac75f0
to
5fa3c85
Compare
We are greed and ready to go :) |
I agree 😄 Not my project and I've had less and less time to contribute to PR's, so despite the lint rules making it more difficult to swallow (so to speak), I can only ... deal with it 🕶️ |
Did you manage to get your IDE set up to respect the linter rules? Did merging 6908 make it easier? |
@tractorcow are you happy now? |
Replying to original PR here: #4418
@dhensby @tractorcow
Firstly: I was in the process of setting this up in master, however I realized that maybe it'd make more sense to go ahead and rethink this and take a different approach (since we're going against master and we can be a little more flexible with the API). That is -- since there must not be very many implementations of this which are already broken by the lack of
->removeValidation()
method, maybe it will make more sense to update the API take the "enable" vs "disable" approach. I think if you need the ability remove validation, you should also then have the ability to enable it again. So, I'd suggest having a new method like->setEnabled($enabled)
which->validate()
will check first before actually doing anything useful.Secondly: Since needed to have
->removeValidation()
available by default (due to it's calling/usage elsewhere), it makes sense for it to do something. In light of the above API (->setEnabled()
) we can have it just be an alias for->setEnabled(false)
and, in order to make it functional, it can maintain state for the->validate()
method, which (if I'm correct) is the goal. That is, to be able to allow regular execution of the->validate()
method without triggering errors (to effectively disable/remove validation, but temporarily if desired).Thirdly: This PR contains that along with a cleanup of
->errors
to be consistent with->getErrors()
documentation (that is, it's expected to always be an array). An empty array loosely validates to false just as well as null does, plus we're being more explicit here and, in my opinion, it's simpler to just return the same type all of the time.