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

  • Home
  • SEARCH
  • 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 8522501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:08:14+00:00 2026-06-11T07:08:14+00:00

Iam new in cakephp ,I need to validate a form. This is the code:

  • 0

Iam new in cakephp ,I need to validate a form.

This is the code:
Controller:

<?php
class TasksController extends AppController {
    var $name = 'Tasks';
    var $helpers = array('Html','Form','Session');
     public function index(){
     }
    function add_task()
    {
        if(!empty($this->data)) {
            //print_r($this->data);
            $this->Task->set($this->data);
            if ($this->Task->validates()) {
                // it validated logic
                //echo "ttt";
            } else {
                // didn't validate logic
                echo $errors = $this->Task->validationErrors;
            }
        }
    }
}
?>

Model:

<?php
    class Task extends AppModel
    {
        public var $name = 'Task';
        var $useDbConfig = 'travanco_erp';
        public var $useTable = 'tbl_tasks'; // This model uses a database table 'exmp'
        public var $validate = array(
                    'task_title_mm' => array(
                            'rule' => 'notEmpty',
                            'required' => true,
                            'message' => 'The title field is required'
                    ),
                    'task_description_mm' => array(
                            'rule' => 'notEmpty',
                            'required' => true,
                            'message' => 'The description field is required'
                    ),
                    'task_from_mm' => array(
                            'rule' => 'notEmpty',
                            'required' => true,
                            'message' => 'The from date field is required'
                    ),
                    'task_to_mm' => array(
                            'rule' => 'notEmpty',
                            'required' => true,
                            'message' => 'The to date field is required'
                    )
            );

    }
?>

This is the view:

<div class="employeeForm" style="width:64%; padding:10px 30%;"> 

            <?php echo $this->Form->create('test', array('class'=>'form'));?>
            <fieldset style="width:36em; padding:0px 0px;">
                    <div style="width:475px; font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#333333; font-weight:bold; margin-left:20px; margin-top:10px;">Add Task</div>
                    <br/>
                <?php
                    /*echo $this->Form->input('task_ids_mm',        array(  'div'=>'frm_filed_new',
                                                                    'error' => array(   'wrap' => 'div',
                                                                                        'class' => 'formerror'
                                                                                    ),
                                                                    'label' => 'Task ID',
                                                                ));*/


                    echo $this->Form->input('task_title_mm',        array(  'div'=>'frm_filed_new',
                                                                    'error' => array(   'wrap' => 'div',
                                                                                        'class' => 'formerror'
                                                                                    ),
                                                                    'label' => 'Title',
                                                                ));


                    echo $this->Form->input('task_description_mm',  array(  'type' => 'textarea',
                                                                        'cols'=>60,
                                                                        'rows' => 5,
                                                                        'div'=>'frm_filed_new',
                                                                        'error' => array(   'wrap' => 'div',
                                                                                            'class' => 'formerror'
                                                                                    ),
                                                                        'label' => 'Description',
                                                                ));

                    echo $this->Form->input('task_from_mm',     array(  'div'=>'frm_filed_new','id'=>'task_from_mm','value'=>'',
                                                                    'error' => array(   'wrap' => 'div',
                                                                                        'class' => 'formerror'
                                                                                    ),
                                                                    'label' => 'From',
                                                                ));
                    echo $this->Form->input('task_to_mm',   array(  'div'=>'frm_filed_new','id'=>'task_to_mm','value'=>'',
                                                                    'error' => array(   'wrap' => 'div',
                                                                                        'class' => 'formerror'
                                                                                    ),
                                                                    'label' => 'To',
                                                                ));

                ?>  
                <br/>
                <?php echo $this->Form->button('Submit', array('type'=>'submit','escape'=>true)); ?>
            </fieldset>  
            <?php echo $this->Form->end(); ?>

        </div>

The validation not working.

What is the error in my code?
How can i solve this?

EDIT:

It is the mistake of misconfiguration of databse.php file.Now its corrected .And the print_r($errors) displays the errors.But that not displayed in the view page , i mean near the textboxes.

This is that error array:
Array ( [task_title_mm] => Array ( [0] => The title field is required ) [task_description_mm] => Array ( [0] => The description field is required ) [task_from_mm] => Array ( [0] => The from date field is required ) [task_to_mm] => Array ( [0] => The to date field is required ) )

How can i put it in near the text box?

  • 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-11T07:08:16+00:00Added an answer on June 11, 2026 at 7:08 am

    CakePHP is designed to automatically validate model and display validation errors. Auto validation runs on model save. In your case:

    $this->Task->save($this->request->data);
    

    above will trigger validation. There is no need to run: $this->Task->validates() – If you do so, you also have to take care of displaying validation error by your own. So I think you simply should try:

    <?php
    class TasksController extends AppController {
      var $name = 'Tasks';
      var $helpers = array('Html','Form','Session');
    
      function add_task()
      {
    
        if ($this->request->is('post')) {
          // If the form data can be validated and saved...
          if ($this->Task->save($this->request->data)) {
             //saved and validated
          }
        }
      }
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi iam new to c++ and iam trying out this vector program and i
CakePHP is driving me nuts! Here is my code: if($this->Page->save($datavalue)) { $this->Session->setFlash('Page content updated
Hi I just thought I would share this here I am new to cakephp
I am new to CakePHP. I would like to use the model validate mechanism,
Well, I am new to CakePHP. So a painful day to debug this. Here
I am new to cakePHP and fairly new to PHP as well, I have
I am new in cakephp and I want to import controller in my controller
I am new to CakePHP so please be easy on me. I have this
I am very new to PHP and even more new to CakePHP, which is
I am new to cakephp and I'm trying to accomplish something that should be

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.