Here is the error message:
Warning (2): preg_match() [http://php.net/function.preg-match]: Delimiter must not be alphanumeric or backslash [CORE/cake/libs/model/model.php, line 2611]
This is happening when I call the following code from my controller:
$this->Account->save($this->data)
The model looks like this:
class Account extends AppModel
{
var $validate = array(
'first_name' => array(
'rule' => array('minLength', 1),
'required' => true
),
'last_name' => array(
'rule' => array('minLength', 1),
'required' => true
),
'password' => array(
'rule' => array('minLength', 8),
'required' => true
),
'email' => array(
'emailRule1' => array(
'rule' => 'email',
'required' => true,
'message' => 'You must specify a valid email address'
),
'emailRule2' => array(
'rule' => 'unique',
'message' => 'That email address is already in our system'
)
)
);
}
I found a similar problem explained here
He solved it by changing required' => true to required' => array(true) I tried that for every occurange in my model but it did not fix the problem.
The problem was I named the rule unique it should be isUnique instead.
I would have figured this out much faster with a better error message.
Related
Hi i encounter strange problem, i have this input in my form
$this->Form->input('processing_data', array('label' => 'STH', 'required' => 'required'));
and this produces this html
<input id="UserProcessingData" type="checkbox" value="1" required="required" name="data[User][processing_data]">
my model validating this field is User model and code of it :
'processing_data' => array(
'rule' => 'notEmpty',
'allowEmpty' => false,
'message' => 'Prosze zaznaczyć'
),
but let's say someone manually delete required="required" from input and then validation is not triggered i thought 'rule => 'notEmpty' will do but nothing changed so next i added 'allowEmpty' => false, but also it didn't help.
What can be done to validate this field even if required is not present
Use this-
'processing_data' => array(
'rule' => array('notEmpty'),
'required' => true,
'message' => 'Prosze zaznaczyć'
),
use required => true in model and you can true your allowEmpty validation if it is mandatory.
'processing_data' => array(
'rule' => 'notEmpty',
'required' => true,
'allowEmpty' => true,
'message' => 'Prosze zaznaczyć'
),
try this
'processing_data' => array(
'rule' => 'notEmpty',
'required' => true,
'allowEmpty' => false,
'message' => 'Prosze zaznaczyć'
),
I am using ZF2 form validation.I have to validate two fields USERNAME and PASSWORD.
everything is working fine but I am getting message like
Please enter username.
Username can not be less than 3 characters.
Please enter password.
Password can not be less than 6 characters.
If user is not entering any value then only this message should display
Please enter username.
Please enter password.
I don't want do display all the error messages on a field on failure.
Thanks in advance.
I got the answer :
In order to break the validation chain in ZF2 , we have to use
'break_chain_on_failure' => true
$this->add(
array(
'name' => 'usernmae',
'required' => true,
'filters' => array(
array('name' => 'Zend\Filter\StringTrim')
),
'validators' => array(
array('name' => 'NotEmpty',
'options' => array('encoding' => 'UTF-8',
'messages' => array(
NotEmpty::IS_EMPTY => 'Please enter username')),
'break_chain_on_failure' => true),
array(
'name' => 'Zend\Validator\StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 3,
'max' => 30,
'messages' => array(
StringLength::TOO_LONG => 'Username can not be more than 30 characters long',
StringLength::TOO_SHORT => 'Username can not be less than 3 characters.')
),
'break_chain_on_failure' => true
)
)
)
);
My Blog : http://programming-tips.in
Zend_Validate allow you to break validators chain if certain vaildation fails. The second parameter of addValidator() function $breakChainOnFailure should be TRUE in this case.
$validatorChain = new Zend_Validate();
$validatorChain->addValidator(new Zend_Validate_NotEmpty(), TRUE)
->addValidator(new Zend_Validate_StringLength(6, 12));
You can also set the 'error_message' key, for example:
'email' => [
'required' => true,
'error_message' => 'Incorrect email address ',
'filters' => [
[
'name' => 'StripTags',
],
[
'name' => 'StringToLower',
]
],
'validators' => [
[
'name' => 'EmailAddress',
'break_chain_on_failure' => true
]
]
],
After studying Miles Jones cupcake forum plugin, I have a couple of questions here:
1)Is it compulsory for each field (that appears in a model's validation rules) to be a field in a database table? I found the following validation rules in the User model of cupcake forum plugin. oldPassword and newPassword are not fields in the users table. I'm confused coz' I thought I should only make validation rules for fields of table.
public $validate = array(
'username' => array(
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'That username has already been taken',
'on' => 'create'
),
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'Please enter a username'
)
),
'password' => array(
'rule' => 'notEmpty',
'message' => 'Please enter a password'
),
'oldPassword' => array(
'rule' => array('isPassword'),
'message' => 'The old password did not match'
),
'newPassword' => array(
'isMatch' => array(
'rule' => array('isMatch', 'confirmPassword'),
'message' => 'The passwords did not match'
),
'custom' => array(
'rule' => array('custom', '/^[-_a-zA-Z0-9]+$/'),
'message' => 'Your password may only be alphanumeric'
),
'between' => array(
'rule' => array('between', 6, 20),
'message' => 'Your password must be 6-20 characters in length'
),
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'Please enter a password'
)
),
'email' => array(
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'That email has already been taken',
'on' => 'create'
),
'email' => array(
//'rule' => array('email', true),//boolean true as second parameter verifies that the host for the address is valid -- to be uncommented once website is uploaded
'rule' => array('email'),
'message' => 'Your email is invalid'
),
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'Your email is required'
)
)
);
2)Does each form field need to be a field in a database table?
For example when I ask a user to signup there will be: username, email addr, password and confirm password. But confirm password field doesn't need to be a field in a table right?
Is that a good practice?
I found the following isMatch function in form_app_model.php:
/**
* Validates two inputs against each other
* #access public
* #param array $data
* #param string $confirmField
* #return boolean
*/
public function isMatch($data, $confirmField) {
$data = array_values($data);
$var1 = $data[0];
$var2 = (isset($this->data[$this->name][$confirmField])) ? $this->data[$this->name][$confirmField] : '';
return ($var1 === $var2);
}
Can someone tell me on what is === in the last line of the above code?
Thank you.
That mean exactly equal (without type conversion) . For example: if y = 25, then y === 25 is true and y == '25' is true, but y === '25' is not true.
== means equal
=== mean identical
http://www.techsww.com/tutorials/web_development/php/tips_and_tricks/difference_between_equal_and_identical_comparison_operators_php.php
I'm using cakephp 2.0 and I'm having problem using Cake's validation thing, which works wonderful when used for it alone.
var $validate = array(
'loginname' => array(
'minCharactersRule' => array(
'rule' => array('minLength', 3),
'required' => true,
'allowEmpty' => false,
'on' => 'create',
),
'alphaNumericRule' => array(
'rule' => 'alphaNumeric',
),
'uniqueRule' => array(
'rule' => 'isUnique',
),
),
'password' => array(
'minCharactersRule' => array(
'rule' => array('minLength', 5),
'required' => true,
'allowEmpty' => false,
),
// on so on ...
Due to Internalization I put the error messages into the view like this:
echo $this->Form->input('display_name', array(
// some other options ...
'error' => array(
'betweenRule' => __('Your Dispay Name must contain at least 3 characters and should be maximal 20 characters long.'),
'uniqueRule' => __('This Display Name is already in use!'),
),
);
Now I want jQuery to validate the form on client-side as well with some little - sexy message box to return validation errors if something goes wrong.
The Ajax Request works fine but the problem here is: I only get returned the name of the validation rule, not the message.
sure: Because I didn't specified any messages in the model.
But how can I get those messages I declared in the view?
Is there any solution without having to type everything twice (PHP and JS)?
I wrote a custom validation method inside my Submission model that basically allows a blank input field, but once someone enters something in it, it'll validate the data entered.
The validation inside my Submission Model looks like this (All other validation rules are working except for 'description'):
var $validate = array(
'title' => array(
'title' => array(
'rule' => 'notEmpty',
'required' => true,
'allowEmpty' => false,
'message' => 'Please enter a title'
),
'minLength' => array(
'rule' => array('minLength', 5),
'message' => 'Please make your title longer'
),
'maxLength' => array(
'rule' => array('maxLength', 300),
'message' => 'Your title needs to be shorter'
),
),
'description' => array(
'checkDescription' => array(
'rule' => array('validateDescription'),
'message' => 'Description must be greater than 5 characters'
),
),
'source' => array(
'source' => array(
'rule' => 'notEmpty',
'required' => true,
'allowEmpty' => false,
'message' => 'Enter a valid source URL'
),
'website' => array(
'rule' => 'url',
'message' => 'Please enter a valid source URL'
),
)
);
My method which is also in my Submission model (below the above code) is:
public function validateDescription($data) {
if(empty($data['Submission']['description']))
return true;
if((strlen($data['Submission']['description'])) <= 5)
return false;
}
I'm not sure why this isn't working at all. In my view, I've got this to display the error:
if ($form->isFieldError('Submission.description'))
echo ($form->error('Submission.description', null, array('class' => 'error')));
The only reason I'm trying to do this, is because using the normal validation with required => false and allowEmpty => true along with a minLength and maxLength validation rule weren't behaving how I intended.
Any help would be greatly appreciated! :)
The $data variable passed into the validation method only contains array($fieldname => $value). You're also not returning true for strings over length of 5. Your method should look like this:
public function validateDescription(array $data) {
$value = current($data);
return !$value || strlen($value) > 5;
}