In my database I have 4 columns namely name, description, image and status. In the default templates created by Oil scaffold, all values are expected.

But in my case, the column 'image" will be the name of file uploaded and "status" smallint column will be default by 1. But the forge fails as its required.

In the model validation, the validate() does as below.

public static function validate($factory) {
    $val = Validation::forge($factory);
    $val->add_field('name', 'Name', 'required|max_length[255]');
    $val->add_field('description', 'Description', 'required');
    $val->add_field('image', 'Image', 'required|max_length[255]');
    $val->add_field('status', 'Status', 'required');

    return $val;
}

As I understand, the File Upload item will not be part of the form.

The $val->run() is called before actually creating the Model object for saving. And it fails as the value of "image" and "status" is populated after that.

   if (Input::method() == 'POST') {
        $val = Model_Category::validate('create');

        if ($val->run()) {
            Upload::process($config);

            if (Upload::is_valid()) {

                Upload::save(0);
                $arr = Upload::get_files();

                $category = Model_Category::forge(array(
                            'name' => Input::post('name'),
                            'description' => Input::post('description'),
                            'image' => $arr[0]['saved_as'],
                            'status' => 1,
                ));

            }
        } else {
            Session::set_flash('error', $val->error());
        }
    }

Is there any better fuelPHP way of handling this? How to controller should be changed? or How the Model should be changed?

有帮助吗?

解决方案

The result of form input fields of type "file" will be in $_FILES, not in $_POST.

Since validation only works with posted data (or any array you pass to it), it can not validate those input fields.

You have to remove the 'image' field from the Validation definition, and have the Upload class validate the uploaded file.

This is a better flow:

if (Input::method() == 'POST') {

    Upload::process($config);

    if (Upload::is_valid()) {
         Upload::save(0);

        if (Upload::is_valid()) {
            $val = Model_Category::validate('create');
            if ($val->run()) {
                $arr = Upload::get_files();

                $category = Model_Category::forge(array(
                            'name' => Input::post('name'),
                            'description' => Input::post('description'),
                            'image' => $arr[0]['saved_as'],
                            'status' => 1,
                ));

            } else {
                Session::set_flash('error', $val->error());
            }

        } else {
            // deal with the Upload errors, check Upload::get_errors()!
        }

    } else {
        // deal with the Upload errors, check Upload::get_errors()!
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top