# |
Jul 27th 2021, 16:51 |
greg138 |
If you only ever need the code in one place, then having it in that one place is more elegant; why have a whole separate file just to implement a one-line function? If you need it in multiple places, but all in the same table, then a function in that table is probably most elegant. If you need it across tables, then a provider gets to be a useful thing. |
# |
Jul 27th 2021, 16:13 |
paolo.bragagni |
...and now that I know about providers seems more elegant... :D |
# |
Jul 27th 2021, 16:12 |
paolo.bragagni |
Can I put custom logic in a method? |
# |
Jul 27th 2021, 16:11 |
paolo.bragagni |
Id like to reuse the custom logic |
# |
Jul 27th 2021, 15:59 |
tyler.adam.lazenby |
I finally got my ci to run properly |
# |
Jul 27th 2021, 15:36 |
kevin.pfeifer |
as cnizzardini already said above you don’t need to create a custom provider. Just use this approach: ```$validator->add('title', 'someuniquename', [ 'rule' => function ($value, $context) use ($extra) { // Custom logic that returns true/false }, 'message' => 'The title is not valid' ]);``` Custom Providers are just a tool to separate custom rules into its own file. |
# |
Jul 27th 2021, 15:33 |
paolo.bragagni |
Il try tomorrow. (Someting easier??) |
# |
Jul 27th 2021, 15:00 |
cnizzardini |
Name your class `MyProvider` is the standard |
# |
Jul 27th 2021, 14:59 |
greg138 |
Should be `'provider' => 'custom'`, no? |
# |
Jul 27th 2021, 14:53 |
paolo.bragagni |
no errors but no validator |
# |
Jul 27th 2021, 14:52 |
paolo.bragagni |
class myProvider { public function customMethod() { return false; } } |
# |
Jul 27th 2021, 14:52 |
paolo.bragagni |
in myProvider |
# |
Jul 27th 2021, 14:52 |
paolo.bragagni |
$validator = new Validator(); $validator->setProvider('custom', new \App\Model\myProvider()); $validator->add('descrizione', 'custom', [ 'rule' => 'customMethod', 'provider' => 'table' ]); |
# |
Jul 27th 2021, 14:51 |
paolo.bragagni |
then in my model table in initialize I wrote |
# |
Jul 27th 2021, 14:50 |
paolo.bragagni |
created myProvider.php in App/Model |
# |
Jul 27th 2021, 14:50 |
paolo.bragagni |
no bo |
# |
Jul 27th 2021, 14:48 |
paolo.bragagni |
no wait |
# |
Jul 27th 2021, 14:47 |
paolo.bragagni |
created myProvider.php in App/Model |
# |
Jul 27th 2021, 14:47 |
paolo.bragagni |
I'm not able |
# |
Jul 27th 2021, 14:34 |
admad |
4.3 will even support PHP 8.1 :) |
# |
Jul 27th 2021, 14:29 |
admad |
`void` can't be used with any other type. |
# |
Jul 27th 2021, 14:23 |
cnizzardini |
at least 4.2 should be |
# |
Jul 27th 2021, 14:23 |
cnizzardini |
it's not like cakephp 4 won't be supported for a very longtime, and its compatible with php 8 |
# |
Jul 27th 2021, 14:18 |
paolo.bragagni |
ok trying |
# |
Jul 27th 2021, 14:17 |
cnizzardini |
or the last thing in the posted snippet, which accepts a string at arg two, but the method must be static |
# |
Jul 27th 2021, 14:16 |
cnizzardini |
i'm guessing here, because i've never used them, but I imagine you could do this: `$validator->setProvider('custom', new \App\Any\Damn\Fqn\MyProvider());` |
# |
Jul 27th 2021, 14:15 |
cnizzardini |
where ever you want per the documentation |
# |
Jul 27th 2021, 14:15 |
paolo.bragagni |
where the provider logic goes? |
# |
Jul 27th 2021, 14:14 |
cnizzardini |
if you're not reusing the rule, i see no reason to create a provider and would just use a callback |
# |
Jul 27th 2021, 14:13 |
cnizzardini |
build the provider and inform Validator |
# |
Jul 27th 2021, 14:12 |
brandon |
Oof. I am only just getting around to upgrading the site 4.x. 5.x is coming this year? :cold_sweat: |
# |
Jul 27th 2021, 14:12 |
cnizzardini |
you can cargo cult something into existence |
# |
Jul 27th 2021, 14:12 |
cnizzardini |
```$validator = new Validator(); // Use an object instance. $validator->setProvider('custom', $myObject); // Use a class name. Methods must be static. $validator->setProvider('custom', 'App\Model\Validation');``` |
# |
Jul 27th 2021, 14:12 |
cnizzardini |
specifically: |
# |
Jul 27th 2021, 14:12 |
cnizzardini |
https://book.cakephp.org/4/en/core-libraries/validation.html#adding-validation-providers |
# |
Jul 27th 2021, 14:11 |
paolo.bragagni |
(if datetime I admit only date in italian, if decimal I admit only 2 decimal etc.. |
# |
Jul 27th 2021, 14:09 |
paolo.bragagni |
I'm trying to create a model.twig where I define my own default validation |
# |
Jul 27th 2021, 14:08 |
cnizzardini |
cool, i've seen lots of PR activity on it |
# |
Jul 27th 2021, 14:08 |
cnizzardini |
which i assume you might not need, especially for a one off validation, depends on what you're trying to do though... |
# |
Jul 27th 2021, 14:08 |
dereuromark |
@cnizzardini It is fair to expect somewhere this year, but usually there is no clear deadline on this. In the past it was often close to the CakeFest. |
# |
Jul 27th 2021, 14:08 |
paolo.bragagni |
probably |