2

我有两个值的单选按钮,即个人和公司。我正在寻找一种情况,如果选择了具有值 Firm 的单选按钮,则CompanyNametextinput 应充当强制(必需)字段。And, when radio button having value Individual is selected, Then CompanyNametextinput should act as Optional field.

我不知道该怎么做。我尝试在 CompanyName 文本输入中添加 addAttribute 作为强制性。但它没有起作用,因为RegisterForm.php(模型)指定的规则很少。

所以,任何想法如何做到这一点。我不明白。有什么帮助吗?

register.php(查看)

<?php $form = ActiveForm::begin(['id' => 'register-form']); ?>
    .
    .
    .
    <?= $form->field($model, 'AdminType')
            ->radioList(array('Individual'=>'An Individual', 'Firm'=>'Firm/Company/Group of Employees'))
            ->label('Are You')?>
    <?= $form->field($model, 'CompanyName')->textInput()->label('Company Name') ?>
    <div class="form-group">
        <?= Html::submitButton('Register', ['class' => 'btn btn-success', 'name' => 'register-button' ]) ?>
    </div>
<?php ActiveForm::end(); ?>

<script>
    $('input[type="radio"]').click(function()
    {
        if($(this).attr("value")=="Firm")
        {
            $('#registerform-companyname').addAttribute("required");
        }
    });
</script>

RegisterForm.php(模型)

<?php

namespace app\models;

use Yii;
use yii\base\Model;
use kartik\password\StrengthValidator;

class RegisterForm extends Model
{
    public $fname;
    public $lname;
    public $email;
    public $password;
    public $confirmPassword;
    public $AdminType;
    public $CompanyName;
    public $verifyCode;

    public function rules()
    {
        return [
                [['fname','lname', 'email', 'password','confirmPassword','verifyCode','AdminType'], 'required'],
                ['email', 'email'],
            ['confirmPassword', 'compare', 'compareAttribute' => 'password'], 
                ['verifyCode', 'captcha'],
        ];
    }
4

2 回答 2

1

首先,进行任何类型的前端验证都是不好的,因为我可以通过以编程方式生成帖子来规避它,并且它将毫无问题地保存记录,从而使记录不一致的可能性成为现实。

您要做的是在模型中创建自定义验证规则函数,并将规则添加到验证数组中:

<?php
namespace app\models;

use Yii;
use yii\base\Model;
use kartik\password\StrengthValidator;

class RegisterForm extends Model
{
    public $fname;
    public $lname;
    public $email;
    public $password;
    public $confirmPassword;
    public $AdminType;
    public $CompanyName;
    public $verifyCode;

    public function rules()
    {
        return [
                [['fname','lname', 'email', 'password','confirmPassword','verifyCode','AdminType'], 'required'],
                ['email', 'email'],
                ['confirmPassword', 'compare', 'compareAttribute' => 'password'], 
                ['verifyCode', 'captcha'],

                //add rule that uses the validator function
                ['AdminType','radioValidator'],
        ];
    }

    //implement the validator
    public function radioValidator($attribute, $params)
    {
        if($this->$attribute === 'Firm' && empty($this->$attribute))
            $this->addError('CompanyName', 'CompanyName cannot be blank');
    }
}
?>

现在您的字段生成代码应该如下所示

 <?= $form->field($model, 'CompanyName')->textInput()->label('Company Name')->error() ?>

希望这可以帮助你

编辑:由于我习惯于使用 AR 类(通常,当使用 gii 生成时,会自动进行验证),我并没有想到您只使用了一个表单模型(在基本应用程序)

忘记->error()在现场,还要确保你有行

if ($model->load(Yii::$app->request->post()) && $model->validate()) {...}

在你的行动中

于 2015-10-05T22:14:52.233 回答
1

我终于明白了,

 ['company_name', 'required', 'when' => function($model){
    return ($model->user_type == 'Firm' ? true : false);
  }, 'whenClient' => "function (attribute, value) {
      return $('input[type=\"radio\"][name=\"Users[user_type]\"]:checked').val() == 'Firm';
  }"],
于 2015-10-15T09:23:18.430 回答