PHPActiveRecord validates

validates_presence_of

#检测是不是为空 为空的话可以抛出异常

*Model类:
static $validates_presence_of = array(
    array(‘title‘, ‘message‘ => ‘不能为空‘)
);

实例化后:
$user = new User(array(‘name‘=>‘‘));
$user->save();

var_dump($user->errors->on(‘name‘)); #=> 不能为空

  

validates_size_of

#对字符串长度的限制
*Model类:
static $validates_size_of = array(
    array(‘name‘,
             ‘within‘ => array(1,5),
             ‘too_short‘ => ‘too short!‘,
             ‘too_long‘ => ‘should be short and sweet‘)
);

  

validates_exclusion_of

#对词语的屏蔽
*Model类:
static $validates_exclusion_of = array(
     array(‘name‘, ‘in‘ => array(‘god‘, ‘sex‘, ‘password‘, ‘love‘, ‘secret‘),
‘message‘ => ‘should not be one of the four most used name‘)
);

  

validates_format_of

#正则匹配
*Model类:
static $validates_format_of = array(
    array(‘email‘, ‘with‘ =>
‘/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/‘)
    array(‘password‘, ‘with‘ =>
‘/^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/‘, ‘message‘ => ‘is too weak‘)
);

  

validates_numericality_of

#对数字大小的限制
*Model类:
static $validates_numericality_of = array(
    array(‘price‘, ‘greater_than‘ => 0.01),
    array(‘state‘, ‘only_integer‘ => true),
    array(‘shipping‘, ‘greater_than_or_equal_to‘ => 0),
    array(‘discount‘, ‘less_than_or_equal_to‘ => 5, ‘greater_than_or_equal_to‘ => 0)
);

  

validates_uniqueness_of
  

#唯一性的限制
*Model类:
static $validates_uniqueness_of = array(
array(‘name‘, ‘message‘ => ‘blah and bleh!‘)
);
*实例化:
User::create(array(‘name‘ => ‘Tito‘));
$user = User::create(array(‘name‘ => ‘Tito‘));
$user->is_valid(); # => false

 before_validation_on_create

#验证之前执行方法
// setup a callback to automatically apply a tax
	static $before_validation_on_create = array(‘apply_tax‘);

	public function apply_tax()
	{
		if ($this->person->state == ‘VA‘)
			$tax = 0.045;
		elseif ($this->person->state == ‘CA‘)
			$tax = 0.10;
		else
			$tax = 0.02;

		$this->tax = $this->price * $tax;
	}

  

 

时间: 2024-10-17 02:25:44

PHPActiveRecord validates的相关文章

在特定的action里使用validates

http://guides.rubyonrails.org/v3.0.8/active_record_validations_callbacks.html#on The :on option lets you specify when the validation should happen. The default behavior for all the built-in validation helpers is to be run on save (both when you’re cr

validates

In addition to those validations, information is provided with each macro about its specific options. Validation Macro Options Validate acceptance of terms. validates_acceptance_of :terms validates :terms, acceptance: true :message :accept # Specify

[Schema]I have updated my XML Schema for my service but SoapUI still generates/validates according to the old schema.

SoapUI caches XML schemas when they are first loaded. If you need to force a reload of an interfaces schema either restart SoapUI or use the "Update Definition" action for an interface and just specify the same URL as the current one.

rails 常用的验证方法 validates (转)

Agile Web Development with Rails 17.4 validation validate              在save的时候激活validate_on_create      createvalidate_on_update      update 通过这三个方法可以添加validates_XXX_xxx没有的功能错误信息写进err#:name 是验证的属性名def validate    unless name && name =~ /^\w+$/  

App 组件化/模块化之路——Android 框架组件(Android Architecture Components)使用指南

面对越来越复杂的 App 需求,Google 官方发布了Android 框架组件库(Android Architecture Components ).为开发者更好的开发 App 提供了非常好的样本.这个框架里的组件是配合 Android 组件生命周期的,所以它能够很好的规避组件生命周期管理的问题.今天我们就来看看这个库的使用. 通用的框架准则 官方建议在架构 App 的时候遵循以下两个准则: 关注分离 其中早期开发 App 最常见的做法是在 Activity 或者 Fragment 中写了大量

Revit 2017 编程须要用Visual Studio2015 +.NET Framework 4.52

一年一度的Revit产品公布时刻,我们抢先想各位介绍下Revit 2017的变化和新功能 Major changes and renovations to the Revit API API changes .NET 4.6 All Revit API binaries are now built targeting .NET 4.5.2. However, Revit uses the runtime from .NET 4.6. At a minimum, add-ins will need

ruby reduce方法

Ruby 中一些好用的方法(注意reduce方法) 2016-07-27 17:57 370人阅读 评论(0) 收藏 举报 #####inject inject是我使用最频繁的方法了,它的强大之处在于可以方便的对嵌套的数组,哈希等混合数据结构进行合并或求和, 可以有效减少代码量. 例如最常见的数组套哈希: 1 2 3 4 array = [{a:100}, {b:200}, {c:300}] array.inject(0) { |sum, e| sum += e.values.first } #

Interview

下面的题是供大家查漏补缺用的,真正的把这些题搞懂了,才能"以不变应万变". 回答问题的时候能联系做过项目的例子是最好的,有的问题后面我已经补充联系到项目中的对应的案例了. 1.简述 private. protected. public. internal 修饰符的访问权限.  private : 私有成员, 在类的内部才可以访问. protected : 保护成员,该类内部和继承类中可以访问. public : 公共成员,完全公开,没有访问限制. internal: 当前程序集内可以访

REST Security with JWT using Java and Spring Security

Security Security is the enemy of convenience, and vice versa. This statement is true for any system, virtual or real, from the physical house entrance to web banking platforms. Engineers are constantly trying to find the right balance for the given