gpt4 book ai didi

php - 如何仅在请求页面中设置 yii 必填字段而不是创建/更新?

转载 作者:行者123 更新时间:2023-12-01 18:45:21 24 4
gpt4 key购买 nike

有谁知道我应该做什么,以便用户只需要在请求页面中填写输入,而不是在创建/更新页面中填写输入?我有自己的理由想要这样做。

在用户模型中

array('user_email, user_name', 'required','on'=>'request'),  //I want this only occur when on my request page

在用户 Controller 中

public function actionRequest()
{
$id=Yii::app()->user->uid;
$model=$this->loadModel($id);

if(isset($_POST['User']))
{
$model->attributes=$_POST['User'];
if ($model->validate('request'))
{
if($model->save())
{
$this->redirect(array('view','id'=>$uid));
}
}
}

$this->render('request',array(
'model'=>$model,
));
}

但不知怎的,这不起作用。

最佳答案

您必须使用场景创建模型,以便应用适用于该场景的规则。

假设您的场景some_scenario,那么当您声明规则时,请像这样使用:

array('user_email, user_name', 'required', 'on'=>'some_scenario')
// now the user_email, user_name will be required properties only when the scenario is 'some_scenario'

现在要将场景应用于模型,我们必须使用该场景创建该模型实例,如下所示:

$model = new SomeModel('some_scenario');

Here's a yii wiki您可能想阅读。

编辑:在您的情况下,您可以在创建模型对象后通过直接设置 scenario property 来设置场景。 :

$model = $this->loadModel($id);
$model->scenario = 'request';

// ...

关于php - 如何仅在请求页面中设置 yii 必填字段而不是创建/更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14639041/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com