Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7896069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:47:55+00:00 2026-06-03T07:47:55+00:00

I am building a test Lithium app to learn how it works and I

  • 0

I am building a test Lithium app to learn how it works and I found that the form helper doesn’t seem to recognise my data being passed back or any validation errors.

At the moment I’m having to manually pass back my errors and then process them in the view.

QuestionsController::ask

public function ask() {
    if (!empty($this->request->data)) {
        $question = Questions::create($this->request->data);
        if ($question->save()) {
            return $this->redirect(array('Questions::view', 'args'=>$question->id));
        } else {
            $errors = $question->errors();
        }

        if (empty($question)) {
            $question = Questions::create();
        }

        return compact('question', 'errors');
    }
}

views/questions/ask.html.php

<?php
// Assign the protected object to a variable so we can get at it
if(isset($question)){
    $data = $question->data();
}else{
    $data['title'] = '';
    $data['text'] = '';
}
?>

<?=$this->form->create();?>

    <?=$this->form->field('title', array('placeholder'=>'Title your question', 'value'=>$data['title']));?>
    <?php if(isset($errors['title'])){
        echo "<div class='alert alert-error'><a class='close' data-dismiss='alert' href='#'>×</a>";
        foreach($errors['title'] as $e){
            echo $e."<br/>";
        }
        echo "</div>";
    }?>

    <?=$this->form->field('text', array('placeholder'=>'Enter your question (supports Markdown)', 'type'=>'textarea', 'value'=>$data['text']));?>
    <?php if(isset($errors['text'])){
        echo "<div class='alert alert-error'><a class='close' data-dismiss='alert' href='#'>×</a>";
        foreach($errors['text'] as $e){
            echo $e."<br/>";
        }
        echo "</div>";
    }?>
    <p>Text input supports <?php echo $this->html->link('markdown', 'http://en.wikipedia.org/wiki/Markdown');?></p>

    <?=$this->form->submit('Ask', array('class'=>'btn'));?>
<?=$this->form->end();?>

I can see from the lithium\template\helper\Form that the field() method can take a template parameter, which in the example is <li{:wrap}>{:label}{:input}{:error}</li> so there is capacity in the helper for displaying the validation messages.

So how do I organise my data in my controller so that it’s passed back to the view in order for the helper to populate my fields and also display errors?

Edit
I should add that the example ‘Sphere’ app, also uses this method, so is it standard? (ref)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:47:56+00:00Added an answer on June 3, 2026 at 7:47 am

    TL;DR

    the short answer is that you can bind the form to a subclass of Entity, i.e. a Record or Document as shown in Form::create() (link is shortened as :: breaks the link parser).

    your code would look like:

    <?= $this->form->create($question); ?>
    <?= $this->form->field('title'); ?>
    <?= $this->form->field('text'); ?>
    

    Long answer:

    QuestionsController::ask():

    public function ask() {
        $question = Questions::create();
        if (!empty($this->request->data)) {
            if ($question->save($this->request->data)) {
                return $this->redirect(array('Questions::view', 'args'=>$question->id));
            }
        }
        return compact('question');
    }
    

    views/questions/ask.html.php:

    <?= $this->form->create($question); ?>
    
        <?= $this->form->field('title', array('placeholder'=>'Title your question'));?>
    
        <?= $this->form->field('text', array('placeholder'=>'Enter your question (supports Markdown)', 'type'=>'textarea'));?>
    
        <p>Text input supports <?php echo $this->html->link('markdown', 'http://en.wikipedia.org/wiki/Markdown');?></p>
    
        <?=$this->form->submit('Ask', array('class'=>'btn'));?>
    
    <?=$this->form->end();?>
    

    Note how the form helper will automatically display errors in case there are any 🙂

    Some useful links into the #li3 philosophy:

    1 – http://www.slideshare.net/nateabele/lithium-the-framework-for-people-who-hate-frameworks

    2 – Video of roughly the same presentation (w/ a good example on changing form templates)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a test app on AWS. I have a 'master' machine that owns
i just started building a small test app to help me learn MVC. i
I've been building a test application that works with a database that up until
To test an app I'm building, I want to be able to emulate the
I'm building a application that sends a test message to another email, the program
Does anyone have experience building a .NET test execution harness that can execute the
I'm new to rails and building a small test app on rails3 (beta4). I
I'm building software that lists apps for users to test. With respect to Android,
I'm building a simple app and trying to test DB result output. But unfortunately
G'day guys, I'm currently flitting through building a test Auction website to learn rails.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.