I have field "type" that need to be unique and rule for the field will be "active" == 1.
$validator
->notEmpty('type')
->add('type', [
'unique' => [
'rule' => ['validateUnique', ['scope' => ['aktivan' => '1']]],
'provider' => 'table',
'message' => 'Not unique']
]);
I try this but doesn't work. I am new to Cakephp and I need help with this.
Related
Here my code :
$rules = [
'name' => 'required|string|max:255',
'price' => 'required|numeric|min:0',
'unit' => 'required|in:piece,kg,m',
'price_type' =>'required|string',
'service' => [
'string',
'required',
Rule::in($services_ids->all()),
],
'facility' => [
'string',
'required',
Rule::in($facilities_ids->all()),
],
'conciergeries' => [
'array',
'required',
Rule::in($conciergeries_ids->all()),
],
];
$custom_messages = [
'required' => 'Vous devez sélectionner un(e) :attribute.'
];
$validated = request()->validate($rules, $custom_messages);
The problem is that my custom_messages only works with 'name', 'price', 'unit', 'price_type' but not with 'service', 'facility' and 'conciergeries'.
Questions :
How to apply my custom messages with 'service', 'facility' and 'conciergeries' too ?
How to create a custom message for specifically one field ?
Thank's !
You just need to specify for which field you want to change the message
Try it like:-
$custom_messages = [
'service.required' => 'Your custom message for required service',
'service.string' => 'Your custom message of service should be string',];
And same process for facility and conciergeries.
I have following conditions:
1) expected request is /a1,a2,aN[/.../n1,n2,nN][?range=xxx-yyyy[&search=string]]
(square brackets contain optional parts)
2) action method signature is public function actionIndex(string $alias = '', string $range = '', string $search = ''): string
3) so I used a rule for this:
[
'pattern' => '<alias:[\\w-,\\/]+>',
'route' => 'shop/products/index',
'encodeParams' => false,
],
It works properly until I try to add pagination, LinkPager ignores a rule I wrote:
[
'pattern' => '<alias:[\\w-,\\/]+>/<page:\d+>',
'route' => 'shop/products/index',
'encodeParams' => false,
],
and displays the alias and page params as GET variables.
What is a right rule adding the page number in the end of request URI like
/a1,a2,aN/n1,n2,nN/2 and ignoring if the number is 1?
UPD: I found a reason, this is a rule I defined before:
'/shop' => 'shop/products/index', //it breaks following rules
[
'pattern' => '<alias:[\\w-,\\/]+>/<page:\d+>',
'route' => 'shop/products/index',
'encodeParams' => false,
],
[
'pattern' => '<alias:[\\w-,\\/]+>',
'route' => 'shop/products/index',
'encodeParams' => false,
],
So, how I to make all these rules work together?
Solution 1: To make another action method which works without alias argument and calls actionIndex with empty one.
Solution 2: To make same rules with different mode in special order:
[
'name' => 'This rule is first when we create a link',
'pattern' => '<alias:[\\w-,\\/]+>/<page:\d+>',
'route' => 'shop/products/index',
'encodeParams' => false,
'mode' => \yii\web\UrlRule::CREATION_ONLY,
],
[
'name' => 'This rule is first when we parse a request',
//
'pattern' => 'shop/<page:\d+>',
'route' => 'shop/products/index',
],
[
'name' => 'Used for parsing when previous rule does not match',
'pattern' => '<alias:[\\w-,\\/]+>/<page:\d+>',
'route' => 'shop/products/index',
'encodeParams' => false,
'mode' => \yii\web\UrlRule::PARSING_ONLY,
],
[
'name' => 'Same as first but when link has no page number',
'pattern' => '<alias:[\\w-,\\/]+>',
'route' => 'shop/products/index',
'encodeParams' => false,
'mode' => \yii\web\UrlRule::CREATION_ONLY,
],
[
'name' => 'First when parsing request with no page number',
'pattern' => 'shop',
'route' => 'shop/products/index',
],
[
'name' => 'Used for parsing when previous rule does not match',
'pattern' => '<alias:[\\w-,\\/]+>',
'route' => 'shop/products/index',
'encodeParams' => false,
'mode' => \yii\web\UrlRule::PARSING_ONLY,
],
If you know a better solution with one action I'll be glad to see it.
how can I add an virtual column to the TCA (TYPO3 8)? I have in a 1:n table with data and I want to display the count of the data in the backend to current element.
I need something like this:
$fields = [
'counts7d' => [
'exclude' => false,
'label' => 'last 7 days',
'config' => [
'type' => 'none',
'procFunc' => '\Namespace\MyClass->MyMethod',
'readOnly' => true,
'params' => [
'period => '7d'
]
]
],
'counts30d' => [
'exclude' => false,
'label' => 'last 30 days',
'config' => [
'type' => 'none',
'procFunc' => '\Namespace\MyClass->MyMethod',
'readOnly' => true,
'params' => [
'period => '30d'
]
]
],
];
pseudo function:
public function myMethod($element, $params){
$sql = "SELECT count(*) FROM TABLE WHERE pid=$element[uid] and date > $params[period]";
return sql_count…
}
The field should only be informative for the backend users.
Does anyone have an idea?
Thanks
Oliver
The TCA field type user is exactly what you are looking for:
'counts7d' => [
'exclude' => false,
'label' => 'last 7 days',
'config' => [
'type' => 'user',
'userFunc' => \Namespace\MyClass::class . '->MyMethod',
'parameters' => [
'period => '7d',
],
],
],
The TCA field type none is exactly what you are looking for. Type none is the only type that does not necessarily need a database field. To manipulate it you can use userFunc which allow you to use custom php function.
I am getting the error:
The input was not found in the haystack
After form post. Please see the selection tag code lines below:
// Add "roles" field
$this->add([
'type' => 'select',
'name' => 'roles',
'attributes' => [
'multiple' => 'multiple',
'options'=>$this->role_desc,
'inarrayvalidator' => false,
'class'=>'form-control'
],
'options' => [
'label' => 'Role(s)',
],
]);
// Add input for "roles" field
$inputFilter->add([
'class' => ArrayInput::class,
'name' => 'roles',
'required' => true,
'haystack'=>$this->role_ids,
'filters' => [
['name' => 'ToInt'],
],
'validators' => [
['name'=>'GreaterThan', 'options'=>['min'=>1]],
['name'=>'InArray', 'options'=>['haystack'=>$this-
>role_ids]]
],
]);
The InArray seems to be validating well, lm just no sure what is bringing up the exception. Thank you in advance.
Actually , your issue is similar to link
To solve this, change your validators definition to:
'validators' => [
['name'=>'GreaterThan', 'options'=>['min'=>1]],
[
'name' => 'Explode',
'options' => array(
'validator' => [
'name'=>'InArray',
'options'=> [
'haystack'=>$this->role_ids
]
]
)
]
],
Unfortunately, I do not think there is a "cleaner" way to do this.
Alternately, Maybe you could use the MultiCheckbox.
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
]
]
],