Only Comma separated values in textbox Symfony2 - validation

I have a text box on my form. I want only a comma separated keyword in that. How to put this type of validation in symfony2.
I found
http://symfony.com/doc/current/reference/constraints.html
and
http://symfony.com/legacy/doc/forms/1_2/en/02-form-validation
but not as I required.

well the validation logic is as simple as
if(is_array(explode(",",$input)){
return true;
}else{
return false;
}
use a custom validator and implement this logic

You should write your custom validator for check this requirements, then you can implements a data transformer so you can manipulate your string as you want. As example, if you are archiving something like a tagging field you can proceed as described.
In the client side you can add a js component that can do this behaviour for you, check this component in the Tagging support section (you can build a REST service for manage a subset of filed).
Let me know if this is your scenario so i can provide example code about.
Hope this help.

Related

Symfony2 validation filters

In my Symfony 2 application I need to filter input before passing it on to validation [1], however, I can't seem to find any system within Symfony to do this.
The type of filtering I looking for is e.g. to be able to filter a dash out of a specific field before validating it. E.g. users can enter 123-123 but the only accepted value is 123123. Just as I can set up validation rules with constraints, I'm looking for something similar for filters.
[1] http://symfony.com/doc/current/book/validation.html
Nifr's answer is good but is missing of an important alternative that, if I understand correctly your question, seems to fit perfectly your needs.
You can use a hook that is pretty much an event listener: if something happens or is going to happen, it intercepts the event and redirect it to your function.
In this case, you need a PRE_BIND hook (is deprecated since 2.3 version, now it's called PRE_SUBMIT)
Read this if you need help about
Either write your own Validation Assert to filter and then proxy the other validators for this purpose ...
... or one or multiple Regex Asserts.
... or use a DataTransformer to transform/filter the input.
With the DataTransformer involved you could aswell consider creating a new FieldType which renders two inputs with a seperator like the date form-field does. ( if not not used with widget => single_text )

Get list of Assert Constraints in FormType Extension in Symfony2.1

I'm trying to implement client side form validation with javascript and I'd like to use the Validation defined in the Entity or the Form by putting them in the fields data- property.
The problem is that I don't know nor find any information about how to get the constraints from within the FormType extension.
I thought that the FormBuilder should have access to the data as it is used to start validation, but there doesn't seem to be any method to retrieve constraints.
Thanks for any help
You can use:
$validators = $form->getConfig()->getValidators();
on your form object
Finally I found a solution. It may not be the best, but serves for me.
I injected the Validator service into the FormTypeExtension.
Then I could do
$this->validator->getMetadataFactory()->getClassMetadata($options['data_class']);
I also had to change the service alias from "field" to "form" to get the data_class in options.
Hope this helps someone having the same problem.
If needed I can publish more details.

How do you validate an archetype field that is not required, but if you give it, you should validate?

Products.validation has some validators (like regex) that I would like to use in some non required fields.
So, the use case is: The validation is required, but only if the field is not empty.
How do you do it? I know I can create a custom validator, check the REQUEST and do all the magic, but this seens wrong to me: I should avoid creating custom code since I should just register the RegexValidators from Products.validation and use it. I tried to understand V_SUFFICIENT and register a regex validator using it, but isn't applicable to my situation.
I couldn't find anything on the internet, on mailing lists, on nabble, etc. So, how do you validate your fields that aren't required, but if the user provides something, you then need to validate?
I need this in a Plone 3.3.5 instance.
You can use required = False and your desired validators:
http://plone.org/documentation/manual/developer-manual/archetypes/fields/fields-reference#common-field-attributes

Validate single form field only in Symfony2

I'm looking for a way to validate just a single field (object property) against the constraints specified in the annotations of a particular entity.
The goal is to send an AJAX request after the "onBlur" event of a form field, asking the server to validate this single field only, and - depending on the response - add a small "OK" image next to this field or an error message.
I don't want to validate the whole entity.
I wonder what's the best approach for this problem? Thanks for any tips.
The Validator class has the validateProperty method. You can use it like this:
$violations = $this->get('validator')->validateProperty($entity, 'propertyName');
if (count($violations)) {
// the property value is not valid
}
Or, if the value is not set in the entity, you can use the validatePropertyValue method:
$violations = $this->get('validator')->validatePropertyValue($entity, 'propertyName', $propertyValue);
if (count($violations)) {
// the property value is not valid
}
Have a look at validation groups. I think this is what you need. You could add a group "ajax" or and just adding the one constraint to it. Then tell the validator to use that group. THe symfony2 docs have an example included.

Drupal: Custom Content Type validation

I've created a custom content type with CCK.
If I need to add some custom code for validating fields of this content type's record form, where do I add the code and which functions are best for this task?
The easiest way would probably be hook_form_alter() and the #validation attribute on the form. You would of cause have to implement this in your own module.
The form api is what you use to validate, you'll be crafting your own validation function. I'm going to assume you are using D6
There's a less painful way:
http://drupal.org/project/validation_api
This module lets you make php code or regex for any given field.
Hope this helps.
To create your own module to implement form validation I suggest this method:
create a new module for content type field validation in drupal

Resources