Jump to content

CakePHP Validation


244863

Recommended Posts

VIEW:

<div class="column span-24">
    <div class="box">
        <?php
            echo $this->html->tag('h2', $title_for_layout);
            echo $this->form->create('Job', array('action' => 'add'));

            echo $this->form->inputs(array(
                'description' => array(
                    'class' => 'span-22'
                )
            ));
            
            echo $this->element('standard-form-buttons');
        ?>
    </div>
</div>
 

 

CONTROLLER:

 

 public function add() {
        $this->set('title_for_layout', __('Create new Job', true));
        if ($this->request->is('post')) {
            $this->request->data['Job']['created_by'] = $this->Auth->user('id');
            unset($this->request->data['Job']['asset_category_id']);
            $this->Job->set($this->request->data);
            if ($this->Job->validates()) {
                $this->Job->create();
                if ($this->Job->save($this->request->data, array($params = array('validate' => 'first')))) {
                    $this->Session->setFlash('The job has been saved');
                    $this->redirect(array('controller' => 'jobs', 'action' => 'index'));
                } else {
                    $this->Session->setFlash('The job could not be saved. Please, try again.');
                }
            }else{
                $errors = $this->ModelName->invalidFields();
            }
        }
}
 

 

 

MODEL:

<?php
class Job extends AppModel {
    public $validate = array(
        'Job.description' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'The descripton must be completed.'
            )
        )
    );
}
?>
 

 

 

IT won't show the validation errors on the form when I hit save??? any ideas what I am doing wrong?

Link to comment
Share on other sites

Tried that both ways.

 

It just says the standard (Please fill out this field) from the DB as it is a required field and not the validation I stipulated below for minLength???

 

 

 public $validate = array(
        'description' => array(
            'rule'    => array('minLength', ,
            'message' => 'The descripton must be completed.'
        )
    );
 
Link to comment
Share on other sites

That's a different problem than the first post, and different code. It helps if you actually explain the problem.

 

It looks fine, according to the manual.

http://book.cakephp.org/2.0/en/models/data-validation.html

 

What happens when you enter a string that is less than 8 long?

 

Don't forget to read the section on multiple rules.

http://book.cakephp.org/2.0/en/models/data-validation.html#multiple-rules-per-field

Edited by Jessica
Link to comment
Share on other sites

I was using $this->Model->Validates() and $this->Model->Save() which validates as well.

 

When I removed the Validates() and left just the save() everything works.

 

Serves me right for trying to validate twice.

 

thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.