yii-validation

yii: how to make a unique rule for two attributes

你说的曾经没有我的故事 提交于 2019-12-18 11:40:54
问题 I have a table like this: (id, name, version, text). (name, version) is unique key, how can i make a rule to validate this. 回答1: This can be done by Yii itself, you do not need an extension for it. However an extension can help cleaning up the rules() method as described here: http://www.yiiframework.com/extension/unique-attributes-validator/ This is the code (copied from that site) which will work without using the extension: public function rules() { return array( array('firstKey', 'unique'

Using custom validators with ActiveForm in Yii2

爱⌒轻易说出口 提交于 2019-12-11 17:04:43
问题 I want to make custom validation function like built-in validation required . I have example code here: Model: use yii\base\Model; class TestForm extends Model { public $age; public function rules(){ return [ ['age', 'my_validation'] ]; } public function my_validation(){ //some code here } } View: <?php use yii\helpers\Html; use yii\widgets\ActiveForm; $this->title = 'test'; ?> <div style="margin-top: 30px;"> <?php $form = ActiveForm::begin(); ?> <?= $form->field($model, 'age')->label("age")

Yii2: scenarios() model method

半城伤御伤魂 提交于 2019-12-10 20:15:32
问题 There are 2 needed functions: set password when registering and change password, if user forgot it. When user signs up, password length must be at least 4 chars; when changes pass - at least 5 chars. View is common for registration and changing pass. Obviously, also 2 actions exist, in which either scenario 'signup', either 'change' used. Code snippet in model: public function rules() { return [ ['password', 'string', 'min' => 4, 'on' => 'signup'], ['password', 'string', 'min' => 5, 'on' =>

scenario for validation rules in yii

元气小坏坏 提交于 2019-11-30 21:20:42
I was wandering is there any chance to use scenario for rules, in my model I have public function rules() { return array( array('delivery, firstNameBilling, lastNameBilling, addressBilling, cityBilling, countryBilling, postBilling, telephoneBilling, mailBilling, firstNameDelivery, lastNameDelivery, addressDelivery, cityDelivery, countryDelivery, postDelivery, telephoneDelivery, mailDelivery', 'required'), array('active', 'numerical', 'integerOnly'=>true), ); } and in my view I have something like this <tr> <td> <p><?php echo $form->label($model,'telephoneBilling'); ?><span>: </span><span class

scenario for validation rules in yii

╄→尐↘猪︶ㄣ 提交于 2019-11-30 05:30:08
问题 I was wandering is there any chance to use scenario for rules, in my model I have public function rules() { return array( array('delivery, firstNameBilling, lastNameBilling, addressBilling, cityBilling, countryBilling, postBilling, telephoneBilling, mailBilling, firstNameDelivery, lastNameDelivery, addressDelivery, cityDelivery, countryDelivery, postDelivery, telephoneDelivery, mailDelivery', 'required'), array('active', 'numerical', 'integerOnly'=>true), ); } and in my view I have something

yii: how to make a unique rule for two attributes

萝らか妹 提交于 2019-11-30 04:15:21
I have a table like this: (id, name, version, text). (name, version) is unique key, how can i make a rule to validate this. This can be done by Yii itself, you do not need an extension for it. However an extension can help cleaning up the rules() method as described here: http://www.yiiframework.com/extension/unique-attributes-validator/ This is the code (copied from that site) which will work without using the extension: public function rules() { return array( array('firstKey', 'unique', 'criteria'=>array( 'condition'=>'`secondKey`=:secondKey', 'params'=>array( ':secondKey'=>$this->secondKey