Eduardo Fuerte
asked on
Could you point how to present and validate a combobox value by using CakePHP?
Hi Experts
Could you point how to present and validate a combobox value by using CakePHP?
Accordingly to:
Data Model - There's a FK from Usuarios table to Posts table
The page's layout
View's code: add_with_validation.ctp
The correspondent controller's code
Model's code
Thanks in advance!
Could you point how to present and validate a combobox value by using CakePHP?
Accordingly to:
Data Model - There's a FK from Usuarios table to Posts table
The page's layout
View's code: add_with_validation.ctp
<div class="posts form">
<p>
This form is like the first one but here we validate that a post must have
at least one tag.
</p>
<?php echo $this->Form->create('Post'); ?>
<fieldset>
<legend><?php echo __('Add Post validating the number of tags'); ?></legend>
<?php
echo $this->Form->input('name');
// Tags correctly presented
echo $this->Form->input('Post.Tag',array('label'=>'At least one Tag', 'type'=>'select', 'multiple'=>true));
//Code to present the Users combo (what I need to be done)
echo $this->Form->input('Post.Usuario',array('label'=>'At least one Usuario', 'type'=>'select', 'multiple'=>true));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List Posts'), array('action' => 'index')); ?></li>
</ul>
</div>
The correspondent controller's code
public function add_with_validation() {
if ($this->request->is('post')) {
$this->Post->create();
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(__('The post has been saved'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The post could not be saved. Please, try again.'));
}
}
$tags = $this->Post->Tag->find('list');
$this->set(compact('tags'));
// I guess something must to be done here to consider "usuarios" - my tryes resulter in errors...
}
Model's code
<?php
App::uses('AppModel', 'Model');
/**
* Post Model
*
* @property Tag $Tag
*/
class Post extends AppModel {
/**
* Display field
*
* @var string
*/
public $displayField = 'name';
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'name' => array(
'notempty' => array(
'rule' => array('notBlank'),
),
),
// My Try to validate the usuario combo contents
'Usuario' => array(
'multiple' => array(
'rule' => array('multiple', array('min' => 1)),
'message' => 'You need to select at least one tag',
'required' => true,
),
),
'Tag' => array(
'multiple' => array(
'rule' => array('multiple', array('min' => 1)),
'message' => 'You need to select at least one tag',
'required' => true,
),
),
);
Thanks in advance!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi
Thank you for the interation.
My doubt involves the presentation part also, since the combo's presentation.
Thank you for the interation.
My doubt involves the presentation part also, since the combo's presentation.
Ah is it a tag that the user can type in ?
ASKER
Thnak you Rob
I need to better understand the overall context based on what you posted and then interact again.
I need to better understand the overall context based on what you posted and then interact again.
I would have at least 2 levels of verification. Verifying the data is correct as you need it e.g. a certain set of values and nothing elde as well as setting up your model so that invalid data cannot be saved e.g. text when it should be numeric.
You can do this in the View, Controller and Model.
The View "verification" is purely aesthetics as it's easy to bypass for anyone that knows a little about how HTML is processed but it does help guide the majority of your users e.g. Please enter a valid phone number etc
The Controller is where I would start doing proper verification such as checking that required fields have a value. I wouldn't necessarily be checking where that value is valid for your application as you'll just be repeating it again when another controller function needs to update the same Model.
The Model is where I would control that the data that has been entered is valid for your application, as multiple Controllers could be saving data to the same Model (e.g. Sign Up versus Log in versus Profile Update).
Start with the Controller checking that fields are present and have the Model reject invalid data when saving.