Is it possible to use Zend_Filter_Input as a generic input filter? I want to use it to filter all form fields (strip tags etc but no validation). All the examples seem to include a $validators array and pre-suppose that I will know the names of the fields on the way in.
Because of the nature of the project, timescales etc, it is not possible to rewrite the forms using Zend_Form. There is a generic Form class which handles all form input so I need to do the filtering in there.
Thanks!
Luke.
You can simply pass an empty array for the $validators argument to skip validation and simply use filtering.
Are you saying that you don't know the field names you'll pass into the Zend_Filter_Input instance? You can use the wildcard *-field to apply a filter to all input fields. Is this what you're asking for?
$input = new Zend_Filter_Input(array(
'*' => 'StripTags'
), array(), $data);
will filter all values in $data with the Zend_Filter_StripTags filter.
EDIT:
Retrieve the values with
$escaped = $input->getEscaped(); // will be automatically run through an HTML-entities-filter
// or
$unescaped = $input->getUnescaped(); // the values as they come out of the filter-chain.
Related
I am making an application where users can upload questions, and questions can have multiple correct answers. The correct answers have names of the form correctAnswer1 correctAnswer2 etc.
I want to know how to require all submitted fields matching this pattern; I was thinking of using something analogous to
/correctAnswer[0-9]/ => 'required'
I'm not sure of the logic behind your requirement, kinda seems you should do things in a different manner, but again I do not know how your app works so I can't be a judge of that. So if the user can add new correct answers fields on the form, and you wan't them to not be empty it makes some sense.
You can't have a regex in the rule name but you can do the following:
$rules = [
// your other rules
];
$correctAnswers = preg_grep( '/^correctAnswer[1-9]{1}$/', array_keys($this->all()));
// use $this->all() when in Http\Requests\YourRequest
// if you are not using the request method of validation (you validate in controller)
// simply replace $this->all() with $request->all() or Input::all().
foreach ($correctAnswers as $correctAnswer) {
$rules[$correctAnswer] = 'required';
}
return $rules;
This assumes you are using the Laravel 5, Http\Requests to validate your input. If you are doin'g the validation elsewhere (in controller for example), just replace $this->all() with $request->all() or Input::all(). I can't give the exact choice as I do not know exactly how you do the validation and what version of laravel you use.
PS: This will match only correctAnswer1 to correctAnswer9. If you want more just play with the [0-9]{1} part of the regex.
Let say I have an HTML form with bunch of fields. Some fields belong to Product, some to Order, some to Other. When the form is submitted I want to take that request and then create Symfony forms for Product, Order, and Other in controller. Then I want to take partial form data and bind it with appropriate forms. An example would something like this:
$productArray = array('name'=>$request->get('name'));
$pf = $this->createForm(new \MyBundle\Form\ProductType(), $product);
$pf->bind($productArray);
if($pf->isValid()) {
// submit product data
}
// Do same for Order (but use order data)
// Do same for Other (but use other data)
The thing is when I try to do it, I can't get $form->isValid() method working. It seems that bind() step fails. I have a suspicion that it might have to do with the form token, but I not sure how to fix it. Again, I build my own HTML form in a view (I did not use form_widget(), cause of all complications it would require to merge bunch of FormTypes into one somehow). I just want a very simple way to use basic HTML form together with Symfony form feature set.
Can anyone tell me is this even possible with Symfony and how do I go about doing it?
You need to disable CSRF token to manually bind data.
To do this you can pass the csrf_protection option when creating form object.
Like this:
$pf = $this->createForm(new \MyBundle\Form\ProductType(), $product, array(
'csrf_protection' => false
));
I feel like you might need a form that embed the other forms:
// Main form
$builder
->add('product', new ProductType)
->add('order', new OrderType);
and have an object that contains association to these other objects to which you bind to the request. Like so you just have to bind one object with the request and access embedded object via simple getters.
Am I clear enough?
Here is an example of setting up the validators as explained in the Manual http://zendframework.com/manual/en/zend.filter.input.html
$validators = array('account' => 'Alpha');
This checks that the input field is on Alphabets.
The question is, how do i find out the other options that can be used for Validating apart from "Alpha"
Where can I find the other Validation constants?
The page you are looking at is the manual page for Zend_Filter, you'd be better reading the Zend_Validate page.
If you look at this manual page, you will see a list of standard validator classes. You can use any of these as an option in $validators. You just use the name of the validator as the option.
For example to use the Date validator you would do this:-
$validators = array('account' => 'Date');
You can also find a list of validators if you look in your library/Zend/Validate folder.
Using CakePHP 1.3
I have a fairly large model in CakePHP, and I'd like to have some hidden elements on the form page to (manually) compare/validate against before saving, but when doing a saveAll() (with validation), I don't want these fields present (essentially to avoid them being updated).
What's the proper way to handle this? Remove them from $this->data before handing that to saveAll()?
Use the 'fieldlist' option:
$this->Model->saveAll($data, array('fieldlist' => array('fields', 'to', 'save')));
$fields = array_keys($this->Model->_schema);
$fieldsNotToSave = array('field1', 'field2');
$fieldsToSave = array_diff($fields, $fieldsNotToSave);
I'll usually use unset() prior to the saveAll(). If you think about it, it's the smarest/easiest way. That is, unless you want to manually name the hidden input fields different than the default data[Model][field] that is generated by the form helper.
But then you'd have to access them manually and validate them manually.
unset() is fast and clear.
Is there any way to output all filtered data from the class Zend_Filter_Input?
Zend_Filter_Input offers numerous methods for retrieving filtered and validated data.
First, you can retrieve an associative array of all fields:
$data = $input->getEscaped(); // Retrieve all data, escaped with Zend_Filter_HtmlEntities
$data = $input->getUnescaped(); // Retrieve all data, not escaped.
You can also get an associative array of certain segments of you data, the method names are very clear:
$invalidFields = $input->getInvalid(); // Fields that failed validation
$missingFields = $input->getMissing(); // Fields that were declared as 'required' using the 'presence' metacommand, but not in the input
$unknownFields = $input->getUnknown(); // Fields that were not declared in the validator rules, but were present in the input.
On top of all that, Zend_Filter_Input offers an object accessor, through an implementation of the __get magic method:
$oneField = $input->oneFieldName
In form you can get unfiltered values. Check the manual ;)