⚠ In case you've missed it we are migrating to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This forum is read-only and soon will be archived. ⚠


API - How to use - getState


  • Please log in to reply
7 replies to this topic

#1 darkstalker

darkstalker

    Member

  • Members
  • PipPip
  • 26 posts

Posted 09 April 2017 - 09:40 AM

Hi John and everyone,

after long lurking time, i decided to join the forum to improve my understanding of Grocery CRUD and help others with my experience.

 

My first question is :

 

How to use gestState correctly ?

 

In the community version i can set rules depending of the state like this example:

 

$state = $crud->getState();

if ($state == 'add') {

  $crud->required_fields('customerName','contactLastName');

} else if ($state == 'edit') {

  $crud->required_fields('customerName');

}

 

but in the Enterprise version it doesn't seem to work with this example:

 

$state = $crud->getState();

if ($state == 'AddForm') {

  $crud->requiredFields(['customerName','contactLastName']);

} else if ($state == 'EditForm') {

  $crud->requiredFields(['customerName']);

}

 

The states are recognized but the rules are not working.

Anyone know how to solve this ?

 

Thank you all !

 

 

 

 

 

 



#2 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts

Posted 10 April 2017 - 06:00 AM

Hi,

 

The getState() is good to go with in the above situation. But u need to understand 1 thing - those rules will be set when the state is to show add form or edit form.

But this is more required when the validation is in process. So the rules need to be active when the state is in validation too and not just add / edit. 

Currently the code runs in. and keeps the rule intact for making sure the fields are declared as required. But the same is not gona get executed as the logic for current state is to show the form to the user and not validate the data. 

The same needs to be worked out for when the validation is in progress.

 

Hope the above have clarified your doubts as how to use the same. 

Happy GCing:)



#3 darkstalker

darkstalker

    Member

  • Members
  • PipPip
  • 26 posts

Posted 10 April 2017 - 07:18 AM

Hi Amit,

thanks for the reply !

 

I understood and also fixed my problem !

I just needed to check the state "Insert" other than "AddForm", as i was doing in the community version !

 

I write it here so it can be useful for other people.

 

Thanks again.



#4 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,166 posts

Posted 13 April 2017 - 07:05 AM

Hello @darkstalker,

 

You were very very close to your solution really. Just to make sure that someone else will have the solution right. You don't really need the AddForm and EditForm. You just need to have this code:
 

$state = $crud->getState();
if ($state == 'Insert') {
  $crud->requiredFields(['customerName','contactLastName']);
} else if ($state == 'Update') {
  $crud->requiredFields(['customerName']);
}
Thanks
Johnny


#5 xmyownprisonx

xmyownprisonx

    Member

  • Members
  • PipPip
  • 12 posts

Posted 09 October 2017 - 02:45 AM

 

Hello @darkstalker,

 

You were very very close to your solution really. Just to make sure that someone else will have the solution right. You don't really need the AddForm and EditForm. You just need to have this code:
 

$state = $crud->getState();
if ($state == 'Insert') {
  $crud->requiredFields(['customerName','contactLastName']);
} else if ($state == 'Update') {
  $crud->requiredFields(['customerName']);
}
Thanks
Johnny

 

 

Hi Johnny,

 

$user = $this->ion_auth->user()->row();

 

$state = $crud->getState();
if ($state == 'Insert') {

// How can get the username of the current user and save it to my table?

  

//i tried this one but all the field of userid got the same username. and inserting two row.

$this->db->insert('my_table', array('userid' => $user->username));

 

 

} else if ($state == 'Update') {
// How can get the username of the current user and save it to my table?

 

}

 

Thank you in advance. Have a great day and more power.

 



#6 larasmith

larasmith

    Advanced Member

  • Members
  • PipPipPip
  • 200 posts

Posted 10 October 2017 - 12:08 AM

 

 

Hello @darkstalker,

 

You were very very close to your solution really. Just to make sure that someone else will have the solution right. You don't really need the AddForm and EditForm. You just need to have this code:
 

$state = $crud->getState();
if ($state == 'Insert') {
  $crud->requiredFields(['customerName','contactLastName']);
} else if ($state == 'Update') {
  $crud->requiredFields(['customerName']);
}
Thanks
Johnny

 

 

Hi Johnny,

 

$user = $this->ion_auth->user()->row();

 

$state = $crud->getState();
if ($state == 'Insert') {

// How can get the username of the current user and save it to my table?

  

//i tried this one but all the field of userid got the same username. and inserting two row.

$this->db->insert('my_table', array('userid' => $user->username));

 

 

} else if ($state == 'Update') {
// How can get the username of the current user and save it to my table?

 

}

 

Thank you in advance. Have a great day and more power.

 

 

 

Hello there!

 

Since you are trying to get the current username, you can actually place it in a session variable upon login

so you can use it when you need to place it in your database.

 

Happy GCing!  :)



#7 xmyownprisonx

xmyownprisonx

    Member

  • Members
  • PipPip
  • 12 posts

Posted 10 October 2017 - 12:36 AM

Hello there!

 

Since you are trying to get the current username, you can actually place it in a session variable upon login

so you can use it when you need to place it in your database.

 

Happy GCing!  :)

<?php
                           $user = $this->ion_auth->user()->row();
                                echo $user->first_name;
                                echo " ";
                                echo $user->last_name;
 ?>

 

like this one, i just echo it out. Thank you Ms. Larasmith. xD have a great day.



#8 larasmith

larasmith

    Advanced Member

  • Members
  • PipPipPip
  • 200 posts

Posted 10 October 2017 - 12:38 AM

<?php
                           $user = $this->ion_auth->user()->row();
                                echo $user->first_name;
                                echo " ";
                                echo $user->last_name;
 ?>

 

like this one, i just echo it out. Thank you Ms. Larasmith. xD have a great day.

 

You're welcome... Happy GCng!  :)






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users