It's didn't work
array('fio', 'length', 'min'=>5, 'max'=>30, 'message' => 'custom'),
but this work
array('fio, login, password', 'required', 'message' => '{attribute} custom'),
For CStringValidator, there is another property called is which specifies the exact length of a string, and the message property is used only when is property is not satisfied by the input.
Take a look at the source, and this will become clear:
if($this->is!==null && $length!==$this->is)
{
$message=$this->message!==null?$this->message:Yii::t('yii','{attribute} is of the wrong length (should be {length} characters).');
$this->addError($object,$attribute,$message,array('{length}'=>$this->is));
}
For this type of validators:
In addition to the {#link message} property for setting a custom error
message, * CStringValidator has a couple custom error messages you
can set that correspond to different * validation scenarios. For
defining a custom message when the string is too short, * you may
use the {#link tooShort} property. Similarly with {#link tooLong}. The
messages may contain * placeholders that will be replaced with the
actual content. In addition to the "{attribute}" * placeholder,
recognized by all validators (see {#link CValidator}),
CStringValidator allows for the following * placeholders to be
specified:
Related
I'm doing the application with the Spring Integration Java DSL.
What is the best way to log to the database using the Spring Data JPA from the application?
I have a quite long integration flow with the multiple HTTP gets and posts. I want at least log the sent and responded messages and which URLs were used and possible some other custom values.
I have tried the logging with the method IntegrationFlowBuilder.log. With that my plan would be create some custom logger, which logs to the database.
I have tried the method IntegrationFlowBuilder.enrichHeaders with the method IntegrationFlowBuilder.log to log the URLSs and the other custom values. How to change some header entry inside the IntegrationFlowBuilder? I have added the entry with same key and different value, but the value in the logging doesn't change.
The HeaderEnricherSpec for the enrichHeaders() provides an option like:
/**
* Determine the default action to take when setting individual header specifications
* without an explicit 'overwrite' argument.
* #param defaultOverwrite the defaultOverwrite.
* #return the header enricher spec.
* #see HeaderEnricher#setDefaultOverwrite(boolean)
*/
public HeaderEnricherSpec defaultOverwrite(boolean defaultOverwrite) {
Also each added entry into the headers can be specified with their own override flag:
/**
* Add a single header specification where the value is a String representation of a
* SpEL {#link Expression}.
* #param name the header name.
* #param expression the expression.
* #param overwrite true to overwrite an existing header.
* #return the header enricher spec.
*/
public HeaderEnricherSpec headerExpression(String name, String expression, Boolean overwrite) {
I have some components inside binder:
binder.bind(cbClientRating, Client::getRating, Client::setRating);
binder.bind(tfFirstName, Client::getFirstName, Client::setFirstName);
binder.bind(tfLastName, Client::getLastName, Client::setLastName);
binder.bind(dfBirthDate, Client::getBirthDate, Client::setBirthDate);
According my business logic i do not need to change readonly status for one component inside binder, for example Combobox - cbClientRating when i call binder.setReadOnly(false). It should be stay in readonly mode equal true.
Now i call
cbClientRating.setReadOnly(true) after calling binder.setReadOnly(false)
binder.setReadOnly(false);
cbClientRating.setReadoOnly(true);
Is there any other solution?
Source code of Binder#setReadOnly(boolean):
/**
* Sets the read only state to the given value for all currently bound
* fields.
* <p>
* This is just a shorthand for calling setReadOnly for all currently bound
* fields. It means that fields bound after this method call won't be set
* read-only.
*
* #param fieldsReadOnly
* true to set the fields to read only, false to set them to read
* write
*/
public void setReadOnly(boolean fieldsReadOnly) {
getBindings().stream().map(BindingImpl::getField)
.forEach(field -> field.setReadOnly(fieldsReadOnly));
}
So this is only a convenience method. I recommend that you handle the field read only states completly by yourself according to your business logic.
Does Laravel validation provide any ways to fail when request contains input keys that are not defined in validation rules? Ex: Validator is instantiated with the following rules: ['name' => 'required', 'email' => 'required|email']. I want validation to fail if $request contains any other keys except name and email (Think of a user POSTing to the route end-point with undesirable data). Is that possible to achieve with simple validation rules?
P.S. I am aware of mass-assignment tricks with Eloquent, however I need to perform strict validation before any data is manipulated / persisted.
No, it's not possible to achieve with simple validation rules but would be easy to add.
All you would need to do is something like the following...
if ( count(request()->except(['name', 'email']) ) > 0) {
return false;
}
I want to validate form using asserts in Entity class but when I submit the form, it says $form->isValid() is true.
Here is how I got it setup:
// config.yml
validation: { enabled: true, enable_annotations: false }
// validation.yml
Software\Bundle\Entity\Program:
properties:
name:
- NotBlank: ~
// MyForm
...
$builder
->add('name', 'text', [
'label' => 'word.name'
])
;
...
// Program.php
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
I tried also via Annotations but it did not help.
I know I can put 'constraints' property to my form and there set new NotBlank() but I want to have that validation on Entity level since I am going to use an API and I want to have validation in one place instead of two.
Is my validation.yml file ignored or what?
EDIT
I did not mention one important thing that my form is embedded into another one. in this case you must use 'cascade_validation' property in your form options.
This answer helped me a lot Symfony2 - Validation not working for embedded Form Type
Your configuration seems correct but your yml is ignored, try to clear your cache.
Also where is your validation.yml ? It must be in a path like : src/AppBundle/Resources/config/validation.yml
In your annotation example, there is no validation constraint you must add #Assert\NotBlank on your property. Nullable = false is only for your database schema.
Symfony doc about validation : http://symfony.com/doc/current/cookbook/validation/custom_constraint.html#using-the-new-validator
Same question about using validation.yml : Symfony2 how to load validation.yml
I'm trying to get a form validated but its behavior is strange, I'm a newbie in symfony2 so I must be missing something.
I use SonataAdminBundle to create forms and CRUD controller. My ResponsableDato entity has this property:
/**
* #var string $contacto
* #Assert\NotBlank(message="Please enter your name.")
* #Assert\Length(min="3", minMessage="too short."))
* #ORM\Column(name="contacto", type="string", length=100, nullable=true)
*/
private $contacto;
If I leave contacto field blank it gives me "Complete this field" message instead of "Please enter your name". If I type one character it passes NotBlank validation but ignores Length validation.
What could I be missing? It sounds as if I had to override something to get it to work
also you're talking about html5 validation or isValid() to form_errors() case?
i'm quite sure that "Complete this field" is related to "required" option in your form field
$builder->add('contacto', 'text', array('required' => false));
set required to false and see your custom error message :)
edit:
use Symfony\Component\Validator\Constraints as Assert;
[...]
/**
* #Assert\NotNull()
*/
private $contacto;
this will fire validation error after submit, though if there's no data for this field in the form, if you need to avoid an empty string use #Assert\NotBlank()