PHP-Libraries. Require multiple columns to be unique as a set
PHP-Libraries. Require multiple columns to be unique as a set
I could have sworn I had seen an example of this but I can't track it down.
Say my records have the columns A, B, C, D, E, F, G.
When I create a new record I want to ensure that no other records have the same values for, say, A, B, D.
Each A, B, D are not required to be unique individually, but they cannot occur in another record with those same values.
Is this a check that is supported in Editor? Or is there a way to validate this without just doing something like
$editor->sql("
SELECT
A, B, D, COUNT(*)
FROM
table
GROUP BY
A, B, D
HAVING
COUNT(*) > 1
");
Thanks for any help!
This question has accepted answers - jump to:
Answers
Yeah its just there is one build in validator "unique".
https://editor.datatables.net/manual/php/validation
cheers
Hannes
Hi btree,
Thanks for the input but as I mentioned
The
Validator()
function operates on a field. Which means, as far as I can tell, you can only check for each individual field to be unique on it's own, not a group of fields to be unique as a set.Hi,
sorry misread your question.
Then i would do the same as you already mentioned, maybe with a function.
Cheers
Hannes
Okay so this is what I've come up with so far
I have three more questions at this point:
1: How should I throw an error with this method? Should I just manually handle that by outputting
If so is there a way to throw a general error rather than a field specific one?
2: As partially noted in a comment it seems that the new row is created BEFORE we finish our
preCreate
function. The count forfetchAll()
will return 1 even when that SQL should return. Am I misinterpretingpreCreate
?3: So let's say the previous two questions are addressed. What is the 'correct' method of canceling the creation of a new row? I would also like to do something similar when performing the delete/remove action. Rather than delete I would like to set the row's "Hidden" field to 1.
Hi,
What you need here is a custom validation method. Although
preCreate
will execute your code, as you have found there is no way to exit from the code (I suppose you could in theory set all fields with->set(false)
, but a validator is the way to do it!).Regards,
Allan
Hi Allan,
Thanks for the input. I'm still poking around trying to figure out the best way to implement this. I see
\DataTables\Editor\Field::validator
can take a function which is how I'm looking at doing this, but that would still leave me with an ugly hack like:or
That's off the top of my head. Some slightly cleaner ways to do that but still, aiming for something cleaner.
However if the action is
Editor::ACTION_EDIT
I then need to determine the values of all of the other fields. I don't see a way to do that either since, at that point, I only have the field's name. I can't use my editor instance obviously, I don't see a way to get this information from$field
,~~I don't have an id to work with either.~~(why no strike-through?) Not sure what I was doing wrong butrow_X
wasn't being included in the sent data.Is there a more straightforward way of doing this that I'm missing? I dug around and wasn't able to see a way to register validator of my own for use with
->validator("funcName");
.Update: Found out that an issue was causing
row_X
not to be sent, it was undefined/null. Another issue is that when using inline editing ONLY the field being edited is sent. If you use the main editor it sends everything.That looks good to me. Why do you think it is a hack? The only way I can see it could be tidied up would be to create a custom validation class, or augment the existing one, and add your validation method to it so you can all it like the pre-build validators.
This upgrade document details that behaviour, why and how to change if you don't want it.
Allan
Thanks for the upgrade doc, I had missed that. I guess by 'hack' I mean 'not as clean as I would like.' I'm on my phone atm so I can't post my current solution. I more or less made a custom validate class to use.
Thanks again for all of your help Allan.