I am using laravel collective to generate my forms and in one of the dropdown I have the following ternary operator which works:
{!! Form::select('department_id', ['' => 'Please select'] + $departments, isset($user->departmentStaff->department_id) ? $user->departmentStaff->department_id : null , ['class' => 'form-control']) !!}
However the laravel shorthand ternary operator doesn't work, it throws an error:
{!! Form::select('department_id', ['' => 'Please select'] + $departments, $user->departmentStaff->department_id or null , ['class' => 'form-control']) !!}
This is from the official docs: https://laravel.com/docs/5.2/blade#control-structures
Related
I am using Kendo MVC wrapper for my grids
here is my column bound code.
columns.Bound(c => c.TypeId).Filterable(x => x.Multi(true).UI("").DataSource(ds => ds.Read("Types", "Ticket")).ItemTemplate("getTicketTypeFilter")).ClientGroupHeaderTemplate("#:getTicketType(value) #").EditorTemplateName("TicketTypes").ClientTemplate("#: Type.Name #");
with the help of this handler, I construct the check list for filters
function getTicketTypeFilter(e) {
console.log(e);
return '<li><label class="k-label"><input type="checkbox" value="#:data.Id#" />#:data.Name || data.all#</label></li>';
}
which will render my TYPE column with all options
Now the problem is I am applying default filters to TYPE field and others
.Filter(f =>
{
f.Add(field => field.StatusId).IsNotEqualTo(4);
f.Add(field => field.StatusId).IsNotEqualTo(6);
f.Add(field => field.StatusId).IsNotEqualTo(7);
f.Add(field => field.StatusId).IsNotEqualTo(8);
f.Add(field => field.TypeId).IsNotEqualTo(7);
f.Add(field => field.TypeId).IsNotEqualTo(5);
f.Add(field => field.PriorityId).IsNotEqualTo(5);
})
and when i hover on column filters, thee filters are not checked, but the data in grid is filtered.
How can I make the UI reflect the default filters.
I have developed a laravel 5 app and everything works fine except that I occationally get TokenMismatchException in VerifyCsrfToken.php line 53. I can't seem to figure out the cause. This happens mostly during login. I used the laravel built-in trait to implement authentication. Below is my sample logout method.
public function getLogout()
{
Auth::logout();
Session::forget('cart_id');
Session::forget('is_supervisor');
Session::forget('is_manager');
return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/');
}
Each time the exception occurs, I have to reload the page and login again before this works. Below is a sample of my login form:
{!! Form::open(array('class' => 'form-horizontal', 'role' => 'form')) !!}
<div class="form-group">
{!! Form::label('username', trans('home.username'), array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-9 col-lg-5">
{!! Form::text('username', null, array('required' => 'required', 'class' => 'form-control input-sm')) !!}
</div>
</div>
{!! Form::close() !!}
When I check the page source, the csrf token is always available. What could possibly be the problem. I am using laravel 5.1.* and SESSION_DRIVER=file in .env.
Edit
// Authentication routes...
Route::get('auth/login', 'Auth\AuthController#getLogin');
Route::post('auth/login', 'Auth\AuthController#postLogin');
Route::get('auth/logout', 'Auth\AuthController#getLogout');
CSRF Protection (Cross-Site-Request-Forgery) saves you from unwanted actions on web application.
The token is generated when rendering form and expired after some time.
If you have the form opened too long, you get TokenMismatchException.
Did you notice the problem shortly after reload page (form)?
Try to add
{{ csrf_field() }} to your form and check if it helps.
Hi I have a form with a number field. I use regex to validate the field. For that reason I added the pattern attribute the element. However when I use formText it html escapes the regex pattern.
//inside the form _construct
$this->add(array(
'name' => 'number',
'type' => 'text',
'options' => array(
'label' => 'Number',
),
'attributes' => array(
'pattern' => '/^(\+)?((\d)+(-|\s)?)+$/',
'maxLength' => '20',
'id' => 'number',
),
));
And in the form
<?php echo $this->formText($form->get('number')); ?>
The result is then
<input type="text" name="number" pattern="/^(\+)?((\d)+(-|\s)?)+$/" id="number" value="" maxlength="20">
How can I add the number field to my form without escaping the regex pattern?
Form view helpers are supposed to work that way, providing some baseline security features and automating stuff. So if you don't won't that don't use them:
<input type="<?php echo $form->get('number')->getType(); ?>" pattern="type="<?php echo $form->get('number')->getAttribute('pattern'); ?>" value="<?php echo $form->get('number')->geValue(); ?>">
Not sure what you need displayed, but it should give you a general idea of "my" approach. You can also manually escape stuff like value:
$this->escape($form->get('number')->geValue())
If you find this tedious, you can always write a helper that does this. You can also make PR with an option to turn of the escaping for attributes, but having them on by default is a sensible.
$form = new Zend_Form();
$form->addElement('text', 'fname', array('belongsTo' => 'user'));
I need to know where is addElement method defined? I have searched \vendor\ZF2\library\Zend([\Form]) directory but could not find this method!. If there is no such method then please help me to understand how this above two line work and what are other array conf & parameter of this method.
Edit: thanks Crisp. Actually I am trying to make an input array in zf2 like
<input name="val[one]" type="text" />
<input name="val[two]" type="text" />
<input name="val[three]" type="text" />
Or at least... like this
<input name="val[0]" type="text" />
<input name="val[1]" type="text" />
<input name="val[2]" type="text" />
I have found a example with above code and its not working as it is zf1.
In ZF2, programmatic form creation has changed from ZF1. There is no more addElement function, instead you add an element using $form->add($element);
The basic steps to create a form in ZF2 are:
Create a form element
Create a form
Add element to the form
Create a form element:
use Zend\Form\Element;
use Zend\Form\Form;
$name = new Element('name');
$name->setLabel('Your name');
$name->setAttributes(array(
'type' => 'text'
));
$send = new Element('send');
$send->setValue('Submit');
$send->setAttributes(array(
'type' => 'submit'
));
Create a form:
$form = new Form('contact');
Add element to the form:
$form->add($name);
$form->add($send);
However in ZF2, another way would be to use a Factory to generate the form from an array configuration:
use Zend\Form\Factory;
$factory = new Factory();
$form = $factory->createForm(array(
'hydrator' => 'Zend\Stdlib\Hydrator\ArraySerializable',
'elements' => array(
array(
'spec' => array(
'name' => 'name',
'options' => array(
'label' => 'Your name',
),
'type' => 'Text',
)
),
array(
'spec' => array(
'name' => 'send',
'type' => 'Submit',
'attributes' => array(
'value' => 'Submit',
),
),
),
),
));
The form is then referenced from the view and is rendered using form view helpers.
References:
http://framework.zend.com/manual/2.3/en/modules/zend.form.quick-start.html
https://zf2.readthedocs.org/en/develop/modules/zend.form.advanced-use-of-forms.html
http://akrabat.com/category/zend-framework-2/
In my ASP.NET MVC application I am using a select list control to generate a list for a multiselect widget:
<%=Html.ListBoxFor( m => m.Product.Name,
new SelectList(
Model.Products.Where(
s => !Model.Product.Any(
t => t.Id == s.Id.Value
) ).OrderBy( t => t.Name ), "Id", "Name",
new { multiselect = "multiselect", #class = "fancySelect products"} ) ) %>
Which will generate a list of items. The problem is that some of them need encoding:
<span>Cōnetic™ Technology</span>
If I render this item directly to the UI using a simple response.write I see this:
<p class="c">Cōnetic™ Technology</p>
How would I go about integrating an Html.Encode into my select list statement to produce the same encoding result? Or is there a better encoding method that will effect select lists on a global level?
This is MVC 2 btw, so no razor.
You need to encode data before displaying, and it should render as expected.
Here you example which we used in our asp.net mvc2 project:
<%= Html.Encode(ViewData["PasswordLength"]) %>
It is in a namespace System.Web.Mvc, and it converts the specified string to an HTML-encoded string.