I need an "onclick" action on the button so i get redirected for something like this:
location/[textfield_data]
http://snag.gy/wz395.jpg
I am using cakephp and at this moment the nearest i reached was this.
echo $this->Form->input('link', array('label' => false, "class" => "form-control input-medium", "placeholder" => __('Procurar')));
echo $this->Form->button('', array('class' => 'btn btn-primary icon-search icon-white' ,'onclick' => "location.href='/'"));
With my code, cakephp is retrieving me this:
/[view]?data%5Blink%5D=
The [view] is the current page i am in.
FOUND THE SOLUTION
Ive found the solution this way.
echo $this->Form->input('link', array('label' => false, "class" => " form-control input-medium", "placeholder" => __('Procurar'), 'id' => 'search'));
echo $this->Form->button(null, array('class' => 'btn btn-primary icon-search icon-white' ,'onclick' => "location.href='/mywantedurl/'+document.getElementById('search').value;"));
Note that i didnt use any form->create or form->end, otherwise it wont work.
Why do you need to use JavaScript? Just use the GET method instead. You should be using GET instead of POST requests for searches any way, so the request can be book-marked etc.
You would achieve this using $this->request->query instead of $this->request->data.
<?php
// app/View/Locations/index.ctp
echo $this->Form->create('Location', array('type' => 'get'));
echo $this->Form->input('Location.keywords');
echo $this->Form->end('Search');
And then in your corresponding controller:
<?php
// app/Controller/LocationsController.php
class LocationsController extends AppController {
public function search() {
if (!isset($this->request->query['keywords'])) {
throw new BadRequestException();
}
$results = $this->Location->findByKeywords($this->request->query['keywords']);
$this->set('results', $results);
}
}
I don’t know the actual schema of your database tables or your model names, but the above should point you in the right direction.
I use a code like this
echo $this->Form->input('link', array('label' => false, "class" => "form-control input-medium", "placeholder" => __('Procurar'), 'id' => 'search-field'));
echo $this->Form->button('', array('class' => 'btn btn-primary icon-search icon-white' , 'id' => 'search-button'));
$this->Js->get('#search-button');
$this->Js->event('click', 'edit()');
$this->Js->buffer
(
"function edit()
{
search_term = this.document.getElementById('search-field').value;
if(search_term)
window.location = \"/index.php/location/\" + search_term;
return false;
}"
);
Related
I have translates table with example data:
So, this table holds records, which will represent custom translation texts.
Now i want to build a form to edit all of those rows in one page / form.
This is controlle code:
public function translate() {
$this->loadModel('Translate');
$data = $this->Translate->find('all');
$this->set('data', $data);
pr ($this->request->data);
if ($this->request->is('post','put')) {
if ($this->Translate->save($this->request->data)) {
$this->Session->setFlash('Recipe Saved!');
return $this->redirect($this->referer());
}
}
}
And view - please note, that i have used loop for creating inputs, not sure if cakephp has better way to do this.
<?php echo $this->Form->create('Translate'); ?>
<?php
foreach ($data as $d) {
echo $this->Form->input('text', array('label' => 'Link strony', 'type' => 'text','value'=>$d['Translate']['text']));
echo $this->Form->input('id', array('type' => 'hidden', 'value' => $d['Translate']['id']));
}
?>
<?php echo $this->Form->end(array('class' => 'btn btn-success floatRight', 'label' => 'Zapisz')); ?>
For now, this code works, but not as i expect. $this->request->data shows only last input, ignoring other ones. Attached, you see debug of $this->request->data. Only last item is edited. All i want is to have ability to edit selected input and save. Thanks for help.
Looks like you're saving multiple rows in a single form. In that case,
you need to change your approach a bit.
Use proper indices in the Form helper.
Use saveAll() instead of save() to save multiple data.
Changes to the View file:
<?php
foreach ($data as $k => $d) {
echo $this->Form->input('Translate.'.$k.'.text', array(
'label' => 'Link strony',
'type' => 'text',
'value' =>$d['Translate']['text']
));
echo $this->Form->input('Translate.'.$k.'.id', array(
'type' => 'hidden',
'value' => $d['Translate']['id']
));
}
?>
And then, in your controller:
if ($this->request->is('post','put')) {
$this->Translate->saveAll($this->request->data['Translate']);
/* Other code */
}
Try to specify the name as array (translate[]) :
echo $this->Form->input('text', array('label' => 'Link strony', 'type' => 'text','value'=>$d['Translate']['text'],'name'=>'translate[]'));
Using cakephp 1.3 I fill out a form, and upon debugging the submit, I notice that
data:$("#submit-478065271").closest("form").serialize()
is empty. Why would something like that happen? I've checked that the form actually has data, which I can serialize manually. Here is the submit code:
echo $this->Js->submit(
'Sign up',
array(
'class' => 'btn btn_submit fr register_submit register_btn_align',
'url' => array('controller' => 'email_guides', 'action' => 'subscribe'),
'before' => '$(".error-message").remove();' . $this->Js->get('#loading')->effect(
'fadeIn',
array('buffer' => false)
),
'complete' => $this->Js->get('#loading')->effect(
'fadeOut',
array('buffer' => false)
) . 'debugger;',
'success' => 'if(data.success) {
$("#CustomUserFirstName").val("");
$("#CustomUserEmail").val("");
$("#EmailGuidesUserStartDate").val("' . date('d/m/Y', strtotime('+1 Weekday')) . '");
$("#EmailGuidesUserTerms").attr("checked", false);
$("#signupModal").hide();
} else {
$("#signupModal").hide();
}
'type' => 'json'
)
);
?>
<?php echo $this->Form->end(); ?>
UPDATE: I've noticed that the form is not a parent of the submit element ... which is very bizarre. This would explain why .closest("form") returns an empty array.
I'm kinda noob yet using cakephp, but I would love to know how to use it, for that, I'm trying to develop something pretty simple.
I have a form, that has an input type = 'text' and I wanna search in my database what I just type in this input.
How can I do that ?
I know that I have to get what is inside of my input text and execute a function in my controller that will receive a parameter and execute the query and return the same query to my view.
Other thing that I couldn't do it yet is this:
<button type="submit" class="btn"><i class="icon-search"></i></button>
I wanna change this to cake format, like:
echo $this->Form->input('button', array('class' => 'btn', type = 'submit'));
but no idea how to add the icon.
Thanks.
You can specify the button text like.
<?php
echo $this->Form->button(
"<i class='icon-search'></i>",
array(
'class' => 'btn',
'type' => 'submit',
'escape' => false
)); // make sure you've use escap = false with html input.
?>
and for the search you can define the action like this.
<?php
public function search() {
$conditions = array();
$params = $this->passedArgs;
if(isset($params['text_field_name']) && !empty($params['text_field_name'])){
$conditions['ModelName.text_field_name like'] = $params['text_field_name'].'%';
}
$this->Paginate->settings = array(
'conditions' => $conditions
);
$this->set('variable_name', $this->Paginator->paginate());
}
?>
you can also use this for searching
---- in your .ctp file
<?php echo $this->Form->create('ModelName'); ?>
<?php echo $this->Form->input('ModelName.search_word'); ?>
<?php echo $this->Form->submit(" <i class='icon-search'></i> " , array('escape' => false , 'class' => 'btn' ) ); ?>
<?php echo $this->Form->end(); ?> // note that no params for end() method
---- in your controller file
$searchWord = $this->request->data['ModelName']['search_word'] ;
$this->paginate = array('limit' => 30 ,
'conditions' => array('OR' => array('ModelName.field1' => "%".$searchWord."%" ,
'ModelName.field2 LIKE' => "%".$searchWord."%" )));
$this->set('searchResults', $this->paginate());
I have a simple delete function. Here is where I set the custom data:
<?php echo $this->Html->link(
__('Delete'),
'#CoursesModal',
array(
'class' => 'btn-remove-modal',
'data-toggle' => 'modal',
'role' => 'button',
'data-uid' => $course['Course']['id'],
'data-uname' => $course['Course']['name']
));
?>
It shows in the element fine:
Delete
But when it comes to deleting this object (here's the code for it:)
<?php echo $this->Html->link(__('Delete'),'/courses/delete/#{uid}',array('class' => 'btn btn-danger delete-course-link')) ?>
For some reason the {uid} doesn't translate to 5 - which causes it not to work.
What am I doing wrong?
I´m developing an application using Zend Framework 2 and I use FormRow helper to render a label, the input and errors (if present) in a Form.
//within the view
echo $this->formRow($form->get('Name'));
When a user submits the form without filling the required input text field FormRow render´s it with the following error message:
<label>
<span>Name: </span>
<input class="input-error" type="text" value="" placeholder="Insert Name Here" name="Name">
</label>
<ul>
<li>Value is required and can't be empty</li>
</ul>
How can I set a class for the li tag to style it afterwards?
I know that I can echo the formElementErrors with the desired class attribute via..
<?php echo $this->formElementErrors($form->get("Name"), array('class' => "valuerequired", 'message' => "errortestmessage")); ?>
..but FormRow will still render the error message without the class.
Just for reference I´m setting the entity this way:
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'Name',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
See the code of formElementErrors
Basically you could do something like:
$this->formElementErrors($elem)
->setMessageOpenFormat('<ul%s><li class="some-class">')
->setMessageSeparatorString('</li><li class="some-class">');
But that is quite unhandy...
The better solution would be to extend the Zend\Form\View\Helper\FormElementErrors by your own class and then register the view-helper formElementErrors to your class. So basically you'd have something like this:
namespace Mymodule\Form\View\Helper;
use Zend\Form\View\Helper\FormElementErrors as OriginalFormElementErrors;
class FormElementErrors extends OriginalFormElementErrors
{
protected $messageCloseString = '</li></ul>';
protected $messageOpenFormat = '<ul%s><li class="some-class">';
protected $messageSeparatorString = '</li><li class="some-class">';
}
Last thing then would be to register the view helper with this new Class. For this you provide the following code inside your Modules Module.php
public function getViewHelperConfig()
{
return array(
'invokables' => array(
'formelementerrors' => 'Mymodule\Form\View\Helper\FormElementErrors'
),
);
}
displaimer: This code isn't tested, let me know if there are some errors, but i think this should work out quite well.
Ok, the solution to my own problem was right in front of me, instead of using:
//within the view
echo $this->formRow($form->get('Name'));
I called each element of the form individually:
//within the view
echo $this->formLabel($form->get('Name'));
echo $this->formInput($form->get('Name'));
echo $this->formElementErrors($form->get("Name"), array('class' => "some_class", 'message' => "errormessage"));
Don´t know if it´s the most efficient way of doing it, plz drop a line if you think otherwise.
FormRow check if "form_element_errors" plugin registered. And if so use it as default to display error messages.
So Sam's example work. You should redefine standard plugin and inform mvc about it.
I'v done it in module.config.php
'view_helpers' => array(
'invokables' => array(
'formElementErrors'=> 'MyModule\View\Helper\FormElementErrors',
and FormRow start display errors as I wish :)
As your problem, please try
Change
//within the view
echo $this->formRow($form->get('Name'));
to
//within the view
echo $this->formRow($form->get('Name'),null,false);
// Note: add more 2 last parameters, false- for $renderErrors => will NOT render Errors Message.
//Look original function in helper/formrow.php: function __invoke(ElementInterface $element = null, $labelPosition = null, $renderErrors = null, $partial = null)
Render Errors Message as your funciton
echo $this->formElementErrors($form->get('name'), array('class' => 'your-class-here'));
From the documentation of ZF2. Here's the link: http://framework.zend.com/manual/2.0/en/modules/zend.form.view.helpers.html#formelementerrors
echo $this->formElementErrors($element, array('class' => 'help-inline'));
// <ul class="help-inline"><li>Value is required and can't be empty</li></ul>
I use echo $this->formElementErrors($form, array('class' => "error-messages")); to show all error messages in one place:
echo $this->formElementErrors($form, array('class' => "error-messages"));// Print all error messagess
echo $this->formLabel($form->get('Name'));
echo $this->formInput($form->get('Name'));
echo $this->formLabel($form->get('Name2'));
echo $this->formInput($form->get('Name2'));