Dev*_*v R 7 ruby-on-rails ruby-on-rails-3.1
我正在尝试验证电话号码是否为数字: -
这是我的user.rg
number_regex = /\d[0-9]\)*\z/
validates_format_of :phone, :with => number_regex, :message => "Only positive number without spaces are allowed"
Run Code Online (Sandbox Code Playgroud)
这是我的view.html.haml
%li
%strong=f.label :phone, "Phone Number"
=f.text_field :phone, :placeholder => "Your phone number"
Run Code Online (Sandbox Code Playgroud)
这是控制器
def edit_profile
@user = current_user
request.method.inspect
if request.method == "POST"
if @user.update_attributes(params[:user])
sign_in(@user, :bypass => true)
flash[:success] = "You have updated your profile successfully"
redirect_to dashboard_index_path
else
flash[:error] = "Profile could not be updated"
render :action => "edit_profile"
end
end
end
Run Code Online (Sandbox Code Playgroud)
当我第一次在文本字段中输入数字时,它会正确验证,但如果我输入正确的格式然后尝试输入错误的格式,它会跳过验证,我会收到一条flash消息,表明配置文件已成功更新,但是错误的值(带字母)不会保存.
这可能是什么问题?
bec*_*076 12
我用它,:with =>"没问题".
validates :phone,:presence => true,
:numericality => true,
:length => { :minimum => 10, :maximum => 15 }
Run Code Online (Sandbox Code Playgroud)
如果你想要一条消息(不是MASSAGE),试试这个,
validates :phone, :presence => {:message => 'hello world, bad operation!'},
:numericality => true,
:length => { :minimum => 10, :maximum => 15 }
Run Code Online (Sandbox Code Playgroud)
还要检查这个问题.
| 归档时间: |
|
| 查看次数: |
17285 次 |
| 最近记录: |