kohana Framework Upload File Verification Rule Writing Example

  • 2021-07-09 07:29:03
  • OfStack

First of all, I am using ko version 3.2. 0.

kohana verification, with the students are less understand, because each function will give an example in the comments. Today encountered the situation is to verify the picture upload, kohana example is like this.

$array->rule('file', 'Upload::type', array(array('jpg', 'png', 'gif')));

This is not a problem in itself, but it is always a little inconvenient in practical application. Why, because when it is transmitted to the later processing, it is necessary to verify not only the uploading of pictures but also some fields of form form.
1. We will write like this

$post = new Validation($_POST);
$file = new Validation($_FILES);

There is no problem in writing this way. When verifying, write ok according to examples. But it is a bit strange to think new twice, and we also know that $_ POST and $_ FILES are both arrays. Can you check it once? That's for sure, of course, we have to turn them into a large array first. Write this as OK.

$post = new Validation(array_merge($_POST,$_FILES));// Students who don't understand , Du Niang Xia array_merge

Here's the point, dear ones. Everyone knows that the field writing of the verification form form is no different from that before merging, but the key is how to write this picture upload (or other upload).
Well, the time relationship directly on the code, we can take to use directly, of course interested students can also try rules.

$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 foreground form form, and ID cannot be found in the background.
Again, I use kohana version 3.2. 0, other versions pay attention to modify the following writing.


Related articles: