How can I render label to the right of the element? - zend-framework

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() ;

Related

zend form client side validation

I am trying to validate a form created by zend form library. It is validating the form properly, but when I press tab button then its validating all the fields immediately. But it should be validating after press the tab button or press the final submit button.
My code is like:
$this->addElement('text', 'email', array(
'label' => 'Email:',
'required' => true,
'class' => 'span12',
'attribs' => array(
'required' => true,
'pattern'=> "^[A-Za-z0-9._]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$"
)
));
$this->addElement('text', 'name', array(
'label' => 'Name:',
'required' => true,
'class' => 'span12',
'attribs' => array(
'required' => true,
'pattern' => '[a-zA-Z]{4,}'
)
));
$this->addElement('text', 'phone', array(
'label' => 'Phone:',
'required' => true,
'class' => 'span12',
'attribs' => array(
'required' => true,
'pattern' => '\d{4,}'
)
));
Please find the image.
Pressing tab after writing email address, showing red box for name and phone number.

Div Hide/Show with radio button click using Zend Form

Here is my code:
class File_Form_AddFile extends Custom_Form {
public function init() {
$translate = Zend_Registry::get('translate');
$this->setTranslator($translate);
$this->setName("addfile");
$this->setMethod('post');
$this->addElement('text', 'title', array(
'filters' => array('StringTrim'),
'validators' => array(
array('StringLength', false, array(0, 50)),
),
'required' => true,
'label' => __('Title') . ':',
));
$this->addElement('radio', 'type', array(
'label'=>__('Applicant Type'),
'multiOptions' => array(
'office' => 'Office',
'community' => 'Community',
'person' => 'Person',
),
'required' => true,
'separator' => '',
'value' => 'office'
));
**// I want this section to show only after 'community' is clicked at above input field.**
$this->addElement('radio', 'community_is_registered', array(
'label'=>__('Registered Community?'),
'multiOptions' => array(
1 => 'Yes',
0 => 'No',
),
'separator' => '',
'value' =>'0'
));
$this->addElement('text', 'name', array(
'filters' => array('StringTrim'),
'validators' => array(
array('StringLength', false, array(0, 100)),
),
'required' => true,
'label' => __('Applicant Name') . ':',
));
$this->addElement('submit', 'save', array(
'required' => false,
'ignore' => true,
'label' => __('Save'),
));
$this->save->removeDecorator('label');
}
}
This is a form to add some information of a file. Here i want to show the section of "Registered Community?" only after the button "Community" is clicked at "Applicant Type". Seeing for help !!
Since you want certain behavior to occur on the client-side based upon certain actions taking place on the client-side, it seems to be fundamentally a client-side question.
Perhaps the easiest thing is to:
Add a certain CSS class to the Registered Community element (or display group, if it's a collection of elements) during server-side form creation.
Use front-end CSS to hide all form elements of that class.
Add a client-side on-click handler to the "Applicant Type" element that removes/changes the class or shows/hides the "Registered Community" section when the "Applicant Type" has the desired value.

How to filter zf2 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

ZendFrame work - how to disable a text box

I am using zend_form in my project. In a form i want to disable a text box. Here is the code:
$personal_information = new Zend_Form(array(
'method' => 'post',
'elements' => array(
'first_name' => array('text', array(
'required' => true,
'filters' => array('StringTrim'),
'validators' => array(
array('NotEmpty', true),
array(),
array('stringLength', false, array(1, 40))
),
'decorators' => $elementDecorators,
'label' => 'First name:'
)),
// THE "NEXT" BUTTON
'signup' => array('submit', array(
'decorators' => $buttonDecorators,
'label' => 'Next',
'required' => false,
'ignore' => true,
))
)
));
How to disable a text box in zend_form?
here is an example of disabled and readonly text field
$lati = new Zend_Form_Element_Text("lati" , array("readonly" => "readonly"));
$lati = new Zend_Form_Element_Text("lati" , array("disabled" => "disabled"));
i think this way is more clear than the way you add elements to the form ,

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)
)
)
));