【问题标题】:Yii how to validate one part of fields and than the whole form?Yii 如何验证字段的一部分而不是整个表单?
【发布时间】:2014-01-13 13:05:46
【问题描述】:

我的表单中的字段很少,而且都是必需的。其中一个字段是 fileField。所以我想做的是使用事务验证字段的一部分,如果这些字段一切正常,上传图片,保存在磁盘上,设置另一个字段的值并将所有字段保存在数据库中。

但正如我写的那样,问题是所有字段都需要设置。而当我验证一部分参数时,另一部分不会设置,所以验证会返回“false”。

附言 对不起我的英语水平

【问题讨论】:

  • 您可以添加您的代码以及模型规则、控制器和字段以进行验证吗?

标签: php forms validation yii


【解决方案1】:

有一些方法可以做到这一点。

首先,您可以创建一个验证类。 例如:

class testModel extends CModel{
    public $test;
    public function attributeNames() {
        return array(
            'test'=>'Test'
        );
    }
    public function rules() {
        return array(
            array('test','required'),
                array('test','exist','className'=>'testClass','attributeName'=>'testAtttibute')
        );
    }
}

然后

$test=new testModel();
$test->test=$_POST['ANYNAME'];
if($test->validate()){
    //Do something
}

您也可以通过将属性传递给 validate 方法来仅验证一个属性

$test->validate(array('test','other attr');

【讨论】:

    猜你喜欢
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多