CakePHP Html->link and Form->end - cakephp

I have this link that I use to for my modal
<?php
echo $this->Html->link('Create Schedule', '#', array('id' => 'createSchedule', 'class' => 'btn btn-success', 'role' => 'button', ));
?>
How do I convert it to $this->Form->end() format?
Thank you!

You can do this also
<?php
echo $this->Form->button("Create Schedule", array("id" => "createSchedule", "class" => "btn btn-success", "type" => "submit"));
echo $this->Form->end();
?>
or you could also do this
<?php
echo $this->Form->submit("Create Schedule", array("id" => "createSchedule", "class" => "btn btn-success"));
echo $this->Form->end();
?>

You can try this
<?php
$options = array(
'label' => 'Create Schedule',
'id' => 'createSchedule',
'class' => 'btn btn-success'
);
echo $this->Form->end($options);
?>

Related

how can i write helper link with image link in span tag cakephp

i want to generate this
<a href="/users/signup" class="sf-with-ul">
<span class="profile-avatar">
<img alt="" src="img/avatar/anonymous.png" height="40" width="40">
</span>
</a>
I have written
<?php echo $this->Html->link(
$this->Html->image('avatar/anonymous.png',array('height' =>40,'width'=>'40')), array('controller' => 'users', 'action' => 'signup'),
array('class' => 'sf-with-ul', 'escape' => false));?>
which generates
<span class="profile-name"></span><img src="/FindTutor/img/avatar/anonymous.png" height="40" width="40" alt="" />
Any help? Thanks in advance.
you can try this just change your image path
<?php echo $this->Html->link('<span class="profile-avatar">'. $this->Html->image('home/logo.png',array('width'=>'40px','height'=>'40px'), array('alt' => '')), array('controller' => 'users', 'action' => 'signup' ), array('class' => 'sf-with-ul', 'escape' => false)).'</span>';?>
Try this:
<?php
$img = $this->Html->image("avatar/anonymous.png",
array("height" => "40", "width" => "40")
);
$img_span = $this->Html->tag('span', $img,
array('class' => 'profile-avatar')
);
echo $this->Html->link($img_span,
array("controller" => "users", "action" => "signup"),
array("escape" => false)
);
?>
This looks a little lengthy but it's easier to understand and works exactly like what you're asking for.
Peace! xD

How to validate a form with radio group in CodeIgniter

I have a form that presents questions and answers from a survey. User can select between 5 radio buttons - to choose answer of questions. They are array and the key is question_id - each question has these 5 radio buttons and they differ on this key.Could you help me with form validation?
Now if you choose answer for one of the questions, form submits. It has to submit only if all questions have answers. That's my view:
<html>
<head></head>
<body>
<?php
$survey_id = $this->uri->segment(3);
$question_id = $this->uri->segment(4);
$att=array('id'=>'form');
echo form_open('index/survey_fill/' .$survey_id .'/'. $question_id , $att);
echo "<table border="0" id='questionsTable' >";
echo "<tr><th>Въпрос</th></tr>";
echo validation_errors();
$index = 0;
foreach ($question as $row)
{
echo "<tr id='$index'>";
$index++;
?>
<td>
<?php echo "$row->question"; ?><br/>
<?php echo "<input type='hidden' name='question_id' value='$row->question_id' />"; ?>
<?php
$data=array(
'name' => 'answer['.$row->question_id.']',
'value' => '5',
'class' => 'answer'
);
echo "<input type='hidden' name='survey_id' value='$row->survey_id'>";
echo form_radio($data);
echo " 5 ";
$data=array(
'name' => 'answer['.$row->question_id.']',
'value' => '4',
'class' => 'answer'
);
echo form_radio($data);
echo " 4 ";
$data=array(
'name' => 'answer['.$row->question_id.']',
'value' => '3',
'class' => 'answer'
);
echo form_radio($data);
echo " 3 ";
$data=array(
'name' => 'answer['.$row->question_id.']',
'value' => '2',
'class' => 'answer'
);
echo form_radio($data);
echo " 2 ";
$data=array(
'name' => 'answer['.$row->question_id.']',
'value' => '1',
'class' => 'answer'
);
echo form_radio($data);
echo " 1 ";
?>
</td></tr>
<?php
}
?>
</table>
<?php echo "<input type='hidden' name='question_id' value='$row->question_id' />"; ?>
<?php echo '<input type="submit" id="button" name = "submit" value="Submit" class="btn btn-success">';
?>
</form>
</div>
</body>
</html>
My controller is:
public function survey_fill()
{
$this->form_validation->set_rules('answer[]', 'Answer', 'required');
if ($this->form_validation->run()==FALSE)
{
$this->survey_show();
}
else
{
if ($this->user_model->survey_fill())
{
header('Refresh: 2; url=/survey/index.php/index/surveys_show');
}
else
{
$this->load->model('user_model');
$data['survey'] =$this->user_model->survey_show();
$data['dynamic_view'] = 'survey_show';
$data['menu']=$this->menu_model->get_menu();
$this->load->view('templates/main',$data);
}
}
}
Two ways:
1) Set the required attribute in your data array like
$data = array('name' => 'answer['.$row->question_id.']', 'required' => 'required');
2) Set the form validation for each of your answer[$question_id] variables.
foreach($questions as $question){
$this->form_validation->set_rules('answer['.$question->id.']', 'Question '.$question->id, 'required');
}
I'd stick with 1.

cannot set id in cakephp postLink

I am unable to set the id of the form or the class of the submit button. I seen examples that follow my code but I am not getting either set.
I am using cakePHP 2.5.
<?php
echo $this->Form->postLink(
'Confirm',
array('controller' => 'assets', 'action' => 'delete'),
array('id' => 'testId', 'class' => 'btn btn-confirm')
);
?>
What I get is
<form action="/assets/ajax_delete/56.json" name="post_53ac1bb05415c464261170" id="post_53ac1bb05415c464261170" style="display:none;" method="post">
<input type="hidden" name="_method" value="POST">
</form>
Confirm
<?php echo $this->Form->postLink('Confirm', array('controller' => 'assets', 'action' => 'delete', $yourId), null, __('Are you sure you want to delete # %s?', $yourId)); ?>

render elements in cakephp on home.ctp

I am using cakephp 2.4
in my apps I have a view in CategoriesController namely "Editorial" and I can show all the articles under that category by http://mydomain/categories/editorial
I am trying to show all the articles under "Editorial" category in the home.ctp by echo $this->element('editorials');
but it shows Notice (8): Undefined variable: articles [APP\View\Elements\Editorials.ctp, line 4]
CategoriesController.php
public function Editorial() {
$category = $this->Category->find('first', array(
'conditions' => array(
'Category.id' => 1
)
));
$this->set(compact('category'));
$this->Paginator = $this->Components->load('Paginator');
$this->Paginator->settings = array(
'Article' => array(
'recursive' => -1,
'contain' => array(
'Category'
),
'limit' => 5,
'conditions' => array(
'Article.category_id' => $category['Category']['id'],
),
'order' => array(
'Article.id' => 'ASC'
),
'paramType' => 'querystring',
)
);
$articles = $this->Paginator->paginate($this->Category->Article);
$this->set(compact('articles'));
}
View file:
<?php if (!empty($articles)): ?>
<?php echo $this->element('editorials'); ?>
<?php echo $this->element('pagination-counter'); ?>
<?php echo $this->element('pagination'); ?>
<?php endif; ?>
Elements/Editorials.ctp:
<div class="row">
<?php
$i = 0;
foreach ($articles as $art):
$i++;
if (($i % 4) == 0) { echo "\n<div class=\"row\">\n\n";}
?>
<div class="col col-sm-3">
<?php echo $this->Html->link($art['Article']['title'], array('controller' => 'articles', 'action' => 'view', 'id' => $art['Article']['id'])); ?>
<br />
</div>
<?php
if (($i % 4) == 0) { echo "\n</div>\n\n";}
endforeach;
?>
<br />
<br />
</div>
try to add $this->render('file'); in your Editorial action and replace file with view file name without.ctp
Something you can change:
1. In controller:
$this->set(compact('category', 'articles')); // one set()
2. In view: (Main Point)
<?php echo $this->element('editorials', array('articles' => $articles)); ?>
Here, you need to pass the articles variable to element like above, by default an element don't recognize the viewVars.

How to create link make cakephp with twitter bootstrap

example:
<a class="btn btn-mini" href="#"><i class="icon-star"></i> Star</a>
i try by Html::Helpers like
echo $this->Html->link($this->Html->tag('i','',
array('class' => 'btn')), array('action' => '../link'));
but it not work !
try:
echo $this->Html->link(
$this->Html->tag('i', '', array('class' => 'icon-star')) . " Star",
array('action' => 'your_action'),
array('class' => 'btn btn-mini', 'escape' => false)
);

Resources