How to disable validation from action in symfony? - php

Can anyone tell me how can we disable validation from action? Actually, I wanted to disable validation dynamically based on certain condition from action. I don't know how can we do that? Please help me.
My For validator as below:
$this->setValidators(
array(
'search_text' => new sfValidatorString(
array('required'=>true),
array('required' => 'Please enter keyword')),
'field_type' => new sfValidatorString(
array('required'=>true),
array('required' => 'Please select an option')),
)
);
I want to disable above validation in action dynamically.
Please help me.

You can create an enable_validation (or whatever name you want) option for your form. The form constructor accepts a $options array. In your action, you pass an array which would look like this : array('enable_validation option' => false)
Then, in your form, use the getOption() method to retrieve this option and set the validators accordingly.

Related

Upon validation laravel sends the column name to the user

I have the following validation code in a controller. The problem is that laravel is sending the column name in the validation error , which is most of the time abbreviations. In this case the column name is "ans". Is there a way to show a different name to the user ?
Validation :
$this->validate($request, [
'ans' => 'required|max:5000|min:10'
]);
Error :
The ans field is required.
If you want to customize the attribute name you can do that; you don't have to customize the message to do this. The validate method takes an array of "custom attribute names" to use (4th argument), just like it takes an array of custom messages (3rd argument).
$this->validate(
$request,
['ans' => 'required|max:5000|min:10'], // rules
[], // custom messages
['ans' => 'Answer'] // custom attribute names
);
You should always have the option to specify custom messages and attributes with the validation methods that are available. These custom attribute names get replaced in the messages where :attribute is used.
If you don't do it this way you would have to create 3 custom messages, one for each rule you have defined to use for your field 'ans', since any of those rules could fail and will include the attribute name.
Yes, you can achieve with the following code.
$this->validate($request, [
'ans' => 'required|max:5000|min:10'
], [
'ans.required' => 'The answer field is required.'
]);
Take a look at the laravel documentation to know more.
You can send custom validation message,define all
your validation message in a variable like this
$messages = [
'ans.required' => 'The answer field is required.'
]
$this->validate($request, [
'ans' => 'required|max:5000|min:10'
], $messages);
Thanks

Zend2 InputFilter: not required as default setting

I wrote an input filter for a Form by inheriting from InputFilter (class CustomInputFilter extends Zend\InputFilter\InputFilter).
By default, each form element is required. This means that I have to set required to False explicitly for all optional elements. How can I change the InputFilter such that all form elements are optional by default?
I use the Zend 2 framework.
I am not sure about it is possible the way you want using InputFilter. I think this implies filtering and validating data. If any input field does not need to be required, then ZF have had the option ('required' => false). See another approach
$inputFilter->add(array(
'name' => 'title',
'required' => true,
'allow_empty' => true,
));
So why is this? This is something that input field must exist but you can keep it empty. If you then put invalid data in it can then validate data too.

Validate choice list in Symfony2

I have a choice list where user can choose one value, but there I even set an empty value if the user doesn't select anything.
The form does not have model, to use #Assert annotation with it, and the choice field is optional, so in some case it will be hidden and need to be validated only if showed to user.
How I can validate this field? When I set it to required in my form type it didn't help (If I am right required equal to true by defaut). Where is my problem?
You need to add the NotBlank validator to your field.
You can add a validator directly to your field, like this:
$this->createFormBuilder()
->add('exampleField', 'choice', array(
'label' => 'Label',
'constraints' => array(
new NotBlank(),
),
))
[...]

NotEqualTo Validation in Zend Framework 2

Is there a NotEqualTo validator in Zend Framework 2? If not; how can we implement a NotEqualTo validator in Zend Framework 2? Or is there any other workarounds?
Example
I have a text field having the following attributes
'onfocus'=>'if(value==\'Name\'){ value=\'\'}'
'onblur'=>'if(value==\'\'){ value=\'Name\'}'
When the user submits the form without entering their name; I have to validate whether the field is having a value of 'Name' and if it has; return an error.
Note:
I can't use the placeholder attribute as my client require the placeholder text to disappear on clicking the text field(currently it disappears only when we begin typing).
I have made a simple workaround using the Callback validator as follows.
'validators' => array(
array(
'name' => 'Zend\Validator\Callback',
'options' => array(
'callback'=>function($value){
return $value!='Name';
}),
),
),
),
Details of the Callback Validator can be seen at the following link
http://framework.zend.com/manual/2.0/en/modules/zend.validator.set.html#callback

Simple Registration on Symfony 2 wrong action

I used the Symfony 2 documentation to create a simple registration.
But now i have two little problems. The password fields, which I created with the Form Builder, be time in plain text.
The second problem is that the action of the form is not used the correct route. When I press the submit button, I get the standard page.
Has anyone of you an idea, which may be related?
If you still parts of the code needed, I like to add these.
Greetings
did you set it to a password type? eg
$builder->add('password', 'password)
Or can do repeated field
$builder->add('password', 'repeated', array(
'type' => 'password',
));
When you build the form do you set action?
$form = $this->createForm(new Type(), $type, array(
'action' => $this->generateUrl('your_route'),
));

Categories