textfield

Drupal form default input focus

Drupal form FAQ: How do I get default input focus on a Drupal form field (presumably a form textfield)?

There may be a better way to do this, but here's what I just did to get default input focus on a series of Drupal form 'add' and 'edit' pages. I just included the following Drupal form suffix definition on any form where I wanted to control the default input focus:

The Drupal form field required syntax

Drupal form fields FAQ: What is the syntax to make a Drupal form field (textfield, textarea, password field) required?

To make a Drupal form field required, just use the Drupal form #required attribute, as shown in this required textfield example:

$form['first_name'] = array(
  '#title' => t('First Name'),
  '#type' => 'textfield',
  '#required' => TRUE,
);

As you can see, to set a Drupal form field to be required, you just need to add the #required form attribute, and set it to TRUE.

 

Drupal form textfield help text (description) syntax

Drupal form textfield FAQ: How do I set the "help" text for a Drupal form textfield?

The help text that you normally see with a Drupal textfield comes from the Drupal form "#description" attribute. Here's a quick example:

Drupal form textfield default value syntax

Drupal form textfield FAQ: How do I set the default value for a textfield in a Drupal form?

Just use the Drupal form "#default_value" tag, like this:

$form['first_name'] = array(
  '#title' => t('First Name'),
  '#type' => 'textfield',
  '#default_value' => 'John',
);

Here's an example of setting the default value of a Drupal textfield to a variable instead of a static text string:

Drupal form textfield required syntax

Drupal form textfield FAQ: What is the syntax to make a Drupal textfield required?

Just use the Drupal form #required property, like this:

$form['first_name'] = array(
  '#title' => t('First Name'),
  '#type' => 'textfield',
  '#required' => TRUE,
);

In that example the Drupal form textfield is made required by setting the "#required" tag equal to TRUE.

 

A Drupal form textfield example

Drupal form textfield FAQ: Can you share an example of how to create an HTML textfield in a Drupal form (Drupal 6 or Drupal 7)?

Assuming that you're already comfortable with creating a Drupal form, you can create a Drupal form textfield as easily as this:

# an html textfield for our drupal form
$form['first_name'] = array(
  '#title' => t('First Name'),
  '#type' => 'textfield',
);

That code creates an HTML textfield with the label "First Name", as you can see here:

A simple Drupal form example ("My first Drupal form")

Drupal form FAQ: Can you share an example of a simple Drupal form? (i.e., a "My first Drupal form" example.)

I've written before about the Drupal learning cliff (learning curve), and as that name implies, getting started with Drupal form development can be a little hard. To that end I'm taking this weekend to write all the form examples I can fit into one weekend.

A CakePHP readonly text field

CakePHP readonly text field FAQ: Can I make a readonly textfield in CakePHP (a CakePHP Form input field, or Form text field)?

I haven't worked it all out yet, but I first tried to create a CakePHP readonly text field like this, using the CakePHP Form text object, which doesn't seem to work:

# this doesn't work
echo $this->Form->text('complexity', array('readonly' => 'readonly'));

When that failed I kept poking on the CakePHP Form input object, and was able to create a readonly text field like this:

A Perl CGI (CGI.pm) textfield form example

Here's the source code for a Perl CGI textfield example (a Perl form example). The Perl code below shows how you can display an HTML form with a textfield using the Perl CGI.pm module.

The first time this script is called it displays a textfield in a form. After you submit the form, this script displays the text that you entered in the textfield.

Here's the source code for this CGI.pm "Perl form" textfield example script:

JDialog input focus - How to get input focus on a JDialog

I just solved a problem I was having in a Java Swing application getting input focus on a JDialog in a homemade text editor I use on my Mac OS X systems. Using my initial Java code the JDialog was properly getting input focus the first time it was displayed, but it was not properly getting input focus when it was displayed a second (or any subsequent) time.

Syndicate content