CakePHP 1.2 - Model validation database table field prefixed with id - cakephp

Inspect the 'Qty' form input with Firebug
<input id "Item0Quantity">
I am having difficult validating this element produced by this code:
<?php echo $form->input("Item.$id.quantity.", array('label' => 'Qty', 'class' => 'txt'));?>
I am working with a form input element that is prefixed with the database table name (Item) and a unique id, the reason for this is to add some JavaScript functionality. Though it still needs CakePHP validation on the Item model.
Here is the input form element that needs to be validated:
<?php echo $form->input("Item.$id.quantity.", array('label' => 'Qty', 'class' => 'txt'));?>
So how will the will be written in the items model? My attempt:
'Item.$id.quantity' => array(
'rule' => 'numeric',
'allowEmpty' => true,
'message' => 'quanitity mut be numeric'
),

The validation will look the same, still by just the field name:
'quantity' => array(
'rule' => 'numeric',
'allowEmpty' => true,
'message' => 'quanitity mut be numeric'
),
When you use saveAll Cake will iterate through each Item and validate it accordingly.

Related

CakePHP2 - Default value for input - select with option multiple

I have Form input with multiple select options. I am unable to set default values. This is my code:
<?= $this->Form->input('PaymentMethods', array(
'type' => 'select',
'multiple' => true,
'label' => false,
'options' => array(
'cash'=>'cash',
'invoice'=>'invoice',
'ax'=>'ax',
'ca'=>'ca',
'vi'=>'vi',
'tp'=>'tp',
'dc'=>'dc'
),
'default'=>'ax'
)); ?>
How do I set default values for this input with PHP only?
This is working on my system. You can also set it from controller like this :
$this->request->data[$this->modelClass]['PaymentMethods'] = 'ax';
Please check these url also
CakePHP select default value in SELECT input
http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html
example :
$sizes = array('s' => 'Small', 'm' => 'Medium', 'l' => 'Large');
echo $this->Form->input(
'size',
array('options' => $sizes, 'default' => 'm')
);
Since this is multi-choice select, value given must be array. And the key shouldn't be default, I should've used value instead.
<?= $this->Form->input('PaymentMethods', array(
'type' => 'select',
'multiple' => true,
'label' => false,
'options' => $options,
'value'=> $array_of_data_fetched_from_database
)); ?>

CakePHP inputDefaults and rewrite by field

I've created a large form in Cake and set default options via inputDefaults. However I wish to change the default values for an individual field.
In setting the form defaults, I wrote approximately this:
'inputDefaults' => array(
'error' => array(
'attributes' => array(
'wrap' => 'span',
'class' => 'invalidate column-7 offset-3')));
...with the result that all like fields produce the same error message. But, when I attempt to change the defaults for a single field, like so:
echo $this->Form->input('name', array(
'error' => array(
'attributes' => array(
'wrap' => 'span',
'class' => 'invalidate column-10'))));
It doesn't work. The field name produces an error whose class reads column-7 and offset-3, whereas I'd intended column-10.
Anybody know a solution?
$options['inputDefaults'] You can declare a set of default options for input() with the inputDefaults key to customize your default input creation:
echo $this->Form->create('User', array(
'inputDefaults' => array(
'label' => false,
'div' => false
)
));
All inputs created from that point forward would inherit the options declared in inputDefaults. You can override the defaultOptions by declaring the option in the input() call:
echo $this->Form->input('password'); // No div, no label
// has a label element
echo $this->Form->input(
'username',
array('label' => 'Username')
);

CakePHP Required Fields - Not Making Any Sense

I have a form that it seems to me Cake is making fields required or not required so randomly that I can't even control it. Please help me to understand how to get required fields working.
public $validate = array(
'fname' => array(
'rule1' => array(
'rule' => 'notEmpty',
'message' => 'Please enter your first name'
),
'rule2' => array(
'rule' => array('maxLength', '50'),
'message' => 'First name cannot exceed 50 characters'
)
),
'sname' => array(
'rule1' => array(
'rule' => 'notEmpty',
'message' => 'Please enter your last name'
),
'rule2' => array(
'rule' => array('maxLength', '50'),
'message' => 'Last name cannot exceed 50 characters'
),
),
'email' => array(
'rule1' => array(
'rule' => 'notEmpty',
'message' => 'Please enter your email address'
),
'rule2' => array(
'rule' => array('maxLength', 100),
'message' => array('Email cannot exceed 100 characters')
),
'rule3' => array(
'rule' => 'email',
'message' => 'Please enter a valid email address'
),
'rule4' => array(
'rule' => 'isUnique',
'message' => 'An account already exists with that email address',
'on' => 'update'
)
)
);
All these 3 fields cannot be empty, but Cake is deciding to make fname and sname show as required on the form, but email is not required. I'm talking about the class on the inputs making it look required. This is so random. In the database the fields are all identical VARCHAR, NOT NULL.
I tried adding allowEmpty => false to the email but it doesn't do anything. This works on other fields that only have one rule but doesn't work on fields that have multiple rules. Still, I want to know why it is making fname and sname required but not email.
Screenshot: http://i.imgur.com/3JMTR.gif
Hi in the table for email field make it as could not be
NULL
(I think email column can accept null values like that u created the table) then it Required * symbol will come on view. Surely it will work, I face the same issue like this
Try adding 'required'=true on the rules of the fields you want to be required.
You can switch off all of the email validation rules by:
unset($this->ModelName->validate['email']);
But after watching the screenshoot,i can see that there is no red star above email field.The cause of this maybe you didn't properly named the email input
echo $this->Form->input('email');
Normally if you have this line(if it is generated by bake than must be right),it must have the validation rules for email field

Cakephp: Form Vailidation

I'm trying to create a validator for my models:
But taking the example from http://book.cakephp.org/:
var $validate = array(
'country' => array(
'rule' => 'notEmpty'
)
);
gives the following error: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash [CORE/cake/libs/model/app_model.php, line 166]
By googling this error I found a mailinglist entry that recommended using: ( http://cakephp.1045679.n5.nabble.com/validation-notEmpty-td1320629.html)
'country' => array(
'rule' => VALID_NOT_EMPTY,
'message' => 'This field cannot be left blank.'
)
which didn't work. so I tried:
'country' => array(
'rule' => 'VALID_NOT_EMPTY',
'message' => 'This field cannot be left blank.'
)
Which marked the field as a required, but didn't stop me from leaving it blank.
My question is: how to do this correctly? I'm using CakePHP 1.3.6
Most probably you need to put the required key; something like this:
'country' => array(
'rule' => 'notEmpty',
required => true,
'message' => 'This field cannot be left blank.'
)
Hope this helps.
Why donĀ“t you use the commandline for baking your models (cmd: cake bake)? If you bake the models you can specify the validation rules there.
It is fast and easy... and you can see how validation works.
It helped me a lot...
Here an example code.
'username' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'Username required',
'allowEmpty' => false,
'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
You must use an array for the rule definition...
in your view file just add 'class'='required'
eg:
<?php echo $this->Form->input('new_email',array('class'=>'email required yellow', 'div'=>false, 'label'=>false));?>

Creating 'select' listboxes using FormHelper in CakePHP

I have two models, Category and Point. The associations are defined as:
Category hasMany Point
Point belongsTo Category
I would like, when adding Points to my database, to be able to select the category it belongs to from a <select> box, along with the rest of the form data.
Where would I need to set the category list and how could I do it? And how would I produce the select box?
I assume it could be done with
$form->input('categorieslist',array('type'=>'select')); //categorieslist needs
//setting somewhere.
Also to generalize a bit:
In a View with access to the Form helper
<?php
echo $form->input( 'dataKey', array(
'type' => 'select',
'options' => array(
'key1' => 'val1',
'key2' => 'val2',
),
));
?>
The above will render a select input with two options. You can also place an empty option as the first item. Passing a value of true will simply append an empty option with a blank value to the beginning of the options rendered in the HTML.
<?php
echo $form->input( 'dataKey', array(
'type' => 'select',
'options' => array(
'key1' => 'val1',
'key2' => 'val2',
),
'empty' => true,
));
?>
You can pass a string to the 'empty' key to have it display custom text as the key field for the empty option.
<?php
echo $form->input( 'dataKey', array(
'type' => 'select',
'options' => array(
'California' => 'CA',
'Oregon' => 'OR',
),
'empty' => 'choose a state',
));
?>
One last example, you can also pre-select an option with the selected key. The value should match the value of one of the select options, not the key.
<?php
echo $form->input( 'dataKey', array(
'type' => 'select',
'options' => array(
'California' => 'CA',
'Oregon' => 'OR',
),
'empty' => 'choose a state',
'selected' => 'California',
));
?>
From the Model
Model->find( 'list', array( ... )); will always return an array formatted for use with select box options. If you pass data to your view stored in a variable with a lowercase plural model name, that is, ( $this->set( 'categories', $categories );, then you will automagically generate drop downs for related models by using the form helper in the view and passing it a data index of the same model name in singular form suffixed with "_id".
Aziz's answer at #2 is the example of that automagic kicking in.
CakePHP 1.3 Form Helper
CakePHP1.2 Form Helper
In the controller:
$categories = $this->Point->Category->find('list');
$this->set(compact('categories'));
In the view:
$form->input('category_id',array('type'=>'select'));

Resources