Place div infront of input element in Zend form - php

Im working on zend form..My problem is..
I have designed sigin and sign up froms using zend forms.My sample snippet is..
$this->addElement('Text', 'email', array(
'label' => $email,
'required' => true,
'allowEmpty' => false,
'inputType' => 'email',
'placeholder' => 'Hello#gmail.com',
));
<div id="email-element" class="form-element">
<input type="email" name="email" id="email" value="" autofocus="autofocus" placeholder="Hello#gmail.com"></div>
But I need to place a div with name email_icon with class name form_icon infront of input element to place an image..But Im unable to get snippets to place div in zend form elements.Could you please help me for few snippets..o tuts..or some guidance..

When displaying the email input element in your view script, as per Zend Framework 2, you can use below code snippet.
echo "<div id='email_icon_div'>";
echo $this->formRow($form->get('email'));
echo "</div><div class='clr'></div>";
I hope this helps.

Related

How to add contextual classes in Yii 2 ActiveField / ActiveForm?

In Bootstrap 3 we can add a contextual class to a form-group container which will color that form field container in a specific color, ie. has-error will render it reddish:
<div class="form-group has-error">
<label for="field">Erroneous label</label>
<input type="text" class="form-control" placeholder="Erroneous input" id="field" />
<p class="help-block">Erroneous help-block</p>
</div>
Label, the input's text color and border and final p.help-block will all become red.
In Yii 2 we can use ActiveForm and ActiveField to do the same in one-liner:
<?= $form->field($model, 'field')
->textInput(['maxlength' => true, 'placeholder' => 'Erroneous input'])
->label('Erroneous label')
->hint('Dummy hint') ?>
And it generates roughly the same markup as above, in a form-group container.
I have gone through the docs and didn't find a way to add a has-error class to the form-group container.
$hintOptions
$inputOptions
$labelOptions
Do not work for this scenario.
Yii automatically adds has-error class in case of validation errors.
If you want to add any CSS-class to ActiveField container then you can use options property. For example:
<?= $form->field($model, 'field', [
'options' => [
'class' => 'form-group has-error',
],
])
->textInput(['maxlength' => true, 'placeholder' => 'Erroneous input'])
->label('Erroneous label')
->hint('Dummy hint');
?>

Styling form in Yii2 (ActiveField / ActiveForm)

I'm really struggling getting my layout into Yii2. To be as close as possible to the state-of-the-art, I read about styling the ActiveField and ActiveForm, which kinda works, but not to 100%. Maybe you see whats wrong.
Let me start of with my HTML, which I want to have:
// normal state without any validations
<p>
<label for="signup_name">Name, Surname:<span class="required">*</span></label>
<input type="text" name="signup_name" id="signup_name" value="" />
</p>
// input field after validation fails (ajax + reload)
<p>
<label for="signup_email">E-mail:<span class="required">*</span></label>
<input class="error-input" type="text" name="signup_email" id="signup_email" value="" />
<span class="error-msg">E-mail must be filled !</span>
</p>
This is the current state, in which the layout is in:
<p>
<label class="control-label" for="signupform-username">Username</label>
<input id="signupform-username" class="form-control" type="text" name="SignupForm[username]">
<span class="error-msg">Username may not be empty</span>
</p>
This is my PHP / Yii2-part
<?php
$form = ActiveForm::begin([
'id' => 'form-signup',
'fieldConfig' => [
'template' => "<p>{label}{input}{error}</p>",
'errorOptions' => [
'tag' => 'span',
'class' => 'error-msg',
],
],
]);
echo $form->field($model, 'username', [
'inputTemplate' => '{input}',
])->textInput(['autofocus' => false])
?>
The Problems:
The <span class="error-msg"> is always rendered, even though there is no error yet. To clarify: The span has no content and is rendered like this:
<span class="error-msg"></span> (I want it to be there if there IS actually an error, not as a placeholder before
I can't figure out on how to apply an error-class to the input, which works for the ajax-validation, as well as the not JS-way.
Any help is appreciated,
thanks
you can add class for each field if you want
<?= $form->field($model, 'title')->textInput(['class'=>'form-control'])->label('Your Label',['class'=>'label-class']) ?>
Try to disable ajax validation of input field.
echo $form->field($model, 'username', ['enableAjaxValidation' => false]);
or
echo $form->field($model, 'username', ['enableClientValidation' => false]);

codeigniter search form error

I have been trying to figure out what is wrong with my form search form. I have tried to build a basic search form in code igniter. I have built a search form before and I am building it the same way for a different application and I keep getting the
An Error Was Encountered
The action you have requested is not allowed.
This is not very descriptive for troubleshooting. I am using the same config as I did in my last project. So I don't understand why I am getting the above error message.
When I go to the http://www.gaddisweb.com/index.php/childtest/search, the search form displays the view code. Now, I have rules set the the field is required. I click the search button with the field empty and it should give me and error message saying field required.
Instead, I get the above error message. Even If I put anything in the block I get the same error message. I know I am missing something simple but I can't see it.
Controller Code:
class ChildTest extends CI_Controller {
public function index(){
$this->search();
}
public function search(){
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules(array(
array(
'field' => 'keyword',
'label' => 'Child Family Name',
'rules' => 'required',
)
));
$this->form_validation->set_error_delimiters('<div class="alert alert-error">', '</div>');
if(!$this->form_validation->run()){
$this->load->view('childsearch');
}else{
$keyword = $this->input->post('keyword');
echo $keyword;
}
}
}
View Code:
<div id="search">
<p></p>
<form id="search" method = "post" action="">
<label id="familyname">Child Family Name</label>
<input type="text" size="20" id="keyword" name="keyword"/>
<input type="submit" value="Search" />
</form>
</div>
If the block is empty I should get an error message saying so and if there is anything in the block it should be displayed to me on the post action.
Neither is happening.
Thanks for your help.
Answer
This is my opinion on what happened. I can't find any documentation that says this but. I was able to achieve what I was trying to do. I had to do a slight rewrite because the difference between the first time I did a search form and the second time was the fact of using the security sessions.
In my opinion, CI when you turn on the sessions will not allow post to the same page. The post data must be passed to another controller and cannot post to the originating controller.
This part was taken out of the original controller above and placed in another that I name childvalidation.
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules(array(
array(
'field' => 'keyword',
'label' => 'Child Family Name',
'rules' => 'required',
)
));
$this->form_validation->set_error_delimiters('<div class="alert alert-error">', '</div>');
if(!$this->form_validation->run()){
}
Once I added the childvalidation controller and passed the form data to it, then I was able to complete the search function.
View Code changed to:
<?php echo validation_errors() . "</br>";?>
<?php echo form_open('childsearchvalidation'); ?>
<label id="familyname">Child Family Name</label>
<input type="text" size="20" id="keyword" name="keyword"/>
<input type="submit" value="Search" />
</form>
Hope this helps someone.

How to validate form without entity in controller?

I have a basic form to send an email :
<form action="{{ path('user_send_mail') }}" method="post" enctype="multipart/form-data">
<input name="email_to" type="email" class="form-control" placeholder="To" required>
<input name="email_cc" type="email" class="form-control" placeholder="Cc">
<textarea name="message" id="email_message" class="form-control" placeholder="message" style="height: 120px;" required></textarea>
<input type="file" name="attachment"/>
<p class="help-block">Max. 2MB</p>
<button type="submit" class="btn btn-primary pull-left"><i class="fa fa-envelope"></i> Send the email</button>
</form>
And an action in my controller to validate the data and send the mail but validation doesn't work..
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\Validator\Constraints\NotBlank;
public function sendMailToUserAction(Request $request)
{
$form = $this->createFormBuilder(null)
->add('email_to', 'email', array(
'required' => true,
'constraints' => new Email()
))
->add('email_cc', 'email', array(
'required' => false,
'constraints' => new Email()
))
->add('message', 'textarea', array(
'required' => true,
'constraints' => new NotBlank()
))
->add('attachment', 'file', array(
'required' => false,
'constraints' => new File()
))
->getForm();
$form->handleRequest($request);
if ($form->isValid()) {
$data = $form->getData();
var_dump($data);exit;
} else {
var_dump("error");exit;
}
}
When I submit the form, I got always "error", so validation doesn't work but I don't know why... I have read a lot of topics about this but I get stuck..
EDIT : If I print form error using $form->getErrorsAsString() instead of var_dump("error");exit;, I got this :
string(97) "email_to: No errors email_cc: No errors message: No errors attachment: No errors "
please reffer to this: http://symfony.com/doc/current/book/forms.html#using-a-form-without-a-class it think you have to pass array with fields name that you want to validate (its like a default values)
p. s. why not using Simple PHP Object for that? Symfony2 forms fields name should contain brakets like (for example): data[email_to] instead of email_to

Zend Framework. How to add extra class to label

My problem is after the form has been generated, the label class has assigned to a default which is optional and the html code looks like this:
<div class="form_wrapper">
<label for="email" class="optional">Username(e-mail):</label>
<input type="text" name="email" id="email" value="">
</div>
My zend code inside action is:
$form->addElement('text', 'email');
$usernameElement = $form->getElement('email');
$usernameElement->setLabel('Username(e-mail):');
$usernameElement->setDecorators(array(
'ViewHelper',
'Label',
new Zend_Form_Decorator_HtmlTag(array('tag' => 'div','class'=>'form_wrapper'))
));
But in some case, I want it to be set as a different value that I can easily style in css. The html code I want is this(get rid of "optional"):
<div class="form_wrapper">
<label for="email" class="email_label">Username(e-mail):</label>
<input type="text" name="email" id="email" value="">
</div>
Assuming this is ZF1, I dug this out the reference for 1.12.3:
$form->getElement('elementname')
->setDecorators(array(
array('ViewHelper',
array('helper' => 'formText')),
array('Label',
array('class' => 'another'))
));
The above provides you with an additional class. So you'll get something like:
<label for="elementname" class="another optional">Your Label Text</label>
OR
<label for="elementname" class="another required">Your Label Text</label>
Take a look at this similar question:
Zend_Form - add CSS Class :: How can I add css class to label in Zend_Form?
Adding a class to the form element itself is as simple as:
$usernameElement->class = 'email_label';
It appears that if you want to set the class on the label though, you'll want to add a Zend_Form_Decorator_Label and pass in the classname.

Categories