PEAK XOOPS - HTMLでValidateするフォームメール (1) in englishin japanese

Archive | RSS |
XOOPS
XOOPS : HTMLでValidateするフォームメール (1)
Poster : GIJOE on 2007-09-04 06:00:47 (15723 reads)

in englishin japanese
いわゆるフォームメールは、デザイナーさんに完全にお任せしたいものの筆頭です。
でも、意外と嫌らしくて、結局プログラマーが手を入れる、なんてことになりがちです。
その最大の理由は、Validationでしょうか。

いろいろなフォームメールシステムを見てきましたが、どんなに簡単である、と謳っているものでも、フィールド定義が独自のXMLとかだったりします。そんな独自のXML仕様書を読まなければならないと考えただけで頭が痛くなる人も少なくないでしょう。

かといって、フォームを自動生成するタイプのフォームメール(XOOPSであればliaiseとか)は、デザイナーさんにとって使い物になりません。なぜなら、彼らはHTMLを自由に書くのが仕事だからです。


…というわけで、アイデアとしてはずっと温めていたHTMLによるValidationを試しに最新のpicoに実装してみました。

試しに pico-1.50 で、こんなコンテンツを作ってみてください(オプションはSmartyのみON)。もちろん、HTMLの部分は自由に記述できますが、checkboxおよびradioは、入力値を保存できません。(まだ作りかけ)

※ 1.51でcheckboxとradioにも対応しました
※※ 1.51で仕様変更があったので、サンプルコンテンツも変更しました。

もう少し細かな注意点は次回以降に書きます。


<{capture}>
<form>

<label for="yourname">お名前</label>
<input type="text" name="yourname" id="yourname" class="required" />
<br />

Fruit:
<label for="checkbox_orange">orange</label>
<input type="checkbox" name="favorite_fruit[]" id="checkbox_orange" class="required" value="orange" />
<label for="checkbox_apple">apple</label>
<input type="checkbox" name="favorite_fruit[]" id="checkbox_apple" class="required" value="apple" />
<label for="checkbox_pear">pear</label>
<input type="checkbox" name="favorite_fruit[]" id="checkbox_pear" class="required" value="pear" checked="checked" />
<br />

<label for="youremail">email</label>
<input type="text" name="youremail" id="youremail" class="email" />
<br />

<label for="selbox">SELECTサンプル</label>
<select name="selbox" id="selbox" class="required">
	<option value="">-- select it --</option>
	<option value="0">0</option>
	<option value="1">1</option>
	<option value="2" selected="selected">2</option>
	<option value="3">3</option>
</select>
<br />

<label for="content">要件</label>
<textarea name="content" id="body"></textarea>
<br />

<input type="submit" value="内容確認" />
</form>
<{/capture}>
<{formmail}>


Printer friendly page Send this story to a friend

Comments list

jidaikobo  Posted on 2009/2/9 23:40
jidaikobo です。

しつこく FormProcessByHtml.class.php の件ですが、ひとつ気づいたことがあります。

134行目、
if( strstr( $fieldsetblock , $tag ) && preg_match( '#<legend[^>]*>([^<]+)</legend>#' , $fieldsetblock , $sub_regs ) ) {

ですが、legend の中で、html のタグが出てきたときに、legend の値を取得しないようになっているとおもいます。
でも、legend 自体は、DTD としては、中になにか要素がきてもいい要素(*)なので、たとえば span、strong とかがくると、値を取得できなくなります。
フォームの html 側でいくらでも回避方法はあるのですが、せっかく自由に html がかける仕組みですし、

if( strstr( $fieldsetblock , $tag ) && preg_match( '#<legend[^>]*>(.+)</legend>#' , $fieldsetblock , $sub_regs ) ) {

としちゃうとマズいでしょうか?
あいかわらず、なにか勘違いしてたらすみません……

* %inline だからインライン要素限定ですが。
GIJOE  Posted on 2008/12/9 4:44
ditamineさん、こんにちは。

Quote:

ここ、私は否定先読みを使って
if( preg_match( '#<fieldset(?:(?!fieldset).)*'.preg_quote($tag).'.*</fieldset>#isU' , $form_html , $regs ) ) {
とやってました。一応これで上手くいくかと思います。
ありがとうございます。
否定先読み苦手なんですよね
今回はこのまま行きますが、今後何かの折には役立たせていただきます。

Quote:
実は1.60くらいでバグかな?と思っていたのですが、なんやかんやで先送りになってました。その時点でご報告していればよかったですね・・・
ぜひお気軽にバグ報告ください
ditamine  Posted on 2008/12/8 10:50 | Last modified
こんにちは、こちらに投稿させて頂くのは初めてです。いつもpico使わせて頂いております。

すでに解決済みのところ恐縮なのですが、

Quote:

<fieldset>と<legend>の間に</fieldset>が見つかったら終了、って正規表現がパッと書けなかったので、preg_split()でいったん分割しました。

ここ、私は否定先読みを使って
if( preg_match( '#<fieldset(?:(?!fieldset).)*'.preg_quote($tag).'.*</fieldset>#isU' , $form_html , $regs ) ) {
とやってました。一応これで上手くいくかと思います。

実は1.60くらいでバグかな?と思っていたのですが、なんやかんやで先送りになってました。その時点でご報告していればよかったですね・・・
jidaikobo  Posted on 2008/12/5 15:57
jidaikobo です。

Quote:

GIJOE wrotes:
<fieldset>と<legend>の間に</fieldset>が見つかったら終了、って正規表現がパッと書けなかったので、preg_split()でいったん分割しました。

1.75aを試してみてください。
試してみました。バッチリです

Quote:
# いただいたコードが悪いってことはないのですが、<fieldset>のブロックという概念から外れてしまうので。
いや、おっしゃる通りだと思います。
そういう論理一貫性があると気持ちいいですね。

この form 解析の仕組みも、ソースコードを拝見して、とても考え方の参考になりました。

ご対応ありがとうございました〜。
GIJOE  Posted on 2008/12/5 3:41
Quote:

jidaikobo wrotes:
pico の survey プラグインで遊ぼう!の picoSurveyAssistant を書いていて気づいたんですが、pico の formmail プラグインで、checkbox や radio が複数あるフォームの場合、最初の要素の legend が、以降すべての checkbox と radio のタイトルとして、メールで送られているようでした。
本当ですね

<fieldset>と<legend>の間に</fieldset>が見つかったら終了、って正規表現がパッと書けなかったので、preg_split()でいったん分割しました。

1.75aを試してみてください。

# いただいたコードが悪いってことはないのですが、<fieldset>のブロックという概念から外れてしまうので。

## コメントのtypoも修正しました
jidaikobo  Posted on 2008/12/4 15:20 | Last modified
pico の survey プラグインで遊ぼう!の picoSurveyAssistant を書いていて気づいたんですが、pico の formmail プラグインで、checkbox や radio が複数あるフォームの場合、最初の要素の legend が、以降すべての checkbox と radio のタイトルとして、メールで送られているようでした。

で、/trust_path/modules/pico/class/FormProcessByHtml.class.php の 132 行目あたりをチェックしてみました。

// search the nearest <regend> for radio/checkbox
if( preg_match( '#<fieldset.*'.preg_quote($tag).'.*</fieldset>#isU' , $form_html , $regs ) ) {
	if( preg_match( '#<legend[^>]*>([^<]+)</legend>#' , $regs[0] , $sub_regs ) ) {
		$label = strip_tags( @$sub_regs[1] ) ;
	}
}

これを下記のようにしてみたところ、目的の legend の値が、タイトルになって送られたように思われます。
#あっ、コメントにも typo が…… ^^;

// search the nearest <legend> for radio/checkbox
if( preg_match( '#<fieldset.*'.preg_quote($tag).'.*</fieldset>#isU' , $form_html , $regs ) ) {
	if( preg_match_all( '#<legend[^>]*>([^<]+)</legend>#' , $regs[0] , $sub_regs ) ) {
		$sub_regs_tmp = isset( $sub_regs[1] ) ? array_pop( $sub_regs[1] ) : '' ;
		$label = strip_tags( $sub_regs_tmp ) ;
	}
}

これが最良の措置かどうかわからないのですが、ご報告まで〜。
gigamaster  Posted on 2007/9/5 7:20 | Last modified
Since the beginning of hodajuku, i start to work on modules from Chatnoir, Marijuana, Taquino, Wye and some others. I got some issues with translation and it is taking me more time than expected to work on a new U.I.

The web is changing, and there's a new definition/mission for cms/frameworks. The project is missing a script for creating and processing web forms and a distribution with the best D3 modules that match such requirements, communicates and powers human being creativity.
I'm sure the distribution will be a very good thing for everyone.

P.S. 私の名前は綴りである = 言うべき私達の表現...非常に熱狂的な人
but similar to kurekurekun 多分それは本当である !!!
GIJOE  Posted on 2007/9/5 6:16
hi gagamaster.

Quote:

I hope to read more about this new feature and
possibilities to replace liaise or formulize.

I know almost designers avoid to use liaise or formulize because of their inconviniences of design.

Only "InquirySP" made by marijuana can be selected by designers in Japan.
Though "InquirySP" is a good module, it contains a lot of hard-coded Japanese, sadly...
gigamaster  Posted on 2007/9/4 7:19
Wow !!!

This is another big step of my favorite module!


I hope to read more about this new feature and
possibilities to replace liaise or formulize.

Best Regards,

Login
Username or e-mail:

Password:

Remember Me

Lost Password?

Register now!