Zend Form Validation:: Does exist oposite validator to Identical?[how to check that input not identical to 'str'] - zend-framework

Zend Form Validation: Is there a validator that is the opposite of Identical (i.e., notIdentical)?
How would I check that an input is not identical to 'str'?

AFAIK there is not something like NotIdentical. Have you tried your own validator in that way?:
class My_Validate_NotIdentical extends Zend_Validate_Identical
{
public function isValid($value)
{
return !parent::isValid($value);
}
}
It just simplest solution - you should also change validation messages etc.

Related

Eclipse Scout go through fields in form data

I would like to go through all fields in form data.
I know that in form I could do something like this :
// Go through all fields with IFormFieldVisitor
box.visitFields(new IFormFieldVisitor() {
#Override
public boolean visitField(IFormField field, int level, int fieldIndex) {
if (field instanceof MyClass) {
...
}
return true;
}
}, 0);
but form data doesn't have this options. How to do this in form data.
You can obtain them using
AbstractFormData.getFields to obtain the top-level fields. If you need nested fields as well, have a look at the more complex AbstractFormData.getAllFieldsRec().
AbstractFormData.getAllProperties to obtain properties that you have defined by annotating the getters and setters with #FormData
That was the simple case.
Now, if you are using the Scout Extension mechanism to add new elements to an existing form (and it's formdata), you will have to take those contributions into account.
If you need to do this, you can refer to the source code of the AbstractForm.importFormData to see how Scout implements this.

Conditional field validation that depends on another field

I need to change the validation of some field in a form. The validator is configured via a quite large yml file. I wonder if there is any way to do validation on two fields at once.
In my case I have two fields that cannot be both empty. At least one has to be filled.
Unfortunately till now I just could see that the validation are defined on a per-field basis, not on multiple fields together.
The question is: is it possible in the standard yml configurations to perform the aforementioned validation?
thanks!
I suggest you to look at Custom validator, especially Class Constraint Validator.
I won't copy paste the whole code, just the parts which you will have to change.
Extends the Constraint class.
src/Acme/DemoBundle/Validator/Constraints/CheckTwoFields.php
<?php
namespace Acme\DemoBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* #Annotation
*/
class CheckTwoFields extends Constraint
{
public $message = 'You must fill the foo or bar field.';
public function validatedBy()
{
return 'CheckTwoFieldsValidator';
}
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
}
Define the validator by extending the ConstraintValidator class, foo and bar are the 2 fields you want to check:
src/Acme/DemoBundle/Validator/Constraints/CheckTwoFieldsValidator.php
namespace Acme\DemoBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class CheckTwoFieldsValidator extends ConstraintValidator
{
public function validate($protocol, Constraint $constraint)
{
if ((empty($protocol->getFoo())) && (empty($protocol->getBar()))) {
$this->context->addViolationAt('foo', $constraint->message, array(), null);
}
}
}
Use the validator:
src/Acme/DemoBundle/Resources/config/validation.yml
Acme\DemoBundle\Entity\AcmeEntity:
constraints:
- Acme\DemoBundle\Validator\Constraints\CheckTwoFields: ~

Symfony 1.4: Check if form has errors inside form class

Is there a simple way in Symfony 1.4 to know whether a submitted form had any errors inside the form class? I'm familiar with the $form['some_field']->hasErrors() for templates but in this case I'd like to run a post-validator only if the form didn't have any errors with the standard validators. I'm basically after something like:
public function configure() {
// widgets
// standard validators
if (!this->hasErrors()) {
// run post-validator
}
}
The API documentation is as cryptic as usual. Thanks in advance.
Since the validation is perfom on the bind call, I don't see other place to post validate on error than in the bind function. So, in your form class:
public function bind(array $taintedValues = null, array $taintedFiles = null)
{
parent::bind($taintedValues, $taintedFiles);
if ($this->hasErrors())
{
// do post validate
// you can access values from your form using $taintedValues
}
}
But you will have to manually call the validator instead of just define a new one (since the bind process has already been done).

How should Zend_Validator_StringLength extended?

How should Zend_Validator_StringLength extended?
MyValidator:
class Zend_Validate_StringLengthNoTags extends Zend_Validate_StringLength {
public function __construct($options = array()) {
parent::__construct($options);
}
public function isValid($value) {
return parent::isValid(trim(strip_tags($value)));
}
}
Use of validator - bug not assign values to $this->_min, $this->_max:
$text->addValidator(new Zend_Validator_StringLengthNoTags(array('max'=>4,'min'=>2)));
Edit:
root of bug: $this->_min==1, $this->_max==null ,
but it should be
$this->_min==2, $this->_max==4
Update: the answer:
This was internal application system problem that generate this bug, it fixed soo the code above working. Thanks for all peaple they try to help me.
Thanks
It seems that your Zend_Validate_StringLengthNoTags could be replaced simply by filters StripTags and StringTrim and standard Zend_Validate_StringLength validator.
For example, I think that the following would work the same as your validator:
$text->setFilters(array('stripTags','stringTrim'));
$text->addValidator(new Zend_Validator_StringLength(array('max'=>4,'min'=>2)));
The reason is that validation in Zend Framework is performed after filtering, i.e. on filtered data.
Returning to your Zend_Validate_StringLengthNoTags code. The code looks ok and I cannot spote a problem with it.

Zend_Form using subforms getValues() problem

I am building a form in Zend Framework 1.9 using subforms as well as Zend_JQuery being enabled on those forms. The form itself is fine and all the error checking etc is working as normal. But the issue I am having is that when I'm trying to retrieve the values in my controller, I'm receiving just the form entry for the last subform e.g.
My master form class (abbreviated for speed):
Master_Form extends Zend_Form
{
public function init()
{
ZendX_JQuery::enableForm($this);
$this->setAction('actioninhere')
...
->setAttrib('id', 'mainForm')
$sub_one = new Form_One();
$sub_one->setDecorators(... in here I add the jQuery as per the docs);
$this->addSubForm($sub_one, 'form-one');
$sub_two = new Form_Two();
$sub_two->setDecorators(... in here I add the jQuery as per the docs);
$this->addSubForm($sub_two, 'form-two');
}
}
So that all works as it should in the display and when I submit without filling in the required values, the correct errors are returned. However, in my controller I have this:
class My_Controller extends Zend_Controller_Action
{
public function createAction()
{
$request = $this->getRequest();
$form = new Master_Form();
if ($request->isPost()) {
if ($form->isValid($request->getPost()) {
// This is where I am having the problems
print_r($form->getValues());
}
}
}
}
When I submit this and it gets past isValid(), the $form->getValues() is only returning the elements from the second subform, not the entire form.
I recently ran into this problem. It seems to me that getValues is using array_merge, instead of array_merge_recursive, which does render to correct results. I submitted a bug report, but have not gotten any feedback on it yet.
I submitted a bug report (http://framework.zend.com/issues/browse/ZF-8078). Perhaps you want to vote on it?
I think that perhaps I must have been misunderstanding the way that the subforms work in Zend, and the code below helps me achieve what I wanted. None of my elements share names across subforms, but I guess this is why Zend_Form works this way.
In my controller I now have:
if($request->isPost()) {
if ($form->isValid($request->getPost()) {
$all_form_details = array();
foreach ($form->getSubForms() as $subform) {
$all_form_details = array_merge($all_form_details, $subform->getValues());
}
// Now I have one nice and tidy array to pass to my model. I know this
// could also be seen as model logic for a skinnier controller, but
// this is just to demonstrate it working.
print_r($all_form_details);
}
}
I have a same problem to get value from subforms I solve it with this but not my desire one
code:
in controller i get value with this code that 'rolesSubform' is my subform name
$this->_request->getParam ( 'rolesSubform' );
Encountered the same problem. Used post instead of getValues.
$post = $this->getRequest()->getPost();
There are times when getValues does not return the same values returned by $post.
Must be a getValues() bug.