我有MyEntity.php模型.作为模型脚本的一部分,有一些规则和一些场景定义:
public function rules()
{
return [
[['myentity_id', 'myentity_title', 'myentity_content', 'myentity_date'], 'required'],
[['myentity_id'], 'integer'],
[['myentity_title', 'myentity_content'], 'string', 'max' => 120],
[['myentity_date'], 'safe'],
];
}
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios['scenario_one'] = ['myentity_id', 'myentity_title'];
$scenarios['scenario_two'] = ['myentity_id', 'myentity_content'];
return $scenarios;
}
Run Code Online (Sandbox Code Playgroud)
我需要能够有不同的场景,并且对于不同的操作,只有某些验证(通过参数)才能激活.例如,scenario_one for actionOne,scenario_two for actionTwo等.
以下是控制器中的一小部分代码:
public function actionOne($id)
{
$modelMyEntity = $this->findModel($id);
$modelMyEntity->scenario = 'scenario_one';
.
.
.
}
public function actionTwo($id)
{
$modelMyEntity = $this->findModel($id);
$modelMyEntity->scenario = 'scenario_two';
.
.
.
}
Run Code Online (Sandbox Code Playgroud)
现在我想要一个scenario_three,根本不应该有任何验证.我将在代码中进行额外的检查,以防止在数据库中存储时出现故障.我只需要确保没有应用任何验证,因为它阻止我的表单提交.如果我不应用任何方案,则应用默认方案(所有列出的验证都将处于活动状态,这与我需要的方案完全相反).
我正在开发 Laravel 应用程序。我在我的应用程序中所做的是我试图覆盖自定义验证规则消息。
我在请求类中有这样的验证规则:
[
'name'=> [ 'required' ],
'age' => [ 'required', new OverAge() ],
];
Run Code Online (Sandbox Code Playgroud)
通常,我们会像这样覆盖规则的错误消息:
return [
'title.required' => 'A title is required',
'body.required' => 'A message is required',
];
Run Code Online (Sandbox Code Playgroud)
但是如何对自定义验证规则类执行此操作?
假设我有一个由property_a和组成的对象property_b,提交时必须至少接收这两个属性之一。
如果对象只是一个,我可以使用required_without如下验证规则
return [
'property_a' => ['required_without:property_b'],
'property_b' => ['required_without:property_a'],
];
Run Code Online (Sandbox Code Playgroud)
如果我的对象本身在另一个对象内部,那么使用点分符号会很容易:
return [
'parent_object.property_a' => ['required_without:parent_object.property_b'],
'parent_object.property_b' => ['required_without:parent_object.property_a'],
];
Run Code Online (Sandbox Code Playgroud)
但是我如何将这个验证规则与对象数组一起使用?
return [
'array.*.property_a' => ['required_without:???.property_b'],
'array.*.property_b' => ['required_without:???.property_a'],
];
Run Code Online (Sandbox Code Playgroud)
required_without的文档没有明确说明我的用例。
有解决方法吗?
我想使用 Java Bean 验证来验证一个整数。它是多重验证的验证。
我目前正在使用 Spring Boot 和验证,并且系统正在使用@RestController我正在接收呼叫后的位置。
public Person addPerson(@RequestBody @Validated Person Person) {/*the code*/}
Run Code Online (Sandbox Code Playgroud)
我希望验证年龄,这些值是有效的:
age == null or age == 0 or (age >= 15 and age <= 80)
public class Person {
private Integer age;
}
Run Code Online (Sandbox Code Playgroud)
我希望能够使用 java 的当前验证约束。我需要实现我自己的约束注释吗?
这会很好,但这不起作用:
public class Person {
@Null
@Range(min=0, max=0)
@Range(min=15, max = 80)
private Integer age;
}
Run Code Online (Sandbox Code Playgroud) 我试图在WPF中使用验证.我创建了一个NotNullOrEmptyValidationRule,如下所示:
public class NotNullOrEmptyValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
if (String.IsNullOrEmpty(value as String))
return new ValidationResult(false, "Value cannot be null or empty");
return new ValidationResult(true, null);
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我需要在我的应用程序中使用它.在我的App.xaml文件中,我为TextBox声明了Style.这是宣言.
<Style x:Key="textBoxStyle" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="Green"/>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Background" Value="Red"/>
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)
现在,我想在我的TextBox上使用它,所以我使用以下代码:
<TextBox Style="{StaticResource textBoxStyle}">
<TextBox.Text>
<Binding>
<Binding.ValidationRules>
<NotNullOrEmptyValidationRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
Run Code Online (Sandbox Code Playgroud)
Tag NotNullOrEmptyValidationRule上出现错误.XAML语法检查程序无法解析NotNullOrEmptyValidationRule.我甚至尝试过放置命名空间但它似乎不起作用.
我正在使用Microsoft VS2010构建Web测试.
我在MSDN上使用了解释:如何:为Web性能测试创建自定义验证规则.
在示例中,当我将此验证规则添加到我的测试时,使用string和int作为私有成员使用公共"获取"和"设置"这些参数在UI中有效进行编辑.
我希望有一个Enum with 3选项,当我将验证规则添加到UI时,我可以选择.
有没有办法添加一个在UI中也有效的Enum变量?
是否有任何其他可以使用的类型在UI中有效?