Showing posts with label Form. Show all posts
Showing posts with label Form. Show all posts

Tuesday, December 16, 2008

rails error ActionController::InvalidAuthenticityToken

升级到Rails 2.2.2后,在用户登录时抛出以上错误,因为Rails新版本对安全控制做了一些加强措施,只要在form中添加<%= token_tag %>即可,Rails会添加一个token(在action中的form_authenticity_token方法生成这个token)在form中,随表单一起提交,可以适当的防止一些web攻击。
<input name="authenticity_token" type="hidden" value="d688e6bf60f43bd171504e059de1ba03f876d129" />
具体可参考 ActionController::RequestForgeryProtection 和 config/environment.rb 中的配置说明:
# Your secret key for verifying cookie session data integrity.
# If you change this key, all old sessions will become invalid!
# Make sure the secret is at least 30 characters and all random,
# no regular words or you'll be exposed to dictionary attacks.
config.action_controller.session = {
:session_key => '_sv3_session',
:secret => 'b35a7a9ffb88288f11c03b0c24fe45f90879d8fbb83f9ba70649b489d8165f06d4484dd07c6e41c2d9616630781fd739a127d1285c589db3d77801afd30a9d35'
}

Tuesday, November 04, 2008

form.onsubmit and form.submit()

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>form.onsubmit and form.submit()</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</script>
</head>
<body>

<div id="content">
<form method="post">
javascript form.submit() 方法不会触发 form.onsubmit 事件。
<input id="message" name="message" size="30" type="text" />
</form>
</div>
<script type="text/javascript">
var form = document.forms[0];
form.addEventListener("submit", function(){alert('x');}, false);
setTimeout(function() {
alert('x');
// <input id="submit" type="submit" value="submit"/>
// TypeError: form.submit is not a function
// 如果form有一个id为submit的对象时,form.submit会指向此对象,从而覆盖了submit()方法,导致以上的类型错误。
form.submit();
}, 10000);
</script>

</body>
</html>

Tuesday, July 22, 2008

firefox3 中tab键无法focus到select控件

解决方法: 在system preference-keyword&mouse-keyword shortcuts中选中all controls.
也可直接用快捷键ctrl+F7进行切换。

Sunday, July 20, 2008

firefox3 page info extension

查看frames/forms/links的firefox3插件,参考链接:
http://mozilla.queze.net/
https://addons.mozilla.org/en-US/firefox/addon/7459
https://addons.mozilla.org/en-US/firefox/addon/5156

个人觉得”Links and Forms“这个插件还是比较有用的,之前在firefox2里内置的links and forms用习惯了,另一个view dependecies因为有firebug,就不必要装了。

Saturday, July 19, 2008

ActionLink / EventLink / Form submission differences

ActionLink, EventLink, and Form submission all cause the ACTION-REQUEST-RENDER sequence of events. However, in other ways they differ:


* ActionLink and EventLink can submit "context" parameters; Form can submit all of its input and hidden fields.
* ActionLink and EventLink send an HTTP GET; Form sends an HTTP POST (POST allows much more data and doesn't display the data in the URL).
* ActionLink generates one event: ACTION; EventLink generates one event that it nominates. Form generates many events including VALIDATE_FORM, SUCCESS, and FAILURE.

Reference:
http://files.doublenegative.com.au/jumpstart/home.html

Friday, October 19, 2007

Iframe src 属性说明及使用

在Google的一些应用网页里面Iframe用得相当频繁,配合ajax做用户交互,有些是将form提交到Iframe(form的target属性指向iframe的name,form的action提交后服务器返回的内容会在form的target所在的iframe中写入,这个使用方法与frameset页面中某帧的链接<a target="xxx" href="xxx">中指定target显示框架用法相同),再从ifrmae中取回数据显示给用户。

如test.html


<form action="form_action.html" method="post" target="target_iframe" accept-charset="utf-8">
<p><input type="hidden" name="var1" value="1" id="var1"></p>
<p><input type="hidden" name="var2" value="2" id="var2"></p>
<p><input type="submit" value="submit"></p>
</form>

<iframe src="http://www.baidu.com" id="iframe_id" name="target_iframe"></iframe>

和form_ation.html

<html>
<body>
<div id="div1">form action returned html body</div>
</body>
</html>

Sunday, August 05, 2007

prototype javascript library Form observe example


<form action="event_observe_submit" method="post" id="f1" name="form_name" accept-charset="utf-8">
<p><input type="text" id="t1" name="t1" value="Continue">text</p>
<p><input type="text" id="t2" name="t2" value="Continue">text</p>
<p><input type="checkbox" id="c1" name="cb[]" checked>checkbox</p>
<p><input type="checkbox" id="c2" name="cb[]">checkbox</p>
<p><input type="submit" id="s1" name="submit" value="Continue"></p>
</form>
<script type="text/javascript" charset="utf-8">
//new Field.EventObserver('t1', function (args) { alert(args + args.value); });
//new Field.EventObserver('c1', function (args) { alert(args + args.value); });
new Form.EventObserver('f1', function (args) { alert(args + args.value); });
</script>

prototype javascript library Form extend example


<form action="form_submit" id="f1" method="post" accept-charset="utf-8">
<p><input type="text" name="test1" value="test1" id="test1"></p>
<p><input type="text" name="test2" value="test2" id="test2"></p>
<p>
<select id="s1" name="s1">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
</p>
<p><input type="submit" value="submit"></p>
</form>
<script type="text/javascript" charset="utf-8">
function doc(argument) {
document.write("<p>\n");
document.write(argument);
document.write("</p>\n");
}

//Form.disable('f1');
Form.focusFirstElement('f1');
doc($('s1').type);// select-one
</script>

Wednesday, July 25, 2007

usage of form_remote_tag and remote_form_for


<%= form_remote_tag :html => { :action => url_for(:controller => "some", :action => "place", :id => 1), :id => "form_id" }, :update => "ajax_update_div" %></form>

A "fall-through" target for browsers that doesn‘t do JavaScript can be specified with the :action/:method options on :html.
<% form_remote_tag({:html => { :action => url_for(:controller => "other", :action => "place", :id => 1), :id => "form_id", :method => :get }, :url => url_for(:controller => "some", :action => "place", :id => 1), :update => "ajax_update_div"}) do%>
<% end %>

<% remote_form_for :person, @person, :html => {:id => 'form_id', :name => 'form_name', :method => :get, :action => url_for(:controller => 'where', :action => 'some', :id => @person)}, :url => { :action => "test" }, :update => 'ajax_div' do |f| %>
First name: <%= f.text_field :first_name %>
Last name : <%= f.text_field :last_name %>
Biography : <%= f.text_area :biography %>
Admin? : <%= f.check_box :admin %>
<% end %>


form_for(object_name, *args, &proc)
Creates a form and a scope around a specific model object, which is then used as a base for questioning about values for the fields. Examples:

<% form_for :person, @person, :url => { :action => "update" } do |f| %>
First name: <%= f.text_field :first_name %>
Last name : <%= f.text_field :last_name %>
Biography : <%= f.text_area :biography %>
Admin? : <%= f.check_box :admin %>
<% end %>

Worth noting is that the form_for tag is called in a ERb evaluation block, not a ERb output block. So that’s <% %>, not <%= %>. Also worth noting is that the form_for yields a form_builder object, in this example as f, which emulates the API for the stand-alone FormHelper methods, but without the object name. So instead of text_field :person, :name, you get away with f.text_field :name.
That in itself is a modest increase in comfort. The big news is that form_for allows us to more easily escape the instance variable convention, so while the stand-alone approach would require text_field :person, :name, :object => person to work with local variables instead of instance ones, the form_for calls remain the same. You simply declare once with :person, person and all subsequent field calls save :person and :object => person.
Also note that form_for doesn’t create an exclusive scope. It’s still possible to use both the stand-alone FormHelper methods and methods from FormTagHelper. Example:

<% form_for :person, @person, :url => { :action => "update" } do |f| %>
First name: <%= f.text_field :first_name %>
Last name : <%= f.text_field :last_name %>
Biography : <%= text_area :person, :biography %>
Admin? : <%= check_box_tag "person[admin]", @person.company.admin? %>
<% end %>

Note: This also works for the methods in FormOptionHelper and DateHelper that are designed to work with an object as base. Like collection_select and datetime_select.
Html attributes for the form tag can be given as :html => {…}. Example:

<% form_for :person, @person, :html => {:id => 'person_form'} do |f| %>
...
<% end %>

You can also build forms using a customized FormBuilder class. Subclass FormBuilder and override or define some more helpers, then use your custom builder like so:

<% form_for :person, @person, :url => { :action => "update" }, :builder => LabellingFormBuilder do |f| %>
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
<%= text_area :person, :biography %>
<%= check_box_tag "person[admin]", @person.company.admin? %>
<% end %>

In many cases you will want to wrap the above in another helper, such as:

def labelled_form_for(name, object, options, &proc)
form_for(name, object, options.merge(:builder => LabellingFormBuiler), &proc)
end