Example of writing a file upload verification rule in the kohana framework,
Declare that I use ko3.2.0.
Kohana verification is rarely understood by the students, because every function annotation will give an example. Today, we are faced with the situation of verifying image uploading. The kohana example is like this.
Copy codeThe Code is as follows: $ array-> rule ('file', 'upload: type', array ('jpg ', 'png', 'gif ')));
This is not a problem, but it is inconvenient in actual application. Why, in addition to verifying the upload of images, you must also verify certain fields in the form.
Generally, we write
Copy codeThe Code is as follows:
$ Post = new Validation ($ _ POST );
$ File = new Validation ($ _ FILES );
There is no problem in writing this way, and it is OK to write it as an example during verification. but I think new2 is a little strange, and we also know that $ _ POST and $ _ FILES are arrays. Can we perform one verification? That's for sure. Of course, we have to convert them into a large array first. In this case, OK.
Copy codeThe Code is as follows:
$ Post = new Validation (array_merge ($ _ POST, $ _ FILES); // if you do not understand it, du Niang's array_merge
It's important to go out. You all know that there is no difference between the field Writing Method of the form and the pre-merger. The key is how to write this Image upload (or other upload.
Okay, the time relationship is directly written into the code. You can use it directly. Of course, you can also try rules if you are interested.
Copy codeThe Code is as follows:
$ Post-> rule ('img ', 'not _ empty ')
-> Rule ('img ', 'upload: type', array (': value', array ('jpg ', 'png', 'gif ')))
-> Rule ('img ', 'upload: size', array (': value', '1m '));
PS: img is the control name of input type = "file" in the front-end form. The ID cannot be found in the background.
Declare the kohana3.2.0 version I used again. For other versions, pay attention to the modification method.