How to filter zf2 forms? - forms

I'm trying to add filters for my form elements
$this->add(array(
'name' => 'name',
'attributes' => array(
'type' => 'text',
'required' => true,
),
'options' => array(
'label' => 'Name',
),
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags'),
)
));
but I still can add some tags in this element, what am I doing wrong?

Create two classes one for form another for filter and set filter for your form.
$form = new Form\CommentForm();
$form->setInputFilter(new Form\CommentFilter());
return $form;
for more info see https://github.com/nsenkevich/comment/tree/master/Comment

Related

How do I limit number of rows in the admin form in magento

I have following code in my Form.php
$fieldset->addField('name', 'textarea', array(
'label' => Mage::helper('module')->__('Name'),
'class' => 'required-entry',
'required' => true,
'name' => 'name',
));
I want to restrict the number of rows to display it like a single row, the code: 'rows' => 1 doesn't work here.
You can use styling to do this
$fieldset->addField('name', 'textarea', array(
'label' => Mage::helper('module')->__('Name'),
'class' => 'required-entry',
'required' => true,
'name' => 'name',
'style' => "height: 1em;",
));
Just had to change 'textarea' to 'text'

Add custom form in magento admin side

I want to make custom form in magento with two section like (two green box)
Also I wand to make three fields instead of one like
I have make code like
protected function _prepareForm() {
$form = new Varien_Data_Form();
$this->setForm($form);
$fieldset = $form->addFieldset('slider_form', array('legend' => Mage::helper('slider')->__('Slider information')));
$fieldset->addField('link_title', 'text', array(
'label' => Mage::helper('slider')->__('Link Title'),
'class' => 'required-entry',
'required' => true,
'name' => 'link_title',
));
$fieldset->addField('link', 'text', array(
'label' => Mage::helper('slider')->__('Link'),
'class' => 'required-entry',
'required' => true,
'name' => 'link',
));
$fieldset->addField('content', 'text', array(
'label' => Mage::helper('slider')->__('Text'),
'class' => 'required-entry',
'required' => true,
'name' => 'content',
));
....
....
}
Can anybody help!!!

How can I render label to the right of the element?

I am adding a consent checkbox to an existing form. I am not able to render the label to the right of the checkbox. What am I doing wrong?
Please note that the check box has created using $this->addElement( because the rest of the form was created this way.
I thank you in advance.
$this->addElement('checkbox', 'isUserConsent', array(
'options' => array(array('placement' => 'APPEND')),
'label' => 'Plz activate',
'validators' => array(
array('InArray', false, array(
'hay' => array(1),
'messages' => 'Please check the consent box'),
)
),
'decorators' => array('ViewHelper','Errors','Label'),
));
The default is to prepend the label, but you can change this by modifying the decorator's 'placement' option:
$this->getElement('isUserConsent')->getDecorator('label')->setOption('placement', 'append');
Edit: I never use this syntax for decorators but it should be something like this:
$this->addElement('checkbox', 'isUserConsent', array(
'options' => array(array('placement' => 'APPEND')),
'label' => 'Plz activate',
'validators' => array(
array('InArray', false, array(
'hay' => array(1),
'messages' => 'Please check the consent box'),
)
),
'decorators' => array(
'ViewHelper',
'Errors',
'Label' => array(
'placement' => 'append'
)
),
));
This is the code for rendering lable of a form element.
$this->form->name->renderLabel() ;

Codeigniter form validation using set of rules

I'm trying to create a set of Form validation rules in my Codeigniter project, so that when the validation of the first set fails, the second validation set should not run.
I found this in the CI manual:
$config = array(
'signup' => array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'required'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'required'
),
array(
'field' => 'passconf',
'label' => 'PasswordConfirmation',
'rules' => 'required'
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'required'
)
),
'email' => array(
array(
'field' => 'emailaddress',
'label' => 'EmailAddress',
'rules' => 'required|valid_email'
),
array(
'field' => 'name',
'label' => 'Name',
'rules' => 'required|alpha'
),
array(
'field' => 'title',
'label' => 'Title',
'rules' => 'required'
),
array(
'field' => 'message',
'label' => 'MessageBody',
'rules' => 'required'
)
)
);
$this->form_validation->set_rules($config);
I know that I can now run the validation of each set separately ($this->form_validation->run('signup') and $this->form_validation->run('email') in this case).
The problem is, that when I use the $config array, errors do not get added to the Form validation class (and thus does not show up) while the form post failed. It did add and show errors when I did not use the set of rules, but just the $this->form_validation->set_rules() method.
What did I do wrong that no error messages are added when entering invalid form data while using a set of rules?
The $config array needs to be in a file called form_validation.php in the application/config directory. It is then loaded automatically when CI is loaded, and passed to the Form validation object when it is created.
The first time the run() method of the FV object is called, if no rules have been set in the FV object, it looks up the config rules it was given on creation, and uses the group indexed by the name passed as argument to run(). All later calls to run() in the same invocation of the CI application, even with different group names, will bypass this check since the rules will now have been set - i.e., you only get to set the rule group once in an invocation.
So you won't be able to create two groups of rules and then call one after the other. You can either call one OR the other.
It might be better to cascade your rules using set_rule() - i.e., add some rules using set_rule(), then validate against them. if they pass, add some more rules and retry validation. You effectively repeat the old rules, knowing they will pass, but that means any failures will be the result of the new rules.
Try array_merge in the form_validation array.
Here if you want two array to combint and gat join validation error. You can use this
$config["form"] = array_merge($config['signup'], $config['email']);
Hope this help.
If someone is facing the same problem try this:
if ($this->form_validation->run('signup') === FALSE) { /* return your errors */ }
$this->form_validation->reset_validation();
$this->form_validation->set_data($data);
if ($this->form_validation->run('email') === FALSE) { /* return your errors */ }
// Do your stuff
You need to reset after each validation to change the rules. You can also use:
$this->form_validation->set_rules($validation_rules);
Note: Set data first and then set rules, it doesn't work the other way around!
hey alwin u need to run the form_validation rules before submitting the form
....
$config = array(
'signup' => array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'required'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'required'
),
array(
'field' => 'passconf',
'label' => 'PasswordConfirmation',
'rules' => 'required'
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'required'
)
),
'email' => array(
array(
'field' => 'emailaddress',
'label' => 'EmailAddress',
'rules' => 'required|valid_email'
),
array(
'field' => 'name',
'label' => 'Name',
'rules' => 'required|alpha'
),
array(
'field' => 'title',
'label' => 'Title',
'rules' => 'required'
),
array(
'field' => 'message',
'label' => 'MessageBody',
'rules' => 'required'
)
)
);
$this->form_validation->set_rules($config);
///u have to chek form validation getting validate or not
//enter code here
if ($this->form_validation->run() === FALSE) {
$this->load->view('your_view');
} else {$this->ur_controller->method_name();
$this->load->view('whatever ur view');
}
}

Zend framework multiple Validators in an array

I want to create a form in Zend framework. I am using the code below for a field:
$this->addElement('text', 'username', array(
'label' => 'Username:',
'required' => true,
'filters' => array('StringTrim'),
'validators' => array(
'alnum'
)
));
This works. But now I also want to add a new validator. In this case StrinLength
$element->addValidator('StringLength', false, array(6, 20));
How can I add this validator in the array I already have? Tnx in advanced
Doesn't this work:
<?PHP
$this->addElement('text', 'username', array(
'label' => 'Username:',
'required' => true,
'filters' => array('StringTrim'),
'validators' => array(
'alnum',
array('StringLength', false, array(6,20))
)
));
Similar to the example given in the manual
You can specify the names of arguments to the addValidator() method as array keys:
$this->addElement('text', 'username', array(
'label' => 'Username:',
'required' => true,
'filters' => array('StringTrim'),
'validators' => array(
'alnum',
// See below
array(
'validator' => 'StringLength',
'options' => array(6, 20)
)
)
));