在Login窗体中,我需要glyphicon-remove在每个验证消息的末尾都有相应字段名称的图标.所以我在下面的代码中使用了Login model.
['email', 'required', 'message' => 'Email cannot be blank<span class="glyphicon glyphicon-remove"></span>'],
['password', 'required', 'message' => 'Password cannot be blank<span class="glyphicon glyphicon-remove"></span>']
Run Code Online (Sandbox Code Playgroud)
而不是上面的代码,有没有任何可能的方法来使用类似下面的代码.
[['email', 'password'], 'required', 'message' => $attribute.' cannot be blank<span class="glyphicon glyphicon-remove"></span>']
Run Code Online (Sandbox Code Playgroud)
上述代码的思想是为每个字段动态获取相应的字段名称.
请只做那些需要的.谢谢.
更新
这里我使用的HTML代码(<span class="glyphicon glyphicon-remove"></span>)通过使用正确输出encode=>'false'.但我需要的是不是为每个字段单独定义,而是需要为所有字段共同定义.
这似乎是一个微不足道的问题,但是我能想到的所有明显的解决方案都有其自身的缺陷.
我们想要的是能够仅为新记录设置任何默认的ActiveRecord属性值,使其在验证之前和验证期间可读,并且不会干扰用于搜索的派生类.
一旦我们实例化类,就需要设置和准备(new MyModel)->attr默认attr值,以便返回默认值.
以下是一些可能性和问题:
A)在MyModel超控的init()方法和分配默认值时isNewRecord,像这样是正确的:
public function init() {
if ($this->isNewRecord) {
$this->attr = 'defaultValue';
}
parent::init();
}
Run Code Online (Sandbox Code Playgroud)
问题:搜索.除非我们明确地取消设置我们的默认属性MySearchModel(非常容易出错,因为它太容易忘记),这也会在调用search()派生MySearchModel类之前设置值并干扰搜索(该attr属性已经设置,因此搜索将返回结果不正确).在Yii1.1中,这是通过在调用unsetAttributes()之前调用来解决的search(),但是在Yii2中不存在这样的方法.
B)在MyModel超控的beforeSave(),像这样的方法:
public function beforeSave($insert) {
if ($insert) {
$this->attr = 'defaultValue';
}
return parent::beforeSave();
}
Run Code Online (Sandbox Code Playgroud)
问题:未在未保存的记录中设置属性.(new MyModel)->attr是null.更糟糕的是,即使是依赖于此值的其他验证规则也无法访问它,因为在验证之后beforeSave()会调用它.
C)为了确保在验证期间值可用,我们可以改写beforeValidate()方法并在其中设置默认值,如下所示: …
由于更新操作上的唯一验证器获取错误('此用户名已被占用')我想在新用户名值不等于prev值时验证更新操作上的用户名,这是我的规则:
[['username'], 'unique', 'on'=>'update', 'when' => function($model){
return static::getOldUsername($model->id) !== $model->username;
}
],
Run Code Online (Sandbox Code Playgroud)
并且我的功能是获得上一个用户名值:
public static function getOldUsername($id)
{
return static::findIdentity($id)->username;
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用,我认为$ model-> getId()没有返回任何因为静态id(例如:23)它的工作.
[['username'], 'unique', 'on'=>'update', 'when' => function($model){
return static::getOldUsername(23) !== $model->username;
}
],
Run Code Online (Sandbox Code Playgroud)
我怎样才能获得模特ID?或者,如果您有其他方法可以在更新操作上跳过yii2唯一验证,如果新值等于prev值,请解释一下.
提前致谢
我试图在登录时显示错误消息中的链接,但它不起作用。
验证中的错误消息LoginForm:
$this->addError($attribute, 'Your account has been disabled. <a href=""> Enable It</a>');
Run Code Online (Sandbox Code Playgroud)
在login.php(视图)中:
<?= $form->errorSummary($model); ?>
Run Code Online (Sandbox Code Playgroud)
我试过如下,但不工作:
<?= $form->errorSummary($model,['errorOptions' => ['encode' => false,'class' => 'help-block']]); ?>
Run Code Online (Sandbox Code Playgroud)
我得到以下输出而不是渲染a标签:
yii2 yii2-advanced-app yii2-basic-app yii2-model yii2-validation
我有一个表for_date在数据库中按类型整数保存列.为了for_date使用DateTime格式显示,我使用ActiveForm代码:
<?= $form->field($model, 'for_date')->textInput([
'class' => 'form-control datepicker',
'value' => $model->for_date ? date(Yii::$app->params['dateFormat'], $model->for_date) : date(Yii::$app->params['dateFormat'], time()),
'placeholder' => 'Time'])
->label('Attendance Date') ?>
Run Code Online (Sandbox Code Playgroud)
但是,当我创造和保存时,Yii通知 This field must be integer
在模型文件中,我有两个函数在验证之前进行转换,但它仍然是错误的.
public function beforeValidate(){
if(!is_int($this->for_date)){
$this->for_date = strtotime($this->for_date);
$this->for_date = date('d/M/Y', $this->for_date);
}
return parent::beforeValidate();
}
public function afterFind(){
$this->for_date = \Yii::t('app', Yii::$app->formatter->asDate($this->for_date));
$this->for_date = date('d/M/Y', $this->for_date);
return parent::afterFind();
}
Run Code Online (Sandbox Code Playgroud)
如何使用整数保存到数据库中?
在我的基本应用项目中,我正在尝试集成注册表单,但出现此错误:
无效的验证规则:规则必须同时指定属性名称和验证器类型。
我的代码在这里。
注册表格.php
<?php
namespace app\models;
use yii\base\Model;
use app\models\User;
/**
* Signup form
*/
class SignupForm extends Model
{
public $user_fname;
public $user_email;
public $user_password_hash;
/**
* @inheritdoc
*/
public function rules()
{
return [
[['user_fname','user_email', 'user_password_hash'], 'required'],
// rememberMe must be a boolean value
['user_password_hash','match','pattern'=>'$\S*(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])\S*$','message'=>'Password must have atleast 1 uppercase and 1 number '],
[['user_password_hash'],'string','min'=>6],
//email validation
['user_email','email']
[['user_email'], 'string', 'max' => 255],
[['user_fname'], 'string', 'max' => 45],
];
}
/**
* Signs user up.
*
* …Run Code Online (Sandbox Code Playgroud) 我需要打破Yii2验证规则中使用的长消息.
我试过这样的:
public function rules()
{
return [
['username', 'required', 'message' => 'long message first line here'."<br>".PHP_EOL.'long message last line here'],
];
}
Run Code Online (Sandbox Code Playgroud)
但是<br>出现在消息中并且该行不会破坏我需要的地方.
为了清楚起见,我得到的是:
long message first line here<br>long message last line here
Run Code Online (Sandbox Code Playgroud)
并不是:
long message first line here
long message last line here
Run Code Online (Sandbox Code Playgroud)
有谁可以帮忙吗?我真的很感激!先感谢您.
我正在为2个表单使用一个模型文件.一个用于SIGNUP和其他用于一个dding成员.
我没有为SIGNUP表单设置任何场景.但是,设置了添加成员表单的方案.
模型
public function rules() {
return [
//Add Members
['first_name', 'required','message'=>'Please enter first name.','on'=>'addteammembersidebar'],
['email', 'required','message'=>'Please enter email address.','on'=>'addteammembersidebar'],
['mobile','required','message'=>'Please enter mobile number.','on'=>'addteammembersidebar'],
//Common
['first_name', 'required','message'=>'Please enter your first name.'],
['email', 'required','message'=>'Please enter your email address.'],
['mobile','required','message'=>'Please enter your mobile number.'],
];
}
Run Code Online (Sandbox Code Playgroud)
视图
在这里,我设置了场景$modelTeamMembers->scenario = 'addteammembersidebar';.
<?php foreach ($modelsTeamMembers as $indexMember => $modelTeamMembers):
$modelTeamMembers->scenario = 'addteammembersidebar';
?>
<tr class="house-item">
<td class="vcenter">
<?php
// necessary for update action. …Run Code Online (Sandbox Code Playgroud) 我对Yii2的验证有疑问.所以,我的模型验证规则是这样的:
return [
['status', 'required', 'on' => 'update'],
[['status'], function ($attribute) {
$this->$attribute = \yii\helpers\HtmlPurifier::process($this->$attribute);
}],
];
Run Code Online (Sandbox Code Playgroud)
问题是如果内容是<script>alert('something')</script>,由于净化器将是空白,内容将通过所需的验证.
那么如何重新验证require的内容呢?或者这样做的好方法是什么?
我正在为Yii2搜索密码强度计.我为Yii1 找到了这篇文章.我在protected那里看到了目录.我找不到这个文件夹了.它是否可用于基本应用程序模板或高级应用程序模板?
yii2 ×10
yii2-validation ×10
php ×5
yii2-model ×3
activerecord ×1
datetime ×1
line-breaks ×1
message ×1
yii2-user ×1