Using a command object boolean with a input:checkbox in grails - grails

This is probably a ridiculously easy question, but I've been struggling with it nonetheless.
I have a form:
.
.
.
<g:form controller='somecontroller' action='someaction'>
<g:textField name='name'/>
<g:checkBox name='active'/>
<g:submitButton name='save'/>
</g:form>
.
.
.
And I have a command object:
class MyCommandObject{
String name
Boolean active
static constraints{
.
.
.
}
}
When I try to access the active property of the command object in my controller, it's always false. The name property, however, is being properly populated. I've tried changing the value to true, and also ${true} (as I saw in some example) and none seem to work. What am I doing wrong here?
Edit: How I'm binding to the command object in the controller:
class MyController{
.
.
.
def save(MyCommandObject cmd){
// Do validation/error checking
def myDomainObject = new DomainObject()
myDomainObject.name = cmd.name
myDomainObject.active = cmd.active
// Do something with myDomainObject
}
}
I've also tried the style:
def save = { MyCommandObject cmd ->
.
.
.
}

Everything you've done looks fine and I've verified as working in 1.3.7 and 2.0.4.
Please do a grails clean and try again.

Related

How to edit an image in laravel 5 if user upload a new one ,otherwise upload with old image file

I have an edit form which has an image field where a user can upload a new image if he wants to.
But if the user does not upload a new photo I want to just use the photo that's already in the database. And not update the image field at all. But in my code whenever I am trying to without uploading new image form is not taking the old input value.
Here is my edit function:
public function expenseupdate1(){
$input = Input::only('id','Expense_date','Expense_category_id','Vendor_id','Customer_id','Amount','Tax1_id','Tax2_id','Note','Receipt');
$data=new Expense;
$id=$input['id'];
$Expense_date=$input['Expense_date'];
$Expense_category_id=$input['Expense_category_id'];
$Vendor_id=$input['Vendor_id'];
$Customer_id=$input['Customer_id'];
$Amount=$input['Amount'];
$Tax1_id=$input['Tax1_id'];
$Tax2_id=$input['Tax2_id'];
$Note=$input['Note'];
if(Input::hasFile('Receipt')) {
$file = Input::file('Receipt');
$name = time() . '-' . $file->getClientOriginalName();
$data->Receipt = $name;
$file->move(public_path() . '/images/', $name);
}
$affectedrows=Expense::where('id', '=', $id)->update(array('Expense_date' => $Expense_date,'Expense_category_id'=>$Expense_category_id,'Vendor_id'=>$Vendor_id,'Customer_id'=>$Customer_id,'Amount'=>$Amount,'Tax1_id'=>$Tax1_id,'Tax2_id'=>$Tax2_id,'Note'=>$Note,'Receipt'=>$Receipt));
return redirect('expenseinfo');
}
and here is my update form image field code:
<td> <div class="form-group"style="margin-left:-305px">
{!! Form::label('image', 'Receipt') !!}
<input Input::old('Receipt'), type="file" name="Receipt" value = '{{$data->Receipt}}'></td><td><?php echo $data->Receipt; ?></td>
</div></td>
<tr>
<td>{!! Form::submit('Update', array( 'class'=>'' )) !!}
{!! Form::close() !!}</td>
Any help would be appreciated greatly
You wouldn't set a default value for file.
The file input type creates a field
through which users can upload files
from their local computer or network.
The VALUE attribute specifies the name
of the initial file, but it is
typically ignored by browsers as a
security precaution.
So, your application is behaving correctly. Since the image is already in the database you wouldn't need to uploaded it again.
Also, just FYI but you can clean up your controller method dramatically!
/**
* Update Expense 1
*
* #param Request $request
* #return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function expenseupdate1(Request $request){
$expense = Expense::find($request->input('id'));
$expense->fill($request->only('id','Expense_date','Expense_category_id','Vendor_id','Customer_id','Amount','Tax1_id','Tax2_id','Note','Receipt'));
if($request->hasFile('Receipt')) {
$file = $request->file('Receipt');
$name = time() . '-' . $file->getClientOriginalName();
$expense->Receipt = $name;
$file->move(public_path('/images/'), $name);
}
$expense->save();
return redirect('expenseinfo');
}
The above assumes you have the necessary use statements at the top of you're controller i.e.
use Illuminate\Http\Request;
use App\Expense; //Assuming that Expense is in this namespace
If you haven't already, you should set the fillable array for you model to allow the fill() method (mass assignment) to work http://laravel.com/docs/5.1/eloquent#mass-assignment
There is even more you can do but I have already gone outside the scope of this question. I would, however, suggest looking at:
http://laravel.com/docs/5.1/routing#route-model-binding
http://laravel.com/docs/5.1/controllers#restful-resource-controllers
http://laravel.com/docs/5.1/controllers#dependency-injection-and-controllers
Hope this helps!

getarguments in typo3 from URL

I am working in TYPO3 .....
how can i get arguments from URL
i passed arguments in url like this ,
as a template in resources folder , file name : list.html
<f:form action="update" object="{hotel}" arguments="{myArgument: argumentname}" name="hotel">
and in controller in updateAction() , i want to fetch that agruments , so i write like this ,
$this->view->assign('hotel', array('test' => 'hello' . isset($this->arguments['myArgument']) .'##' . $this->getParametersSafely('myArgument')));
and i make the function in controller...
public function getParametersSafely ($parameterName) {
if ($this-> request-> hasArgument ($parameterName)) {
return $this-> request->getArgument($parameterName);
}
}
So please help me this is not working
I guess "this is not working" means the string in the fluid variable {hotel} is kind of not what you expect? Or what exactly is not working?
First of all isset() returns a boolean, taht you should not just add to your string.
Secondly, if you use arguments="{myArgument: argumentname}"fluid expects argumentname to be a variable passed to the template. If you want to pass a string, you need to specify it: arguments="{myArgument: 'argumentname'}".

How to override processForm() in admin-generator

I'm trying to add some extra logic to update and delete actions in a module generated by task doctrine:generate-admin. I read here and see that I can create executeUpdate() and executeDelete() methods in /application/mymodule/actions/actions.class.php. Because executeUpdate() call proccessForm() method then I think the best is to override this instead of executeUpdate() but don't know how. The code I need to add is this:
$q = Doctrine_Core::getTable('SdrivingEmpresa')
->createQuery('a')
->where('idempresa = ?', $request->getParameter('idempresa'))
->execute();
$file = new sfFilesystem();
$file->remove(sfConfig::get('sf_upload_dir') . '/' . $q[0]->getLogotipo());
I do it for executeDelete() by copying the generated cache coded and writing my logic see below:
public function executeDelete(sfWebRequest $request) {
$request->checkCSRFProtection();
$this->dispatcher->notify(new sfEvent($this, 'admin.delete_object', array('object' => $this->getRoute()->getObject())));
$q = Doctrine_Core::getTable('SdrivingEmpresa')
->createQuery('a')
->where('idempresa = ?', $request->getParameter('idempresa'))
->execute();
$file = new sfFilesystem();
$file->remove(sfConfig::get('sf_upload_dir') . '/' . $q[0]->getLogotipo());
if ($this->getRoute()->getObject()->delete()) {
$this->getUser()->setFlash('notice', 'The item was deleted successfully.');
}
$this->redirect('#sdriving_empresa');
}
But I tough this is not right, so my question is, should I do the same with processForm() or exists a better way to do this? any advice or help?
You can copy the proccessForm() generated in the cache of the application in your admin module action to override it.

How to get the current view being rendered inside ZF2

Is there a way to get the current view being rendered inside zend framework 2?
I believe this should be possible with the event system but I can't seem to make it work.
The reason I want to get this information is so I can automatically include a .js file with the same name, this would save me time having to specify this rule each time i'm inside a action.
Many thanks,
Tom
I'm not quite sure what you mean by rendering the current view inside ZF2, but here's how you can add a js file named after the action automatically. Just put this in your controller:
public function onDispatch(MvcEvent $mvcEvent)
{
$renderer = $this->serviceLocator->get('Zend\View\Renderer\PhpRenderer');
$actionName = $mvcEvent->getRouteMatch()->getParam('action');
$jsFile = $actionName . '.js';
$baseUrl = $mvcEvent->getRouter()->getBaseUrl();
$renderer->headScript()->appendFile($baseUrl . '/js/' . $jsFile);
return parent::onDispatch($mvcEvent);
}
You may need to adjust the code for your js file location and name of course. The onDispatch method is called automatically before the action.
Thank You Wunibald,
Your example worked perfectly, I have modified it below to be attached to an event so that it applies to every controller/module. To do this I have included it into the onBootstrap function in my Application module.
$events = StaticEventManager::getInstance();
$events->attach('Zend\\Mvc\\Application', 'dispatch', function(\Zend\EventManager\Event $event)
{
$baseUrl = $event->getRouter()->getBaseUrl();
$renderer = $event->getApplication()->getServiceManager()->get('Zend\View\Renderer\PhpRenderer');
$action = $event->getRouteMatch()->getParam('action');
$controller = $event->getRouteMatch()->getParam('controller');
if (strlen($controller) > 0)
{
list($module, $_null, $controller) = explode('\\', $controller);
$renderer->headScript()->appendFile($baseUrl . '/module/' . $module . '/view/' . strtolower($module) . '/' . strtolower($controller) . '/' . strtolower($action) . '.js');
$renderer->headScript()->appendFile($baseUrl . '/module/' . $module . '/view/' . strtolower($module) . '/' . strtolower($module) . '.js');
}
});
Once again thank you for pointing me in the right direction.

Symfony task: displaying colored text in the output (command line)?

I would like to have a very basic formatting (just colors) on the text displayed by a Symfony task in the command line output (just like the colors you see when running tests): any idea?
Thanks a lot
In your task you may use two functions, both with a $style parameter, to generate an output :
logBlock() - print a (colored) whole text
logSection() - print a (colored) section with an additional text
And here you can find Symfony's documentation.
You can try something like:
echo "\033[01;31m Show me some red colors \033[0m";
More colors here: http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
You can change the color of the Symfony terminal output by changing the file:
/lib/symfony/vendor/phing/listener/AnsiColorLogger.php
At the constructor method you can change the color of each type of output (info, error, debug...):
public function __construct() {
parent::__construct();
$this->errColor = self::PREFIX . self::ATTR_DIM . self::SEPARATOR . self::FG_RED . self::SUFFIX;
$this->warnColor = self::PREFIX . self::ATTR_DIM . self::SEPARATOR . self::FG_MAGENTA . self::SUFFIX;
$this->infoColor = self::PREFIX . self::ATTR_DIM . self::SEPARATOR . self::FG_GREEN . self::SUFFIX;
$this->verboseColor = self::PREFIX . self::ATTR_DIM . self::SEPARATOR . self::FG_CYAN . self::SUFFIX;
$this->debugColor = self::PREFIX . self::ATTR_DIM . self::SEPARATOR . self::FG_BLUE . self::SUFFIX;
}
You have a limited range of colors but it may help you.

Resources