3

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

  • There is line break between text field name and text field area. I have tried to pass 'div' => false but that didn't work. How can I remove line break and display both on same line?

  • I have added validation rule to this textfield but when I click save Error message doesn't show up. Do I need to do something else beside adding validates in my model?

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
    }
}
4
  • Can you post the controller when you save your data? Commented Oct 11, 2012 at 10:38
  • try to stick to conventions. (db) fields are lowercase_underscored. Commented Oct 11, 2012 at 12:24
  • Move styles to the stylesheet. Post a piece of questioned HTML. Commented Oct 11, 2012 at 12:31
  • What is the version of cake ? Commented Oct 11, 2012 at 13:52

4 Answers 4

2

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();
Sign up to request clarification or add additional context in comments.

Comments

2

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.

3 Comments

I've updated my model as you said. A red asterix symbol is there near File Id label but again if filed is empty and I click save post button it doesn't show any error message near this label. FYI I am assuming that if field is empty it will show error message on pressing save button.
Controller method is big. I've shown the relevant part of it. I am not using save. Is there any other way to show errors?
Yes, you need to call save, otherwise the validation method will not run. Or, you can call validates(), check this.
1

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');

7 Comments

That doesn't show any error message when I click on Save Post button while keeping text field empty.
I think adding 'between' => '<br />' is adding line break between label and field. I want to remove line break and show them in the same line.
Ok I undestrand now, I have edit my answer, think that in this mode you have label and input into the same line after you can configure what you want padding... for the model I'm searching..
I am using only one text field so I haven't used any css file on my own.
I have edit my answer for the model, without css you cannot position your element into your site you have to implement into an existent css you own css line
|
0

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.

1 Comment

You do not need a table to layout a form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.