在cakephp2中编辑时,表单验证失败

时间:2012-11-19 09:40:07

标签: forms validation cakephp edit

'employees_firstname' => array(
    'rule' => 'alphaNumeric',
    'required' => true,
    'allowEmpty' => false,
    'on' => '',
    'message' => 'This field is required'
)

这是我的验证。添加时工作正常。但是当我编辑时,即使字段有值,也会显示验证错误。问题是什么?

function edit_employee($employees_id)
    {
       debug($this->request->data);
        $business_accounts = $this->BusinessAccount->get_business_accounts();
        $business_domains = $this->BusinessDomain->get_business_domains();
        $designations = $this->Designation->get_designations();
        $work_locations = $this->EmployeesWorkLocation->get_worklocations();
        //$employees = $this->Employee->get_user_details();
        $employees = $this->Employee->get_employee_details($employees_id);
        $employees_details = $this->EmployeesDetail->get_employee_personal_details($employees_id);
        $countries = $this->Country->get_countries();
        $additional_data = array(
                        'business_accounts' => $business_accounts,
                        'business_domains' => $business_domains,
                        'designations' => $designations,
                        'work_locations' => $work_locations,
                        'supervisors' => $employees,
                        'countries' => $countries,
                        'employees' => $employees,
                        'employees_details' => $employees_details
                     );
        $this->set($additional_data);
        $this->set('title_for_layout', 'Calpinemate - Admin');
        $this->layout = 'admin_console';
       // echo $employees_id;
        $this->Employee->id = $employees_id;
        //$this->Employee->save($this->params['data']['Employee']);
         //print_r($this->data);
        if(!empty($this->data)){ 
            if(isset($this->params['data']['add'])){
                if($this->Employee->validates(array('fieldList' => array('employees_firstname','employees_lastname','employees_emailaddress','employees_password','employees_code','employees_mobile')))){
                    echo "IN";
                $fileOK = $this->uploadFiles('img/Employees', $this->data['File'], $this->data['Employee']['employees_code']);
                if (array_key_exists('urls', $fileOK)) {
                    $this->request->data['Employee']['employees_image'] = $fileOK['urls'][0];
                }

                $this->Employee->save($this->params['data']['Employee']);

                $this->request->data['EmployeesDetail']['employees_id'] = $this->Employee->id;
                $this->EmployeesDetail->save($this->params['data']['EmployeesDetail']);
                $this->Session->setFlash("Employee  Details Updated Successfully!");
                $this->redirect('employee');
                }
            }
            elseif(isset($this->params['data']['cancel'])){
                $this->redirect('employee');
            }
        } 
    }

这是控制器中的功能

echo $this->Form->create('Employee', array('url' => array('controller' => 'MasterConsole', 'action' => 'edit_employee',$employees[0]['Employee']['employees_id']),'type' => 'file'));

    <div class="listing1">
         <div class="list_grid">First name</div>
         <div class="list_grid7"><?php echo $this->Form->input('employees_firstname',array('type' => 'text','label' => false,'default' => isset($employees) ? $employees[0]['Employee']['employees_firstname'] : '')); ?></div>  


</div>

这是视图文件中的代码。

1 个答案:

答案 0 :(得分:0)

除非您只指定创建或更新,否则无需添加'on' => ''

尝试完全删除该部分。此外,请确保在编辑时正确将字段的数据传递给模型。在控制器的编辑方法中,在顶部添加调试以打印出数据并检查它是否在正确的字段中实际出现:

debug($this->request->data);

否则,请发布控制器的编辑方法和视图代码。