V
V
Vladislav2018-11-13 19:20:04
Yii
Vladislav, 2018-11-13 19:20:04

How to validate radioList in ActiveForm?

When registering a user, I need him to select one of the two radio buttons, and the data about this is entered into the database as values ​​1 or 2.
View:
Signup.php

<?php

/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \frontend\models\SignupForm */

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;

$this->title = 'Signup';
$this->params['breadcrumbs'][] = $this->title;
?>
</header>
<section class="login__form">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="formHeading">Signup</div>
                <form action="" class="loginForm">
                    <div class="input_container">
                        <?php $form = ActiveForm::begin(['id' => 'form-signup']); ?>

                        <p class="form__input__title">E-mail</p>
                        <?= $form->field($model, 'email')->textInput(['autofocus' => true, 'class' => 'inputs'])->label(false) ?>

                        <p class="form__input__title">Nickname/Company</p>
                        <?= $form->field($model, 'username')->textInput(['autofocus' => true, 'class' => 'inputs'])->label(false) ?>

                        <p class="form__input__title">Password</p>
                        <?= $form->field($model, 'password')->passwordInput()->label(false) ?>

                        <label><?= $form->field($model,'account_type')->radioList( ['1'=>'Searching Job', '2'=>'Company'], array('separator'=>' ')) ?></label>
                        <br>

                        <?= Html::submitButton('Sign Up', ['class' => 'button__to accent button__sumbit', 'name' => 'signup-button']) ?>

                        <?php ActiveForm::end(); ?>
                    </div>
                </form>
            </div>
        </div>
    </div>
</section>

SignupForm:
<?php
namespace frontend\modules\User\models;

use yii\base\Model;
use frontend\models\User;

/**
 * Signup form
 */
class SignupForm extends Model
{
    public $username;
    public $email;
    public $password;
    public $account_type;

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            ['username', 'trim'],
            ['username', 'required'],
            ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
            ['username', 'string', 'min' => 2, 'max' => 255],

            ['account_type', 'required'],

            ['email', 'trim'],
            ['email', 'required'],
            ['email', 'email'],
            ['email', 'string', 'max' => 255],
            ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],

            ['password', 'required'],
            ['password', 'string', 'min' => 6],
        ];
    }

    /**
     * Signs user up.
     *
     * @return User|null the saved model or null if saving fails
     */
    public function signup()
    {
        if (!$this->validate()) {
            return null;
        }
        
        $user = new User();
        $user->username = $this->username;
        $user->email = $this->email;
        $user->setPassword($this->password);
        $user->generateAuthKey();
        
        return $user->save() ? $user : null;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2018-11-14
@webinar

Set two required validators
- to make sure the value is selected and
in - so that only 1 and 2 are allowed, here is the doc:
https://www.yiiframework.com/doc/api/2.0/yii-valid...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question