Cakephp 2.x表单验证不适用于复选框

时间:2016-12-06 12:46:57

标签: php forms validation cakephp

更新:

请求$ this-> request->数据的输出 interest_id数组是复选框:

array(
'Openmind' => array(
    'gender' => '',
    'firstname' => '',
    'middlename' => '',
    'lastname' => '',
    'city' => '',
    'emailaddress' => '',
    'emailaddress_check' => '',
    'education_id' => '',
    'current_education_custom' => '',
    'current_class' => '',
    'keep_informed' => '0',
),
'OpenmindInterest' => array(
    'interest_id' => array(
        (int) 0 => '1',
        (int) 1 => '2',
        (int) 2 => '3',
        (int) 3 => '4',
        (int) 4 => '5',
        (int) 5 => '6',
        (int) 6 => '7'
    )
)

我正在使用cakephp 2.X

处理表单

表单现在才有效,我需要在复选框上添加额外的必需验证

Openmind控制器:

        if ($this->request->is('put') || $this->request->is('post'))
        {

            $this->Session->write('register', 1);

            $this->Openmind->create();

            if ($Openmind = $this->Openmind->save($this->request->data))
            {

                if (is_array($this->request->data['OpenmindInterest']['interest_id']))
                {
                    for ($i=0; $i<count($this->request->data['OpenmindInterest']['interest_id']); $i++)
                    {

                        $this->OpenmindInterest->create();
                        $this->OpenmindInterest->save(array(
                            'openmind_id' => $Openmind['Openmind']['openmind_id'],
                            'interest_id' => $this->request->data['OpenmindInterest']['interest_id'][$i]
                        ));

                    }
                }

                $this->Openmind->save($Openmind);

                $this->sendEmail($Openmind['Openmind']['openmind_id']);

                $this->redirect( array(
                    'controller' => 'openminds',
                    'action' => 'registered',

                ) );

            }

        }

我将验证规则添加到我的OpenmindInterest模型中,但是当我发布表单时未设置验证。

这是我的OpenmindInterest模型:

    class OpenmindInterest extends AppModel {

    public $useTable = 'openmind_interests';
    public $primaryKey = 'openmind_interest_id';

    public $validate = array(

        'interest_id' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Vul je voornaam in'
            )
        )
    );


    public $belongsTo = array(
        'Openmind' => array(
            'className' => 'Openmind',
            'foreignKey' => 'openmind_id'
        ),
        'Interest' => array(
            'className' => 'Interest',
            'foreignKey' => 'interest_id'
        )
    );

}

为什么数组中的保存功能没有开始验证?

0 个答案:

没有答案