issue in validation a form a in zend - forms

I am using zend form and try to validate a client side validation. 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->setAttrib('id', 'lead_form_creation');
$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,}'
)
));
By the above three fields I am able to create a form. Also my code is validating above three fields also. But whenever I am writing some valid email address and press tab then name field and phone number fields are showing red simultaneously. But user's perspective it should be only name field. Then if name is not validating and press tab, name field s well as phone field should be show error.
Please let me know whether anything I have to mention in the attrbs attribute or anywhere else?
Thanks in advance.

Typo in your code (attribs should be attributes):
$this->addElement('text', 'email', array(
'label' => 'Email:',
'required' => true,
'class' => 'span12',
'attributes' => array(
'required' => true,
'pattern'=> "^[A-Za-z0-9._]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$"
)
));
$this->setAttrib('id', 'lead_form_creation');
$this->addElement('text', 'name', array(
'label' => 'Name:',
'required' => true,
'class' => 'span12',
'attributes' => array(
'required' => true,
'pattern' => '[a-zA-Z]{4,}'
)
));
$this->addElement('text', 'phone', array(
'label' => 'Phone:',
'required' => true,
'class' => 'span12',
'attributes' => array(
'required' => true,
'pattern' => '\d{4,}'
)
));

Related

ZF2 - how to check if a route with child exists

Given this route:
'country' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/1country',
'defaults' => array(
'controller' => 'Index',
'action' => 'country',
'country_id' => '5',
),
),
'may_terminate' => true,
'child_routes' => array(
'city' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/1city',
'defaults' => array(
'controller' => 'Index',
'action' => 'city',
'city_id' => '4',
),
),
),
),
),
if I do:
$this->getServiceLocator()->get('Router')->hasRoute('country'); //true
$this->getServiceLocator()->get('Router')->hasRoute('country/city'); //false
While both should be true.. Any ideas?
Try this:
// explode if child..
$route = 'home/default';
$names = explode('/', $route, 2);
// use
/* #var Router $router */
$router = $container->get('Router');
if (!$router->hasRoute($names[0])) {
// ...
}

Remove 'label__' from prototype name

I have a form like this:
$formMapper
->add('formats', 'collection', array(
'type' => new FormatType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'prototype_name' => 'remove'
))
->add('papers', 'collection', array(
'type' => new PaperType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'prototype_name' => 'remove'
))
;
As you can see I changed the prototype_name to remove.
Originally it looks like this:
When I click on the plus sign I get this:
As you can see the remove text is added. But also label__* . How can I fix this that the label__* text isn't added or displayed?
Use 'label_attr' => ['style' => 'display: none']
$formMapper
->add('formats', 'collection', array(
'type' => new FormatType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'label_attr' => ['style' => 'display: none']
))
->add('papers', 'collection', array(
'type' => new PaperType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'label_attr' => ['style' => 'display: none']
))
;
->add('appointment', CollectionType::class,[
'label' => false,
'entry_type' => AppointmentType::class,
'entry_options' => ['label' => false],
'allow_add' => true,
'allow_delete' => false,
'prototype' => true,
])
this is my collection for add appointments see the entry_options

zf2 form collection don't show element description

When the collection element is rendered , description element does not appear.
ZF2 seems to ignore the description option in the form collection.
Form
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'value',
'options' => array(
'label' => 'Please choose value',
'count' => 1,
'should_create_template' => true,
'target_element' => new ValueFieldset($objectManager)
)
));
Form Fieldset: ValueFieldset
$this->add(array(
'name' => 'name',
'type' => 'Zend\Form\Element\Text',
'options' => array(
'label' => 'Values',
'label_attributes' => array(
'class' => 'form-label'
),
'description' => 'X',
'multiple' => true,
),
)
);
View
echo $this->formCollection($values);

Symfony2 Looping Same field in form

I have a problem in symfony2 form.
I have a same field in a form. I create one by one by changing the name.
$formBuilderUpload = $this->createFormBuilder()->setAction($this->generateUrl('bsi_bkpm_backend_uploadfile_create'));
$formBuilder = $formBuilderUpload;
$formBuilder->add('file_1','file', array('required'=> false, 'label'=> 'File 1'))
->add('filename_1' , 'text', array(
'required' => false,
'label' => 'Subject 1'
))
->add('description_1','text',array('required'=> false, 'label'=>' Description 1'))
->add('file_2','file', array('required'=> false, 'label'=> 'File 2'))
->add('filename_2' , 'text', array(
'required' => false,
'label' => 'Subject 2'))
->add('description_2','text',array('required'=> false, 'label'=>' Description 2'))
->add('file_3','file', array('required'=> false, 'label'=> 'File 2'))
->add('filename_3' , 'text', array(
'required' => false,
'label' => 'Subject 3'))
->add('description_3','text',array('required'=> false, 'label'=>' Description 4'))
->add('file_4','file', array('required'=> false, 'label'=> 'File 2'))
->add('filename_4' , 'text', array(
'required' => false,
'label' => 'Subject 4'))
->add('description_4','text',array('required'=> false, 'label'=>' Description 4'))
->add('file_5','file', array('required'=> false, 'label'=> 'File 5'))
->add('filename_5' , 'text', array(
'required' => false,
'label' => 'Subject 5'))
->add('description_5','text',array('required'=> false, 'label'=>' Description 5'))
// add submit button
->add('submit', 'submit', array('label' => 'Create'));
//Create the form
$form = $formBuilder->getForm();
return $form;
But I want the code become efficient by looping it. Does anyone know how to looping it?
Best regards,
hendrawan
This seems like a pretty easy one, just loop over a for loop like so...
$limit = 5;
for ($i = 1; $i <= $limit; $i++) {
$builder
->add('file_'.$i, 'file', array(
'required' => false,
'label' => 'File '.$i,
))
->add('filename_'.$i, 'text', array(
'required' => false,
'label' => 'Subject '.$i,
))
->add('description_'.$i, 'text', array(
'required' => false,
'label' => 'Description '.$i,
))
;
}

Implement onkeyup() function in Zend forms to add an element

I want to implement a sort of onkeyup() function in Zend form to add an element. I don't know the syntax.
This is my code:
$this->addElement('text', 'userid', array(
'label' => '',
'required' => true,
'filters' => array('StringTrim'),
'style' => array('width:212px'),
Use the following code:
$this->addElement('text', 'userid', array(
'label' => '',
'required' => true,
'filters' => array('StringTrim'),
'style' => array('width:212px'),
'attribs' => array('onkeyup'=>'jsFunction();')
));
OR you can use below code:
$element = $this->addElement('text', 'userid', array(
'label' => '',
'required' => true,
'filters' => array('StringTrim'),
'style' => array('width:212px'),
));
$element->setAttrib('onkeyup', 'jsFunction();');
you have to use attribs options to add extra attributes
Try This
$this->addElement('text', 'userid', array(
'label' => '',
'required' => true,
'filters' => array('StringTrim'),
'style' => array('width:212px'),
'attribs' => array('onkeyup'=>'handler()')
you can also use setAttrib