How to add new rules in Kohana framework - php

I'm using Kohana framework for my project development, I need to introduce new rules Suppose I have original_price and discount_price fields, for this discount_price must be always always less than or equal to original_price.
For this scenario, how to write new rule(method).

In your Kohana framework,you will find validation.php file,in that write a appropriate validation function.Suppose you want to test some length of a text field then write relevant function here and call that function in your controller class using add_rules().I hope you got the answer.

Use v3.2 and check this page out: http://docs.kohanaphp.com/libraries/validation

Related

What is a macro in laravel Macroable

please can anyone help me understand what a macro is in Laravel Macroable trait, reading this documentation https://laravel.com/api/5.4/Illuminate/Support/Traits/Macroable.html only tells me how to use but why do I use it, what is it meant for.
It is for adding functionality to a class dynamically at run time.
use Illuminate\Support\Collection;
Collection::macro('someMethod', function ($arg1 = 1, $arg2 = 1) {
return $this->count() + $arg1 + $arg2;
});
$coll = new Collection([1, 2, 3]);
echo $coll->someMethod(1, 2);
// 6 = 3 + (1 + 2)
echo $coll->someMethod();
// 5 = 3 + (1 + 1)
We have 'macroed' some functionality to the Collection class under the name someMethod. We can now call this method on the Collection class and use its functionality.
We just added a method to the class that didn't exist before without having to touch any source files.
For more detail of what is going on, please check out my article on Macros in Laravel:
asklagbox - blog - Laravel Macros
It allows you to add new functions. One call to ::macro adds one new function. This can be done on those of the internal framework classes which are Macroable.
This action of adding the function to the class is done at run time. Note there was/is an already existing perfectly good name for this action, which isn't the word "macro", which I'll explain at the end of this post.
Q. Why would you do this?
A. If you find yourself juggling with these internal classes, like
request & response, adding a function to them might make your code more
readable.
But as always there is a complexity cost in any
abstraction, so only do it if you feel pain.
This article contains a list of the classes you can add functions to using the static call "::macro"
Try not to swallow the word macro though, if you read that article - if you're like me it will give you big indigestion.
So, let's now add one extra function to an internal framework class. Here is the example I have just implemented:
RedirectResponse::macro('withoutQuery', function() {
return redirect()->to(explode('?', url()->previous())[0]);
});
This enables me in a controller to do this:
redirect()->back()->withoutQuery();
(You can just do back() but I added redirect() to make it clear).
This example is to redirect back and where the previous route was something like:
http://myapp.com/home?something=something-else
this function removes the part after '?', to redirect to simply:
http://myapp.com/home
I did not have to code it this way. Indeed another other way to achieve this is for me to put the following function in the base class which all controllers inherit from (App\Http\Controllers\Controller).
public function redirectBackWithoutQuery()
{
return redirect()->to(explode('?',url()->previous())[0]);
}
That means I can in any controller do this:
return $this->redirectBackWithoutQuery();
So in this case the "macro" lets you pretend that your new function is part of an internal framework class, in this case the Illuminate/RedirectResponse class.
Personally I like you found it hard to grasp "laravel macros". I thought that because of the name they were something mysterious.
The first point is you may not need them often.
The second point is the choice of the name ::macro to mean "add a function to a class"
What is a real macro?
A true macro is a concept unique to Lisp. A macro is like a function but it builds and returns actual code which is then executed. It is possible to write a function in other languages which returns a string which you then execute as if it was code, and that would be pretty much the same thing. However if you think about it you have all of the syntax to deal with when you do that. Lisp code is actually structured in lists. A comparison might be imagine if javascript was all written as actual json. Then you could write javascript, which was json, which returned json, which the macro would then just execute. But lisp is a lot simpler than json in terms of its syntax so it is a lot easier than what you just imagined. So, a true lisp macro is one of the most beautiful and amazing things you can encounter.
So why are these add-a-function things in laravel called macros?
That's unknown to me I'm afraid, you'd have to ask the author, but I asked myself what they really do and is there already a name for that.
Monkey Patches
TL;DR laravel's ::macro could more accurately be described as monkey patch
So if using laravel ::macro calls, I personally decided to create a MonkeyPatchServiceProvider and put them all there, to reduce unnecessary confusion for myself.
I realise the name might sound a bit derogatory, but that's not intended at all.
It's simply because there's already a name for this, and we have so much terminology to deal with why not use an existing name.

What is use of "expect_file()" function in Codeception?

Hi i am new to codeception unit testing and i am using it with Yii2. I know the user of functions expect_not() and expect_that() and also know little about expect() function and uses it to check key in error array.
However I don't know the use of expect_file(). I searched little in internet but found not any good help. can anyone please give me little description about the use of this function.
expect_file() is used to verify with assertions specific to file system objects. It has two parameters (one is optional).
If you call this function with a single parameter, it will be used as the Subject Under Test file name. if it is called with two parameters, will be used as a description to display if the assertion fails but if you if you call it with 0 or more than two arguments it will throw a bad method call exceptions.
You can use it like this
expect_file('filename.txt')->exists();
expect_file('filename.txt')->notExists();
BTW expect_file() is an alternate function for verify_file().

How to disable translation of Model validation messages only in cakePHP?

CakePHP will automatically assume that all model validation error messages in your $validate array are intended to be localized. But, I don't want to translate model validation messages. How to achieve this, any suggestion?
The simplest most easiest way would be just not translating those strings. So, if in your .po file
Mistake here -> Error aqui //don't do that
Mistake here -> Mistake here
And your validation errors are "translated" to the same language.
If you just don't want to filter yourself which strings are from validation and which are "normal" strings, change the validation domain of the models (do it in the AppModel so you'll only have to do it once).
class User extends AppModel {
public $validationDomain = 'validation_errors';
}
and now your validation messages will be in the new validation domain and not in default.pot, so you could just not translate the whole "validation_errors.pot" file and you'd be fine.
This part is only really valid for cake 2.5, I can't be sure if it applies to other versions
Now, if you want the really "difficult" way and just erase that functionality from the face of the earth, you'll have to overwrite some functionalities in Cake lib.
I'm not recommending changing the code directly in the the lib Folder, just extending the classes and replacing the in app/lib, otherwise upgrading versions will be a pain.
The class and functions you'll have to modify should be CakeValidationSet in lib/Cake/Model/Validator and the function is _processValidationResponse
All parts that have something like this
__d($this->_validationDomain, $result, $args);
should be replace with a vsprintf($result, $args) or similar (depending on the name of the parameters. This __d function is called 4 times inside that function, so replace all of them.
Personally, I'd just change the validation domain, wouldn't translate the file, and be done with it. Searching the code that translated this messages was really not worth the effort (except, you know, just to know how it's done).

where can I find a list of all the validators and filters of a Zend_Form?

I'm learning Zend Framework using their manual at framework.zend.com.
where can I find the full list of all the validators and filters
that can be used in a Zend_Form ?
Using Zend Framework 1.11.6.
example:
filters: 'StringTrim'
validators: 'EmailAddress'
thank you!
Check the following paths of your Zend Framework installation folder:
/Zend/Validate/*
/Zend/Filter/*
Or check the corresponding manual pages:
http://framework.zend.com/manual/en/zend.filter.html
http://framework.zend.com/manual/en/zend.validate.html
You can use all the shipped filters & validators with zend-form aswell as custom ones.
The usage, for anyone that got a little confused as I did, is to take the last part of any Zend_Validate_XYZ and use the XYZ as the string in any addValidator chain like so: ->addValidator('XYZ') so for example, to validate an email address use the Zend_Validate_EmailAddress class or ->addValidator('EmailAddress') on the elements variable
Don't forget that the second parameter is whether a failure should break the chain. True breaks the chain, False does not. So, if we wanted to validate an Alpha only field and then an AlphaNumeric field but NOT break if the Alpha field !isValid() then:
->addValidator('Alpha', FALSE)->addValidator('AlNum', TRUE)

How can I change Zend Framework's routing schema to not use key/value pairs?

Rather than using controller/action/key1/value1/key2/value2 as my URL, I'd like to use controller/action/value1/value2. I think I could do this by defining a custom route in my Bootstrap class, but I want my entire application to behave this way, so adding a custom route for each action is out of the question.
Is this possible? If so, how would I then access valueN? I'd like to be able to define the parameters in my action method's signature. e.x.:
// PostsController.php
public function view($postID) {
echo 'post ID: ' . $postID;
}
I'm using Zend Framework 1.9.3
Thanks!
While I don't think it's possible with the current router to allow N values (a fixed number would work) you could write a custom router that would do it for you.
I would question this approach, however, and suggest that actually listing all of your routes won't take long and will be easier in the long run. A route designed as you've suggested would mean that either your named parameters are always in the same order, i.e.
/controller/action/id/title/colour
or that they are almost anonymous
/controller/action/value1/value2/value3
With code like
$this->getRequest()->getParam('value2'); //fairly meaningless
Does it have to be N or can you say some finite value? For instance can you imagine that you'll never need more than say 5 params? If so you can set up a route:
/:controller/:action/:param0/:param1/:param2/:param3/:param4
Which will work even if you don't specify all 5 params for every action. If you ever need 6 somewhere else you can just add another /:paramN onto the route.
Another solution I've worked with before is to write a plugin which parses the REQUEST_URI and puts all the extra params in the request object in the dispatchLoopStartup() method. I like the first method better as it makes it more obvious where the params are coming from.

Categories