Validation always return error |
I have this form:
Code: <form action="<?= base_url('admin/blog/post/' . $post['id']) ?>/update" method="post" class="form-horizontal" enctype="multipart/form-data"> as you can see I have two field "title" and "content", when I submit the form, this method is called: PHP Code: public function update($id) for some reason validationRules always fail, infact I get this error: string(1) "1" array(3) { ["title"]=> string(31) "Field title is required." } but I have passed the title field, infact when I do $_POST I can see the `title` field with the content provided. What I did wrong?
I will try to explain the reason.
You are using spoofing: Code: <input type="hidden" name="_method" value="put" /> In the controller, when the validate() method is called, an instance of the Validation class is created and passed the request to it. And the class automatically determines the data for validation by the type of request. Since the framework has defined the method as PUT, the Validation class tries to get data from the request body via php://input. And this is where the fun begins. When you specify the encryption method 'multipart/form-data' in the enctype attribute in the form tag, the request is encoded in a special way, and get it through the php://input impossible. I have counted 3 ways to solve the problem so far. 1. Don't use encoding 'multipart/form-data' 2. Do not use spoofing. 3. Do not use controller validation method. In this case, you can write the implementation of this method yourself, and pass the data to the Validation::run() method. https://github.com/codeigniter4/CodeIgni...ssues/4552 (04-10-2021, 10:35 PM)iRedds Wrote: I will try to explain the reason. I'm interested in the first solution, 'cause for the other two I have no clue how to setup them. Could you please show me an example of file uploading without using `multipart/form-data`? Thanks again
Without using multipart/form-data, you won't upload the file.
If you remove the <input type="hidden" name="_method" value="put" /> then the framework defines the request method as POST, not PUT. And everything will work for you. Instead of the controller's validation method, you can work with the validator directly. PHP Code: $validator = \Config\Services::validation(); (04-11-2021, 05:57 AM)iRedds Wrote: Without using multipart/form-data, you won't upload the file. I have removed PHP Code: <input type="hidden" name="_method" value="put" /> but when I submit the form I get: 404 - File Not Found The controller or the method was not found: \App\Controllers\Admin::blog the form: PHP Code: <form action="<?= base_url('admin/blog/post/' . $post['id']) ?>/update" method="post" class="form-horizontal" enctype="multipart/form-data"> these are my routes: PHP Code: $routes->group('admin', function ($routes) { the update() method in the controller seems to be working only if I use "put", but maybe am I missing something here?
He's also using the jQuery Summernote editor in his form which may affect the form data.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
If you are using the "resource" route generator, the update method handles put and patch requests.
Since you are using the "resource" generator, removing the spoofing method will result in an error. You can solve it like this: PHP Code: //Add a line to the closure where you call the resource method But I'm worried about that address in the form: <?= base_url('admin/blog/post/' . $post['id']) ?>/update The format of the address conflicts with the expected format when using the "resource" generator. For a PUT request, the address should be like this: admin/blog/post/ID. This means that the "admin/blog/post/ID/update" address should cause an error. @InsiteFX I do not know this tool, but it seems to me that it does not affect the sending of data. Maybe I'm wrong. |
Welcome Guest, Not a member yet? Register Sign In |