Tutorial

Form Handling and Validation in Oorian

Complete guide to building forms with validation, error handling, and data binding in Oorian.

M. WarbleMarch 19, 20263 min read
Form Handling and Validation in Oorian

Forms are the backbone of most web applications. Oorian provides a robust, type-safe approach to form handling that leverages Java's strengths while keeping your code clean and maintainable.

Basic Form Structure

@Page("/contact")
public class ContactPage extends HtmlPage implements FormListener
{
    private TextInput nameInput;
    private TextInput emailInput;
    private TextArea messageInput;

    @Override
    protected void createBody(Body body)
    {
        OorianForm form = new OorianForm();
        form.registerListener(this, FormEvent.class);

        nameInput = new TextInput();
        nameInput.setName("name");
        nameInput.setPlaceholder("Your Name");
        form.addElement(createFormGroup("Name", nameInput));

        emailInput = new TextInput();
        emailInput.setName("email");
        emailInput.setPlaceholder("your@email.com");
        form.addElement(createFormGroup("Email", emailInput));

        messageInput = new TextArea();
        messageInput.setName("message");
        messageInput.setRows(5);
        form.addElement(createFormGroup("Message", messageInput));

        Button submit = new Button("Send Message");
        form.addElement(submit);

        body.addElement(form);
    }

    @Override
    public void onEvent(FormEvent event)
    {
        Parameters params = event.getParameters();
        String name = params.getParameterValue("name");
        String email = params.getParameterValue("email");
        String message = params.getParameterValue("message");

        // Process form data
        sendContactEmail(name, email, message);
        showSuccessMessage();
    }
}

Form Validation

Validate on the server side where you have full control:

@Override
public void onEvent(FormEvent event)
{
    Parameters params = event.getParameters();
    List<String> errors = new ArrayList<>();

    String name = params.getParameterValue("name");
    if (name == null || name.trim().isEmpty())
    {
        errors.add("Name is required");
        highlightError(nameInput);
    }

    String email = params.getParameterValue("email");
    if (!isValidEmail(email))
    {
        errors.add("Please enter a valid email address");
        highlightError(emailInput);
    }

    if (!errors.isEmpty())
    {
        showErrors(errors);
        return;
    }

    // Proceed with valid data
    processForm(name, email);
}

private void highlightError(Element input)
{
    input.setBorder("2px solid #dc2626");
}

private void clearError(Element input)
{
    input.setBorder("1px solid #d1d5db");
}

Real-Time Validation

For immediate feedback as users type:

public class ContactPage extends HtmlPage implements FormListener, InputListener
{
    @Override
    protected void createBody(Body body)
    {
        emailInput = new TextInput();
        emailInput.registerListener(this, InputChangeEvent.class);
        // ...
    }

    @Override
    public void onEvent(InputChangeEvent event)
    {
        if (event.getSource() == emailInput)
        {
            String email = emailInput.getValue();
            if (email.length() > 3 && !isValidEmail(email))
            {
                emailError.setText("Invalid email format");
                emailError.setDisplay(Display.BLOCK);
            }
            else
            {
                emailError.setDisplay(Display.NONE);
            }
        }
    }
}

Working with Select Elements

Select countrySelect = new Select();
countrySelect.setName("country");
countrySelect.addOption("", "Select a country");
countrySelect.addOption("us", "United States");
countrySelect.addOption("uk", "United Kingdom");
countrySelect.addOption("ca", "Canada");
countrySelect.setSelectedValue("us");

// Handle selection changes
countrySelect.registerListener(this, InputChangeEvent.class);

Checkbox and Radio Buttons

// Checkbox
Checkbox newsletter = new Checkbox();
newsletter.setName("newsletter");
newsletter.setChecked(true);

// Radio buttons
RadioButton small = new RadioButton();
small.setName("size");
small.setValue("small");

RadioButton medium = new RadioButton();
medium.setName("size");
medium.setValue("medium");
medium.setChecked(true);

RadioButton large = new RadioButton();
large.setName("size");
large.setValue("large");

Form Data to Objects

Map form data to domain objects cleanly:

@Override
public void onEvent(FormEvent event)
{
    Customer customer = extractCustomer(event.getParameters());
    validateAndSave(customer);
}

private Customer extractCustomer(Parameters params)
{
    Customer customer = new Customer();
    customer.setName(params.getParameterValue("name"));
    customer.setEmail(params.getParameterValue("email"));
    customer.setPhone(params.getParameterValue("phone"));
    customer.setNewsletter(
        "on".equals(params.getParameterValue("newsletter"))
    );
    return customer;
}

Conclusion

Form handling in Oorian is straightforward and type-safe. Server-side validation gives you complete control, while real-time validation provides immediate user feedback. The familiar Java patterns make form code easy to write and maintain.

Related Articles

Tutorial

Getting Started with Oorian: Your First Java Web Application

December 31, 2025
Security

Security by Default: How Oorian Protects Your Applications

January 11, 2026
Announcement

Why We Built Oorian: The Story Behind the Framework

January 7, 2026