简体   繁体   English

在Form中验证FuelPHP中的表列值

[英]Validation of Table Column Values in FuelPHP in Form

In my database I have 4 columns namely name, description, image and status. 在我的数据库中,我有4列,即名称,描述,图像和状态。 In the default templates created by Oil scaffold, all values are expected. 在Oil scaffold创建的默认模板中,应使用所有值。

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. 但是在我的情况下,“ image”列将是上传文件的名称,“ status” smallint列将默认为1。但是伪造会按要求失败。

In the model validation, the validate() does as below. 在模型验证中,validate()的操作如下。

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. 在实际创建要保存的Model对象之前,将调用$ val-> run()。 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? 有没有更好的fuelPHP处理方式? 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. 类型为“文件”的表单输入字段的结果将在$ _FILES中,而不在$ _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. 您必须从“验证”定义中删除“图像”字段,并让Upload类验证上传的文件。

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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM