16855

Laravel - Repopulate checkbox to ticked or unticked

Question:

In Laravel, if validation failed, it goes back to a same form page. How to repopulate a checkbox to ticked or unticked?

By default, it should be unticked when loading on the page for the first time.

{{ Form::checkbox('custom_address', false, Input::old('custom_address'), array('class' => 'test')); }}

Answer1:

You need to set a value for your checkbox, otherwise the Input class will always have it empty / unchecked.

Give the checkbox a value of 1, then it should work.

{{ Form::checkbox('custom_address', 1, Input::old('custom_address'), array('class' => 'test')); }}

Answer2:

just use a conditional.

@if(!empty(Input::old('custom_adress'))) {{ Form::checkbox('custom_address', true, Input::old('custom_address'), array('class' => 'test')); }} @else {{ Form::checkbox('custom_address', false, Input::old('custom_address'), array('class' => 'test')); }} @endif

Recommend

  • Collision Detection using bounded boxes
  • integrating python scripts with django
  • D3: How to draw multiple Convex hulls on Groups of Force layout nodes?
  • Get enum value from string
  • Implementing IDataErrorInfo using Castle.DynamicProxy in lazy loading scenario using NHibernate
  • Filling web form via PowerShell does not recognize the values entered
  • Struts2 validation is not working properly, validation error messages not shown
  • Time series plot range
  • BlueBird Promises returns “Undefined” when return inside “then” function
  • Getting the result of the promise returned by an asyncValidator
  • ng-pattern that allows decimal ratios
  • Javascript setCustomValidity does not work on Chrome Version 65
  • simple php mail not sending email
  • Sorting a DataTable using LINQ, when sort-by-columns may vary
  • Root element minOccurs or maxOccurs
  • TypeError: Cannot visit Mail::Multibyte::Chars
  • Custom Validator, should I use FacesContext#addMessage() or throw ValidatorException?
  • Failed to read schema document 'http://www.springframework.org/schema/tool/spring-tool-4.1.xsd&
  • How can include component js after templete js
  • My first C# WPF ValidationRule is not firing
  • jquery validation - waiting for remote check to complete
  • Best way to dynamically load an xml configuration file into a Flex4/Flash movie at runtime?
  • Login not working in Firefox in Meteor
  • Unable to install Git-core+svn by MacPorts
  • Transactional Create with Validation in ServiceStack Redis Client
  • JQuery Internet Explorer and ajaxstop
  • VSO Build — Response status code does not indicate success: 404 (Not Found)
  • How to delay loading a property with linq to sql external mapping?
  • ilmerge with a PFX file
  • Cannot connect to cassandra from Spark
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Spray.io: When (not) to use non-blocking route handling?
  • Modifying destination and filename of gulp-svg-sprite
  • GridView Sorting works once only
  • SQL merge duplicate rows and join values that are different
  • Proper way to use connect-multiparty with express.js?
  • How to set the response of a form post action to a iframe source?
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]