Based on following line from CakePHP:
trigger_error(__d('cake_dev', 'Could not find validation handler %s for %s', $this->_rule, $field), E_USER_WARNING);
And rules for the field:
'number' => array(
'required' => true,
'isUnique' => array('rule' => 'isUnique', 'on' => 'create'),
'notBlank' => array('rule' => 'notBlank'),
'maxLength' => array('rule' => array('maxLength', 15)))
CakePHP detects required => true as a rule! When I remove that line, everything works fine!
Note: the field already exists into the data:
$data = array(
'name' => 'xxx',
'fields' => 'xxx',
'startYear' => '999',
'biography' => 'xxx',
'headquarter' => 'xxx',
'number' => '(999) 9999-9999',
'tags' => 'xxx',
'updateTime' => '9999999999'
)
How can I fix that problem?
jeremyharris on this discuss helped me. On CakePHP 2.x the required attribute must be used with a rule; But outside of a rule it will be determined as a separated rule.
Related
Here is my code
$users = array(
array( 'username' => $this->request->data['Shop']['admin_username'],
'password' => $this->request->data['Shop']['admin_password'],
'role' => 'admin',
'shop_id' => $shop_id,
'active' => 1
),
array( 'username' => $this->request->data['Shop']['manager_username'],
'password' => $this->request->data['Shop']['manager_password'],
'role' => 'manager',
'shop_id' => $shop_id,
'active' => 1
)
);
Debugger::dump($users);
//$this->User->create();
$this->User->saveMany($users);
where I am trying to save the 2 recoded in Database. But it's not working. I have also tried saveAll but i also did not work.
What does $this->User->saveMany($users); return?
Are there any validation rules you have inside your User Model?
I have 3 tables: projects, project_reminder_users, project_types.
The relations is as follow:
Project => belong to => ProjectType
hasMany => ProjectReminderUser
ProjectReminderUser => belong to => Project
ProjectType => hasMany => Project
I get all data based on who assigned(ProjectReminderUser) by this
$this->Project->ProjectReminderUser->Behaviors->load('Containable');
$this->paginate = array(
'ProjectReminderUser' => array(
'limit' => $limit,
'contain' => array(
'Project' => array(
'ProjectComment',
'ProjectFile',
),
'User'
),
'conditions' => array(
'User.group_id' => $this->Session->read('Auth.User.group_id'),
'ProjectReminderUser.user_id' => $this->Session->read('Auth.User.id'),
'Project.project_status_id' => PROJECT_STATUS_OPEN,
),
'order' => 'Project.due_date',
)
);
$this->set('myTasks', $this->paginate('ProjectReminderUser'));
and the result look like this
array(
'ProjectReminderUser' => array(
'id' => '96',
'user_id' => '1',
'project_id' => '46'
),
'Project' => array(
'id' => '46',
'project_type_id' => '9',
'contact_id' => null,
'company_id' => null,
'subject' => 'Test Modified Field',
'description' => 'Test Modified Field',
'ProjectFile' => array(
(int) 0 => array(
'id' => '19',
'project_id' => '46',
'user_id' => '6',
'file_path' => '46_bhbinary_xmm_1728.jpg',
'notes' => null,
'created' => '2013-11-26 18:37:49'
),
),
'ProjectComment' => array(
)
),
'User' => array(
'password' => '*****',
'id' => '1',
'group_id' => '1',
'email' => 'xxx#xxxxx.com',
'first_name' => 'xxxx',
'deleted' => false,
'displayName' => 'xxxxx'
)
)
In the result There is data for Project.project_type_id, but I would like to have more detail of that. So I can show it as name instead of number. maybe like ProjectType.name instead.
How can I achieve this, so I can sort it in the view? something like this
$this->Paginator->sort('ProjectType.name', 'Type');
The problem is that Paginator doesn't play nice with deep model associations. I think though if rather than using Cake's methods for doing model associations, you instead do your joins manually, you may be able to work out the sorting as you want. See the below discussion.
http://sinkpoint.railsplayground.net/cakephp-pagination-deep-sort-and-habtm/
Worst comes to worse, you may have to also rewrite the model's paginate function to deal with sorting how you need it to.
How about
'contain' => array(
'Project' => array(
'ProjectComment',
'ProjectFile',
'ProjectType',
),
'User'
),
Looks like you did not contain "ProjectType"
I've followed this great document which successfully creates a webform and associated components.
I am trying to adjust the code so it works for existing webforms instead of creating new ones.
My code is as follows:
$nid = 12;
$node = node_load(12);
// Create the webform components.
$components = array(
array(
'name' => 'Gender',
'form_key' => 'gender',
'type' => 'select',
'mandatory' => 1,
'weight' => 0,
'pid' => 0,
'extra' => array(
'title_display' => 'inline',
'private' => 0,
'items' => "Mrs|Mrs\nMiss|Miss\nMr|Mr",
'aslist' => 1,
),
),
);
// Setup notification email.
$emails = array(
array(
'email' => 'somebody#example.tld',
'subject' => 'default',
'from_name' => 'default',
'from_address' => 'default',
'template' => 'default',
'excluded_components' => array(),
),
);
// Attach the webform to the node.
$node->webform = array(
'confirmation' => '',
'confirmation_format' => NULL,
'redirect_url' => '<confirmation>',
'status' => '1',
'block' => '0',
'teaser' => '0',
'allow_draft' => '0',
'auto_save' => '0',
'submit_notice' => '1',
'submit_text' => '',
'submit_limit' => '-1', // User can submit more than once.
'submit_interval' => '-1',
'total_submit_limit' => '-1',
'total_submit_interval' => '-1',
'record_exists' => TRUE,
'roles' => array(
0 => '1', // Anonymous user can submit this webform.
),
'emails' => $emails,
'components' => $components,
);
// Save the node.
node_save($node);
When I attempt to execute my code I get the following error:
Error message SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'nid' cannot be null
First try to print all the available values of $node.
echo "<pre>";
print_r($node);
echo "</pre>"'
exit;
check that you are getting value for $node[nid] or not.
I have read the CakePHP documentation about validation rules, but I'm still stuck with changing the error message on an email field.
I currently have this Validation rule in my model:
public $validate = array(
'emailadres' => array(
'rule' => 'email',
'required' => true,
'allowEmpty' => false,
'message' => 'My custom error message'
)
);
The field shows up as being mandatory, but a standard error message appears instead of my custom message.
Does anybody see what I'm doing wrong?
My CakePHP version is 2.3.7
You might want to double check the documentation: http://book.cakephp.org/2.0/en/models/data-validation.html#one-rule-per-field
Its not 'ruleName' => 'email',, but 'rule' => 'email',.
You can also try the verbose representation:
public $validate = array(
'emailadres' => array(
'email' => array(
'rule' => 'email',
'required' => true,
'allowEmpty' => false,
'message' => 'My custom error message'
)
)
);
I set up rule 'isUnique' and set 'create' value for 'on' option.
Complete code:
'username' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'Empty field',
'required' => true,
),
'between' => array(
'rule' => array('between',3,25),
'message' => 'Too long value'
),
'unique' => array(
'rule' => 'isUnique',
'on' => 'create',
'message' => 'Already taken',
'required' => true,
)
)
But when I'm trying to log in, I get the error message. Why?
CakePHP 2.2.1
The reason is because you have 'required' => true set, which requires the key to be present when validating. This conflicts with the 'on' key, as explained here: http://book.cakephp.org/2.0/en/models/data-validation.html#one-rule-per-field
Instead, make it required only when creating.
'unique' => array(
'rule' => 'isUnique',
'on' => 'create',
'message' => 'Такой никнейм уже занят',
'required' => 'create', // change this to 'create'
)
The problem is, that when you are trying to log in, CakePHP thinks that you are creating a new record (CakePHP identifies it as new record because you don't provide ID of the item like you do when you are editing some item).
The solution could be to create a custom validation function which would check for unique username all the time except when logging in.