访问关联模型中的验证规则 (CakePHP)

发布于 2024-10-10 04:18:43 字数 1243 浏览 2 评论 0原文

您好,我一直在尝试从主控制器访问关联模型的验证规则。它似乎不起作用,我也找不到任何关于此的参考。这是我想要做的:

用户控制器片段:

$this->User->TalentProfile->set( $this->data );
if ($this->User->TalentProfile->validates()) {
    //it always validates and doesn't seem to see model's validation rules
}

TalentProfile 模型片段:

var $validate = array (
        'first_name' => array(
            'maxLength' => array(
                    'rule' => array('maxLength', 20),  
                    'message' => 'Can not be longer than 20 characters.',
                    'last' => true
            ),
            'first_name_not_empty' => array(
                    'rule' => 'notEmpty',
                    'message' => 'This field is required',
                    'last' => true
            ),
        ),
);

我还尝试重新包装数组,我的想法是模型名称可能会与验证混淆:

$this->User->TalentProfile->set( array('TalentProfile',$this->data) ) );

我也尝试过这个希望,但结果仍然相同:

$this->loadModel('TalentProfile');
$this->TalentProfile->set( $this->data) );

有是我所缺少的东西。请帮忙!谢谢

编辑:

我试图保存表单数据,这在 SQL 中给了我空白。我认为这可能是一个比我想象的更简单的问题。

Hi I have been trying to access the validation rules of an associated model from my main controller. It doesn't seem to be working and I haven't been able to find any reference about this. Here is what I am trying to do:

User Controller snip:

$this->User->TalentProfile->set( $this->data );
if ($this->User->TalentProfile->validates()) {
    //it always validates and doesn't seem to see model's validation rules
}

TalentProfile Model snip:

var $validate = array (
        'first_name' => array(
            'maxLength' => array(
                    'rule' => array('maxLength', 20),  
                    'message' => 'Can not be longer than 20 characters.',
                    'last' => true
            ),
            'first_name_not_empty' => array(
                    'rule' => 'notEmpty',
                    'message' => 'This field is required',
                    'last' => true
            ),
        ),
);

I have also tried rewrapping the array my thought being that the model name could be screwing with the validation:

$this->User->TalentProfile->set( array('TalentProfile',$this->data) ) );

I have also tried this hoping but still same results:

$this->loadModel('TalentProfile');
$this->TalentProfile->set( $this->data) );

There is something I am missing. Please help! Thanks

EDIT:

I tried to save the form data which is giving me blanks in the SQL. I think this might be a simpler problem than I thought.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

带刺的爱情 2024-10-17 04:18:43

通过在 TalentProfile 模型中设置 $validate 数组,您需要以下形式的数据:

Array(
    [TalentProfile] => Array(
        [first_name] =>
    )
)

关于其工作原理的一些解释:

在调用 $this->User-> TalentProfile->validates()$this->TalentProfile->validates() 您正在使用 TalentProfile 模型中的验证方法。这意味着 CakePHP 将根据该模型中的验证规则来验证您的输入数据,因此它期望在数组中设置 TalentProfile 键(但如果没有设置,则会安静地消失)。

在您的 $validates 数组中,您已为名为 first_name 的键设置了验证规则。 CakePHP 将此键用作从表单输入的字段名称。

现在,您将输入作为 fname 而不是 first_name。您的输入也与 User 模型关联,而不是与 TalentProfile 关联。

如果您进一步解释一下您要对表格执行的操作,我可以提供更多帮助。

By setting the $validate array in the TalentProfile model, you require data to be of the form:

Array(
    [TalentProfile] => Array(
        [first_name] =>
    )
)

A little explanation on how this works:

In the call $this->User->TalentProfile->validates() and $this->TalentProfile->validates() you are using the validation methods in the TalentProfile model. This means that CakePHP is going to validate your input data against the validation rules in that model, so it expects the TalentProfile key to be set in the array (but dies down quietly if it's not).

In your $validates array, you have set up validation rules for a key called first_name. CakePHP takes this key as a field name that is being input from a form.

Right now, you are taking inputs as fname instead of first_name. Your inputs are also associated with the User model, and not TalentProfile.

If you explain further what you're trying to do with your form, I can be of more help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文