user760955
user760955

Reputation:

CakePHP: Error message not showing Validate Fields

I am new to CakePHP. I have two problem with the view.

Here is my view input.ctp

echo $this->Form->input('fileId', array(
    'type'=>'text', 
    'style' => 'width: 200px; height: 15px'
));
echo $this->Form->end('Save Post');

Here is my model:

var $validate = array(
    'fileId' => 'notEmpty',
    'message' => 'Should not be empty'
    );

Controller:

if ($this->request->is('post')) {
    $data = $this->request->data;
    if ($data) {
        // saving the data
    }
}

Upvotes: 3

Views: 8697

Answers (4)

Sapan Diwakar
Sapan Diwakar

Reputation: 10966

If you are not using save then you need to manually validate the data using validates. In such case you also need to set the data. For e.g. in your controller

   $this->ModelName->set($data);
   $this->Modelname->validates();

Upvotes: 2

Paulo Rodrigues
Paulo Rodrigues

Reputation: 5303

For validate your data, you should have something like this:

public $validate = array(
    'fileId' => array(
        'rule' => 'notEmpty',
        'message' => 'Should not be empty'
    )
);

And your Controller:

if ($this->request->is('post')) {
    if ($this->Model->save($this->request->data)) {
        // saved
    }
}

If you can not save, the error will be shown near the corresponding field. Or you can customize your error using $this->Model->validationErrors array.

For the line break question, make sure that 200px does the automatic line break because of where these elements are positioned.

Upvotes: 2

Krishna
Krishna

Reputation: 1540

you can customize your output by following method:

<tr>
    <td><label>Username</label></td>
    <td>
        <?php echo $this->Form->input('username',array('label'=>false,'div'=>false,'error'=>false)); ?>
    </td>
<td><?php echo $this->Form->error('username'); ?></td>
</tr>

This method will give you output in same line.

Upvotes: 0

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35983

validation errors appear when validates() or save() was called. setup your action completely. If you're not using FormHelper::input, which outputs the field, label and error, you need to manually output the error as well using $this->Form->error('fileId').

And for the form try this: add this to your css

label { float: left; 
        width: 150px; 
        display: block; 
        clear: none; 
        text-align: left; 
        vertical-align: middle; 
        padding-right: 0px;} 

.xg { 
display: block; 
float:left; 
} 

echo $this->Form->input('fileId', array('div'=>'xg','type'=>'text', 'style' => 'width: 200px; height: 15px'));
echo $this->Form->end('Save Post');

Upvotes: 1

Related Questions