Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS 

PHP: Create a SELECT input field

Creates a SELECT input field with an optional parameter to preselect an item

function selectfield($optionsarray, $selected = "") {
  $returnval = "";
  foreach ($optionsarray as $field=>$value) {
    if ($field == $selected) {
      $returnval .= "<option selected value='" . $field . "'>" . $value . "</option>\n";
    } else {
      $returnval .= "<option value='" . $field . "'>" . $value . "</option>\n";
    }
  }
  
  return $returnval;
}

Actionscript _Text Class

Useful functions for adding dynamic text fields. Don't forget, you have to add the font of choice into the main movie Library for this to work (Library Panel Menu > New Font). The name you give the font is the string that you pass to the getTextFormat function for the "font" parameter. Setup your font styles with getTextFormat, setup your dynamic text box with getTextField, and then add text to the text box with appendTextField.

dynamic class _Text {
	static function getTextFormat(font:String,size:Number,color:Number,leading:Number,url:String,target:String){
		var fmt:TextFormat = new TextFormat();
		fmt.font = font;
		fmt.kerning = true;
		fmt.leading = (leading != undefined && leading != null) ? leading : 0;
		fmt.bold = false;
		fmt.size = (size != undefined && size != null) ? size : 11;
		fmt.color = (color != undefined && color != null) ? color : 0x000000;

		if (url != undefined && url != null) {
			fmt.url = url;
			fmt.target = (target != undefined && target != null) ? target : "_self";
		}
		return (fmt);
	}
	
	static function getTextField(targetMC,targetDepth,txtFieldName,x,y,w,h,txtObj){
		var targetDepth = (targetDepth != undefined && targetDepth != null) ? targetDepth : targetMC.getNextHighestDepth();
		var txtfld = targetMC.createTextField(txtFieldName, targetDepth, x, y, w, h);
		txtfld.antiAliasType = (txtObj.antiAliasType) ? txtObj.antiAliasType : "advanced";
		txtfld.sharpness = (txtObj.sharpness) ? txtObj.sharpness : -60;
		txtfld.thickness = (txtObj.thickness) ? txtObj.thickness : -100;
		txtfld.embedFonts = (txtObj.embedFonts) ? txtObj.embedFonts : true;
		txtfld.selectable = (txtObj.selectable) ? txtObj.selectable : false;
		txtfld.html = (txtObj.html) ? txtObj.html : true;
		txtfld.multiline = (txtObj.multiline) ? txtObj.multiline : true;
		txtfld.autoSize = (txtObj.autoSize) ? txtObj.autoSize : "left";
		
		if (txtObj.htmlText) txtfld.htmlText = txtObj.htmlText;
		if (txtObj.txtFormat) txtfld.setTextFormat(txtObj.txtFormat);
		
		if (txtObj.wordWrap == undefined || txtObj.wordWrap == null) txtfld._width = txtfld.textWidth + 10;
		else txtfld.wordWrap = txtObj.wordWrap;
		return txtfld;
	}
	
	static function appendTextField(txtfld,txtfrmt,txt){
		var beginIndex:Number = txtfld.htmlText.length;
		txtfld.setNewTextFormat(txtfrmt);
		txtfld.replaceText(beginIndex,beginIndex,txt);
	}
}

Putting the errors in the right place

The idea is to display the error near the field instead of in global area at the top of the page.
Simple… first go into your view and delete the
error_messages_forobject’


Then stick this in your application helper.


# application_helper.rb
def error_for(object, method = nil, options={})
if method
err = instance_variable_get("@#{object}").errors.on(method).to_sentence rescue instance_variable_get("@#{object}").errors.on(method)
else
err = @errors["#{object}"] rescue nil
end
options.merge!(:class=>’fieldWithErrors’,
:id=>"#{[object,method].compact.join(’_')}-error",
:style=>(err ? #{options[:style]}":"#{options[:style]};display: none;")
)
content_tag("p",err || "", options )
end


Then in your form view, add an ‘error_for’ call wherever you need one…

# _form.rhtml
  <p><label for="code_project_name">Name</label>
  <%= text_field ‘code_project’, ‘name’  %>
  <%= error_for ‘code_project’, ‘name’ %></p>


If the model fails a validation test, then it will show the message right next to the field that caused the validation problem.
Also note that if you define an instance variable called @errors containing a hash of field_names and messages, they will also be used. This is handy for those form fields that don’t correspond to a model attribute.

Java - PasswordField

JPasswordField passField = new JPasswordField(10);
passField.setEchoChar('*');

evaluate form fields with php

// contains all types of fields

// of special interest are selection boxes.
// They can return an array.
// Here is a quick evaluation with implode().

<!-- Formular auswerten (evaluate form)
     ********************************** -->

<?php
$hidden_value_1 = $_POST['hval1'];
$hidden_value_2 = $_POST['hval2'];

$short_text	= $_POST['stext'];
$password	= $_POST['pwd'];
$long_text	= $_POST['ltext'];

$selected_option = $_POST['optn'];
$radio_selection = $_POST['radsel'];

$check_selection = $_POST['checksel'];
$check_text = implode(', ',$check_selection);
?>



<html><body>
<h1>Kurze Anzeige (show values)</h1>
Versteckte Werte: <?=$hidden_value_1?>, <?=$hidden_value_2?><br>
Kurzer Text und Passwort: <?=$short_text?>, <span style="color: gray;"><?=$password?></span><br>
Langer Text:  <?=$long_text?><br>
Option: <?=$selected_option?><br>
Radio-Auswahl: <?=$radio_selection?><br>
CheckBox-Auswahl: <?=$check_text?>

<h1>Ein Formular (form)</h1>

<!-- Das Beispielformular (form example)
     ********************************** -->
<form action="form.php" method="post">

<!-- versteckte Elemente (hidden elements) -->
<input type="hidden" name="hval1" value="Der erste versteckte Wert">
<input type="hidden" name="hval2" value="Der zweite versteckte Wert">
<p>

<!-- einzeiliges Eingabefeld und Passwortfeld (text fields) -->
Kurzer Text und Passwort <br>
<input type="text" size="32" maxlength="64" name="stext" value="Kurze Textvorbelegung">
<input type="password" size="16" maxlength="16" name="pwd" value="geheim">
</p><p>

<!-- mehrzeiliges Eingabefeld (text area) -->
Langer Text <br>
<textarea cols="128" rows="4" name="ltext">
Optionale Textvorbelegung (optional text presetting): kann bei
mehrzeiligen Textfeldern lang sein,da genügend Platz vorhanden ist.
</textarea>
</p><p>

<!-- Auswahlliste mit Vorauswahl (selection list) -->
Option <br> <select name="optn">
<option selected>Die Erste Option</option>
<option>Die zweite Option</option>
<option>Die dritte Option</option>
</select>
</p><p>

<!-- Radio-Buttons mit Vorauswahl (radio buttons) -->
Radio-Auswahl <br>
<input type="radio" name="radsel" value="first">Die erste Radiowahl<br>
<input type="radio" name="radsel" value="seccond">Die zweite Radiowahl<br>
<input type="radio" name="radsel" value="third" checked> Die dritte Radiowahl
</p><p>

CheckBox-Auswahl <br>
<input type="checkbox" name="checksel[]" value="chk1" checked>Die erste Checkwahl<br>
<input type="checkbox" name="checksel[]" value="chk2" checked>Die zweite Checkwahl<br>
<input type="checkbox" name="checksel[]" value="chk3">Die dritte Checkwahl
</p><p>

<!-- Buttons zum Absenden/ Abbrechen (buttons to submit/reset) -->
<input type="submit" value="auswerten">
<input type="reset" value="zurücksetzen">

</form>
</body></html>

Use background color to show error fields instead of wrapping them in a div

Using the default rails field_error_proc may lead to some layout headaches--your form looks perfect until, uh-oh, someone entered an invalid email address and Rails adds a fieldWithError-styled div that wraps around the problem field.

While this works in many cases, some pixel-perfect layouts may not be able to tolerate the 2-pixel padding around the text_field caused by the red border. An alternative is to change the background color of the offending field.

Include the following code in environment.rb (or use a "require" like I do):

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
  error_style = "background-color: #ffff80"
  if html_tag =~ /<(input|textarea|select)[^>]+style=/
    style_attribute = html_tag =~ /style=['"]/
    html_tag.insert(style_attribute + 7, "#{error_style}; ")
  elsif html_tag =~ /<(input|textarea|select)/
    first_whitespace = html_tag =~ /\s/
    html_tag[first_whitespace] = " style='#{error_style}' "
  end
  html_tag
end


See my blog.inquirylabs.com for more Rails stuff.
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS